Expect是在提示输入用户凭据时发送$expect_out(缓冲区),而不是指定变量($user)。然后就超时了。以下是代码:
...
}
"yes" {
send_user "\nEnter your username for the WLC supporting the new APs:\n"
sleep 6
}
}
expect {
-re "(.*)\n" {
set user $expect_out(1,string)
}
}
send_user "\nEnter your password for the WLC supporting the new APs:\n"
sleep 15
expect {
-re "(.*)\n" {
set pass $expect_out(1,string)
}
}
sleep 1
send_user "\nSshing to the IP of the WLC supporting the new APs ($wlc_temp)\n"
spawn ssh $wlc_temp
expect {
"User:" {
send $user\n
sleep 1
}
}
expect {
"assword:" {
send $pass\n
sleep 1
}
}结果如下:
用户:(设备主机名)用户:
以下是首次亮相:
支持新的APs ()派生ssh父级的WLC的IP :等待同步字节父级:告诉子进程前进,父级:现在与子生成不同步:返回{7153} expect:"“(spawn_id exp6)是否与”用户:“匹配?不是 expect:"\r\n“(spawn_id exp6)与glob模式"User:”匹配吗?否(设备主机名)用户: expect:“\r\n(设备主机名) User:”(spawn_id exp6)与glob模式"User:“匹配吗?是的: set expect_out(0,string) "User:“expect: set expect_out(spawn_id) " exp6”expect: set expect_out(缓冲区)“\r\n(设备主机名)用户:”发送:发送"\n“到{exp6} 预期:“”(spawn_id exp6)匹配单词"assword:“吗?不是 (设备主机名)用户: expect:“\r\n(设备主机名)用户:”(spawn_id exp6)与glob模式"assword:“匹配吗?没有期望:超时
更新:将请求用户输入的语句重新定位到脚本的顶部,作为一项工作。
发布于 2013-06-25 08:12:10
当您想从用户读取输入时,应该使用expect_user而不是expect。
send_user "\nEnter your username for the WLC supporting the new APs:\n"
sleep 6
}
}
expect_user {
-re "(.*)\n" {
set user $expect_out(1,string)
}
}
send_user "\nEnter your password for the WLC supporting the new APs:\n"
sleep 15
expect_user {
-re "(.*)\n" {
set pass $expect_out(1,string)
}
}
sleep 1
send_user "\nSshing to the IP of the WLC supporting the new APs ($wlc_temp)\n"
# ...(我还认为可以在其中使用更少的sleep调用,而只是调整超时,但这不太可能对脚本的正确操作产生影响。)
https://stackoverflow.com/questions/17290385
复制相似问题