I'm attempting to write a script that will login to a remote server via ssh, without popping up a password prompt to the user (the remote server is not under my control, and they won't allow me to use key authentication).

Normally I just pass the password into the command, and this works fine, but every 3 months, they want me to change the password, so it pops up a password prompt, which breaks my script. I've tried capturing the password prompt via STDOUT and STDERR, but it is not getting sent to either one. I'm not sure how to capture the prompt, or send the response back.

As a test, I've tried just doing ssh without a password, and get the same basic behavior.

ssh user@sdf.org 2> /dev/null

still shows the password prompt, e.g.

so does ssh -T and ssh -t

any tips would be appreciated.


  • 4
    I'd expect expect could detect the password prompt, and otherwise automate SSH.
    – thrig
    Feb 25 '16 at 20:39
  • If you're just looking to login without a password, SSH keys might work for you. They don't prompt the user for a password, but you'd need to set them up first which is what I assume is the limiting factor here Feb 25 '16 at 22:38
  • @thrig -- looks like expect works as expected :) -- thanks much -- if you want to put that into an answer, I'll flag it -- have just done some basic tests so far (have to get the remote server admin to reset my password for a full test), but looks like it should work great.
    – Kem Mason
    Feb 26 '16 at 1:17
  • @perhapsmaybeharry -- key authentication would work awesome, but as I mentioned in the description, the remote admins won't allow me to use key authentication, and the server isn't under my control -- I get the impression they think it is less secure than having a password that changes every 3 months.
    – Kem Mason
    Mar 1 '16 at 20:33

Use expect, or an implementation of such in another language (Wikipedia has a list of these at present). expect was designed for precisely this sort of terminal automation task. The quickest way to bootstrap this automation might run along the lines of:

autoexpect ssh user@sdf.org

And then mash the keyboard as necessary, exit ssh, and inspect the resulting script file.

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.