为什么Expect转储命令没有执行?

时间:2011-06-17 20:49:19

标签: expect

我正在学习Expect,我注意到我的Expect脚本有时会达到他们开始转储命令而不执行它们的程度。是什么导致这种情况发生?我已经删除了Google和一些Expect教程,但与此问题相关的关键字并未过滤答案。非常感谢任何指导或参考。

示例期望脚本:

#!/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."
    }
}

输出:

$ ./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.

1 个答案:

答案 0 :(得分:1)

Echo不是命令,因此它可能会显示,因为它不会告诉它做任何事情。

请改为尝试:

#!/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."
    }
}