开发者

Why does Expect dump commands without execution?

开发者 https://www.devze.com 2023-03-13 15:28 出处:网络
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 co

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.


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."
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消