0

I'm learning Expect, and I've noticed that my Expect scripts sometimes reach a point where they begin to dump commands without executing them. What causes this to happen? I've scraped Google and a couple Expect tutorials, but the keywords related to this issue are not filtering an answer. Any guidance or references are greatly appreciated.

Example Expect Script:

#!/usr/bin/env expect
set ip [lindex $argv 0]
spawn ssh root@$ip /dir/run.sh
expect {
    "password:" {
        send "secret_password\n"
    }

    "No route to host" {
        spawn echo "This gets dumped, not executed."
    }
}

Output:

$ ./ex.exp 192.168.0.22
spawn ssh root@192.168.0.22 /dir/run.sh
ssh: connect to host 192.168.0.22 port 22: No route to host
spawn echo This gets dumped, not executed.
Cryo
  • 817
  • 1
  • 7
  • 16

1 Answers1

1

Echo is not a command, so maybe it displays instead since it doesn't tell it to do anything.

Try this instead:

#!/usr/bin/env expect
set ip [lindex $argv 0]
spawn ssh root@$ip /dir/run.sh
expect {
    "password:" {
        send "secret_password\n"
    }

    "No route to host" {
        puts stdout "Host not reachable."
    }
    "Connection refused" {
        puts stdout "Host not accepting ssh."
    }
}
wallyk
  • 56,922
  • 16
  • 83
  • 148