使用expect登录并发出命令

时间:2014-12-20 17:03:19

标签: expect

你能告诉我为什么这个Expect脚本会以循环模式向我显示日历吗? 我只想看一个日历。

#!/usr/bin/expect
set fid [open /Users/john/secret]
set password [read $fid]
close $fid

spawn ssh [lindex $argv 0]@[lindex $argv 1]
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
}

"*?assword:*" {
send $password
send "\n"
exp_continue
}

"*$*" {
send "cal\r"
exp_continue
}

"*$*" {
send "exit\r"
}

1 个答案:

答案 0 :(得分:2)

这对我来说似乎不对

"*$*" {
send "cal\r"
exp_continue
}

"*$*" {
send "exit\r"
}

检查美元符号是否处于同一水平,因此在您发送cal\r后,您将循环播放,它将再次查找$并再次发出cal\r,但未到达最后一个"*$*" { 1}}

你可能会这样做吗?

#!/usr/bin/expect
set fid [open /Users/john/secret]
set password [read $fid]
close $fid

spawn ssh [lindex $argv 0]@[lindex $argv 1]
expect {
    -re ".*Are.*.*yes.*no.*" {
        send "yes\n"
        exp_continue
    }
    "*?assword:*" {
        send $password
        send "\n"
        exp_continue
    }
    "*$*" {
        send "cal\r"
        expect "*$*" {
            send "exit\r"
        }
    }
}

你应该注意到我在第一次检查中嵌套了$的第二次检查。我还假设你在原始问题的底部留下了一个结束括号?