My company offered me a server to do my code job. I have to enter a passphrase every time while login the server.

~$ ssh -o StrictHostKeyChecking=no -p 22 [email protected]
Enter passphrase for key '/home/me/.ssh/id_rsa':

After entering the passphrase, I will be able to login.

Now I'm trying to make a bash script to avoid typing the passphrase every time. Here is my script:

#!/usr/bin/expect -f

set ip x.x.x.x
set password mypwd
spawn ssh -o StrictHostKeyChecking=no -p 22 me@$ip
expect {
#"*yes/no" { send "yes\r"; exp_continue}
"*passphrase*" { send "$password\r" }

However, when I execute the script, it doesn't seem to work as expect, I get the output as below and I still have to type the passphrase every time:

spawn ssh -o StrictHostKeyChecking=no -p 22 [email protected]
Enter passphrase for key '/home/me/.ssh/id_rsa':

Did I do something wrong?

BTW, x.x.x.x is just an IP4.

  • 1
    If you're going to store the passphrase in a text file like this, you could just remove the passphrase from the key.
    – Kenster
    Commented Mar 10, 2021 at 12:27

1 Answer 1


Note, there is no point putting glob * patterns at the start and end of "*passphrase*", as simply "passphrase" will be the match.

The problem may be that, as man expect says: Sometimes a delay is required between a prompt and a response. This may be due to the application switching off echo, and then flushing the input at that time. You should at least try to match the very last character of the prompt, in this case a colon (perhaps with a following space?), so pattern "passphrase*:" might work.

If it still does not, try adding a short wait:

{ sleep 0.5; send "$password\r" }

You can also run with debug expect -d to investigate.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .