Sample Header Ad - 728x90

ssh with separate stdin, stdout, stderr AND tty

10 votes
1 answer
4578 views
### Problem Consider a command like this: binary_output 2>error.log where tool is arbitrary and ssh is a wrapper or some ssh-like-contraption that allows the above to work. With regular ssh it doesn't work. I used sudo here but it's just an *example* of a command that requires tty. I'd like a general solution, not specific to sudo. --- ### Research: the cause With regular ssh it doesn't work because: - sudo needs tty to ask for password (or to work at all ), so I need ssh -t ; actually in this case I need ssh -tt. - On the other hand ssh -tt will make sudo read the password from binary_input. I want to provide the password via my local tty. Even if sudo is configured to work without password or if I inject the password to the binary_input, ssh -tt will make sudo and tool read from the remote tty and write output *and* errors and prompts to the remote tty. Not only I won't be able to tell the output and the errors/prompts apart locally. All the streams will be processed by the remote tty and this will mangle data (you can see this in some examples in this answer of mine , in the section entitled "Some practice"). --- ### Research: comparison to commands that work - This local command is the reference point. Let's assume it successfully processes some binary data: binary_output - If I need to run tool on a server, I can do this. Even if ssh asks for my password , this will work: binary_output In this case ssh is transparent for binary data. - Similarly local sudo can be transparent. The following command won't mangle the data even if sudo asks for my password: binary_output - But running tool on the server with sudo is troublesome: binary_output In this configuration ssh and sudo *together* cannot be transparent in general. Finding a way to make them transparent is the gist of this question. --- ### Research: similar questions I have found few similar questions: - Use sudo with ssh command and capturing stdout This question cares about stdout only. The existing answer (from the author of the question) advises sudo -S which consumes stdin. I don't really want to alter my binary_input. And I would appreciate a solution not specific to sudo. - stderr over ssh -t This concentrates on passing Ctrl+c and the background is GNU parallel. A workaround that only makes Ctrl+c work without a remote tty is not enough for me. - SSH: Provide additional “pipe” fds in addition to stdin, stdout, stderr This is a good start (especially this answer , I think). However here I want to emphasize the need for tty. I want a solution that automates things and allows me to use remote sudo (or whatever) as if it was local. --- ### My explicit question In the following command: binary_output 2>error.log requires-tty is a placeholder for code that requires a tty but processes binary data from its stdin to its stdout. It seems I need ssh -tt, otherwise requires-tty will not work; and at the same time I mustn't use ssh -tt, otherwise the binary data will be mangled. How can I solve this problem in a convenient way? requires-tty can be sudo … but I don't want a solution specific to sudo. I imagine the ideal(?) solution will be a script/tool that replaces ssh in the above invocation and just works. It should(?) connect the remote stdin, stdout and stderr each to its local counterpart, *and* the remote tty to the local tty. If it's possible, I prefer a client-side solution that does not require any server-side companion program.
Asked by Kamil Maciorowski (24294 rep)
Jun 8, 2021, 05:42 PM
Last activity: Jun 18, 2025, 07:35 AM