My example is that I'm tunneling through a server and want to create a command to type in a password but leave stdin open once that's done. For example I need to sudo to a different user everytime I login to this remote host. So I do something like:

ssh -p $tunnel_port $me@localhost -t "sudo -S -u $_remoteUser -i" <<< "$( findPassword )"

'findPassword' is a function that gets my password out of a password manager

This closes the connection, how do I keep the connection open after the password has been written to stdin for sudo?

  • 2
    Ordinarily, sudo doesn't read from stdin at all, but opens the terminal directly, presumably to deal with just this problem. I don't think there's an easy way to feed data, send EOF, then feed more data in a way that applications will accept. expect, maybe. – Tom Hunt Sep 18 '15 at 22:30
  • 1
    -S causes sudo to read from stdin. – Mark Wagner Sep 18 '15 at 23:25
  • 4
    Use something that passes the stdin afterwards, e.g. cat: ( findPassword; cat ) | ssh ........ – Arthur2e5 Sep 19 '15 at 1:37
  • I tried your suggestion to pipe the password in instead of using the here document <<< but got the same result where the connection is closed after the password is sent over stdin. I want to connection to remain open. And I can't use cat in either case because cat waits for the end-of-file before proceeding. It would allow me to add custom input if I know what's coming after ssh connects but it's not interactive. – jaxzin Sep 19 '15 at 23:23

You have the quoting such that STDIN of the ssh command is being fed by findPassword. You want the STDIN of the sudo command to be fed by findPassword.

ssh -p $tunnel_port $me@localhost -t "echo $(findPassword) | sudo -S -u $_remoteUser -i"


ssh -p $tunnel_port $me@localhost -t "sudo -S -u $_remoteUser -i < $(findPassword)"

  • 1
    Wouldn't < $(findPassword) try to read from a filename of whatever that variable expands out to? – thrig Sep 18 '15 at 23:48
  • 1
    Not that with these recommendations, findPassword will run on the remote machine. In this case the remote machine is localhost, so it's not an issue. But conceptually, this is worth keeping track of. Is there some problem with feeding the STDIN to ssh? Won't it just pass that on to the process running on the remote machine? I'd expect @Arthur2e5's suggestion (in a comment to the question) to work. – dubiousjim Sep 19 '15 at 7:20
  • Sorry for the confusion, I've setup a tunnel to a remote server. So while I'm connecting to a port on localhost, the terminal is connected to a remote server. You are correct that Mark's suggestion won't work because he's changed the logic to run findPassword on the remote server when in fact it needs to run on my local machine (hence the here document) – jaxzin Sep 19 '15 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.