-1

Is there a way I can IF CONDITION an expect command. For example in the below code, I am trying to ssh into a specific IP address and sometimes it asks me "Are you sure you want to continue connecting (yes/no)?" and sometimes it doesn't. How do I make it so that IF it shows up I send "yes/r" and IF it doesn't, I continue on with my code?

set name="123.456.78.910"
expect "$ "     ;       send -- "ssh root@$name\r"
expect {
  "Password:" { send "password" }
  timeout   { sleep 1 }

}

expect "Are you sure you want to continue connecting (yes/no)?" ; send -- "yes\r”
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
YuanL
  • 67
  • 8

1 Answers1

1

This is the purpose of the exp_continue command:

spawn ssh user@host

set prompt {\$ $}    ;# or whatever regex your prompt matches

expect {
    timeout {error "timed out connecting to host"}
    "continue connecting" {send "yes\r"; exp_continue}
    "Password:" {send "$password\r"; exp_continue}
    -re $prompt
}

send "do stuff\r"
# ...

If expect times out, the script errors.
If you get the "continue connecting" prompt, send "yes" and keep expecting things.
If you get the "Password" prompt, send your password and keep expecting things.
When you get your shell prompt, the expect command completes and you carry on with your program.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352