0

I tried my hand at an expect script that's supposed to copy an ssh public key up to a remote server and copy it into place so that you'll be able to ssh into it wihout having to enter in a password next time.

I'm trying to allow for a host connecting for the first time to expect the complaint about an unrecognized host key:

#!/usr/bin/expect -f
set PUB "~/.ssh/id_rsa.pub"
spawn scp $PUB [email protected]:
expect {
 "(Are you sure you want to continue connecting yes/no)?" { send "yes\r"; exp_continue }
  password:                                                {send secret!\r; exp_continue}
  }
expect "password: "
send "secret!\r"

This is what happens when I actually run the script:

#./bin/ssh-login.ssh
spawn scp ~/.ssh/id_rsa.pub [email protected]:
spawn ssh [email protected] /bin/mkdir .ssh && /bin/chmod 700 .ssh && /bin/cat id_rsa.pub >> .ssh/authorized_keys && /bin/chmod 600 .ssh/authorized_keys
The authenticity of host '100.114.116.106 (100.114.116.106)' can't be established.
RSA key fingerprint is 3b:04:73:25:24:f3:aa:99:75:a7:98:62:5d:dd:1b:38.
Are you sure you want to continue connecting (yes/no)? secret!
Please type 'yes' or 'no':

Basically the problem is that the line I'm trying to setup to expect the "Are you sure you want to continue connecting yes/no" string is not being recognized by the script.

Any suggestions on how I can improve that so that those lines match?

Thanks

1 Answer 1

1

You have a typo in your script - the opening bracket on the "Are you sure..." line should be before the "yes/no", not at the start. The fixed version:

#!/usr/bin/expect -f
set PUB "~/.ssh/id_rsa.pub"
spawn scp $PUB [email protected]:
expect {
 "Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue }
  password:                                                {send secret!\r; exp_continue}
  }
expect "password: "
send "secret!\r"

...works fine for me

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.