3

when I have to do ssh to one of my machine below is the command and if I type 'yes' it is working and able to login as below.

ssh [email protected]
The authenticity of host '192.168.1.177 (192.168.1.177)' can't be established.
ED25519 key fingerprint is SHA256:KqI6oKKY1JOH+OJZzCYObPdkMVNNwhkaMGTYgx/fDxE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.177' (ED25519) to the list of known hosts.
localhost ~ # 

Same thing I am trying through expect script and it throws me error as below.

 ./expectscriptssh.sh 192.168.1.177
spawn ssh [email protected]
invalid command name "fingerprint"
    while executing
"fingerprint"
    invoked from within
"expect "Are you sure you want to continue connecting (yes/no/[fingerprint])? ""
    (file "./expectscriptssh.sh" line 4).

Below is my expect script:

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect "Are you sure you want to continue connecting (yes/no/[fingerprint])? "
send "yes\r"

Can any body suggest, how can I resolve this.

4
  • Aren't expect patterns regular expressions? In that case, )? is not matching a question mark after a right parentheses, but says "match an optional )". Likewise [fingeprint] would match one of the characters in [...]. Even if they are only globbing patterns, [fingerprint] would cause an issue for you.
    – Kusalananda
    Jan 29, 2021 at 8:28
  • 1
    @Kusalananda Whilst patterns in expect can be regular expressions, they are not by default. One uses -re as a switch to enable them.
    – icarus
    Jan 29, 2021 at 9:17
  • @icarus Ah, there you go. Your TCL-foo is stronger than mine.
    – Kusalananda
    Jan 29, 2021 at 9:26
  • Thanks much. I tried your inputs and now script is working for me. Really helpful.
    – Rags
    Jan 30, 2021 at 7:26

1 Answer 1

5

In TCL, the [ ] pair is a call to do command substitution, rather like $( ) is in POSIX shells.

If you have autoexpect available then the simplest way to write an expect script is to use autoexpect to watch you doing something and then edit the resulting script to remove unneeded stuff.

You can change the "..." to {...} to avoid the evaluation of the string.

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect {Are you sure you want to continue connecting (yes/no/[fingerprint])? }
send "yes\r"

Usually you want something more, to allow the prompt to be optional, e.g.

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect {
    {Are you sure you want to continue connecting (yes/no/[fingerprint])? } {
        exp_send "yes\r"
        exp_continue
    }
    {Password:} {send "secret\r"}
    {# }
}
6
  • @ Kusalananda & @glenn jackman: I would like to thank both of you for all the inputs and help. I took your inputs and tried again. Script looks good and working for me. Thanks a lot. Below is how script looks now and it is perfectly working.
    – Rags
    Jan 30, 2021 at 7:10
  • #!/usr/bin/expect set VAR [lindex $argv 1] set timeout 60 log_file -noappend myfile.log spawn -noecho ssh root@$argv expect { timeout {send "exit\r"} "connecting (yes/no/)?" { send -- "yes\r"} "localhost*" { send -- "exit\r"} } expect { "localhost*" { send "exit\r"} }
    – Rags
    Jan 30, 2021 at 7:13
  • Thank you glenn jackman. I tried inputs. Script is working for me. Thanks bunch.
    – Rags
    Jan 30, 2021 at 7:27
  • @Rags Good! If this solves your issue, please consider accepting the answer.
    – Kusalananda
    Jan 30, 2021 at 8:00
  • @icarus, I am not sure how you got that to work. From the answer of this question, we still need the escape character for the square brackets. Thus, [fingerprint] should change to \[fingerprint\] for it to work.
    – CaTx
    Feb 24, 2023 at 4:14

You must log in to answer this question.

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