我遇到了问题。我有一个简单的期望脚本,我在php脚本中调用登录到cisco终端服务器以在实验时间结束后清除vty行。逻辑很简单,如果用户存在,那么它只占用行vty 0。
如果用户不存在,则会出现问题... cisco路由器只显示错误,指出%connection连接为clear或者某些东西,并且期望运行其余的脚本,这是不必要的,因为我正在管理10个路由器的实验室并加载需要花费大量时间的配置文件。
如果我收到该消息,请告诉我如何从日志和结束脚本中读取错误。
#! /usr/bin/expect -f
spawn telnet 192.168.2.1
set user [lindex $argv 0]
set pass [lindex $argv 1]
expect "Username:"
send "$user\r"
expect "Password:"
send "$pass\r"
expect "*>"
send "enable\r"
expect "*#"
send "clear line vty 0\r"
send "\r"
expect "*#"
send "copy tftp://192.168.2.3 running-config\r"
send "config\r"
send "\r"
expect "*#\r\n"
答案 0 :(得分:3)
您要问的错误处理代码在这里......
send "clear line vty 0\r"
expect {
"confirm]" { send "\r" }
"% Not allowed" { send "quit\r"; exit }
}
expect "#"
这会查找[confirm]
或% Not allowed to clear current line
,并做出适当的决定/不做决定。
我对你的脚本做了一些修改,所以我可以在我实验室的机器上运行它。我不确定您是如何禁用路由器请求启用密码的,但这可能会添加到您的脚本中。
如果您想使脚本更加健壮,可以在清除行之前发出show user
,并且只清除脚本未打开的行。我将把它作为练习让你去试验。
#!/usr/bin/expect -f
spawn telnet 172.16.1.5
set user [lindex $argv 0]
set pass [lindex $argv 1]
expect "Username: "
send "$user\r"
expect "assword: "
send "$pass\r"
expect ">"
send "enable\r"
expect "assword: "
send "$pass\r"
expect "#"
send "clear line vty 0\r"
expect {
"confirm]" { send "\r" }
"% Not allowed" { send "quit\r"; exit }
}
expect "#"
send "copy tftp://192.168.2.3 running-config\r"
expect "Source filename *]? "
send "config\r"
expect "Destination filename *]? "
send "running\r"
expect "#"
exit