简单的EXPECT脚本来执行远程命令并显示输出

我正在尝试连接到networking路由器并在其上执行show status
目前我正在使用:
spawn ssh -o StrictHostKeyChecking=no [email protected]
expect " * ? password:*\r"
send -- "secretPassword\r"
sleep 5
send -- "show status\r"
sleep 10
send -- "exit\r"

它不工作,我卡住在[email protected]'s password:我已经尝试input密码,但它不工作,我得到:
server1:~# secretPassword
-bash: server1: command not found
server1:~#

我在这里做错了什么…?

尝试这样做

 #!/usr/bin/expect -f set timeout 120 spawn ssh -o StrictHostKeyChecking=no [email protected] expect "*?assword:*" send -- "secretPassword\r" sleep 5 send -- "show status\r" sleep 10 send -- "exit\r" expect eof 

如果您的设备响应速度较慢,则可能需要设置适当的超时时间。

首先,您应该考虑使用RANCID自动化收集和跟踪路由器信息的整个过程,而不是一次性解决scheme。

对于这个特定的问题,看看autoexpect自动创build你期望的脚本。 这应该给你一个工作期望的脚本开始。 要修复您现有的脚本,请尝试使用-d参数运行expect。 这将告诉你,期望匹配什么,并希望告诉你什么是你的匹配expression错误。