我有一个自动预期脚本。我修改了它并且效果很好,但是当它完成运行时它会挂起,我需要执行 ctrl+C 才能获取我的终端
[..]
887VA#logout
Connection to 10.255.255.1 closed by remote host.
Connection to 10.255.255.1 closed.
root@blackbox:/etc/myscripts#
^Croot@blackbox:/etc/myscripts# ^C
root@blackbox:/etc/myscripts#
在上面的空白处我按了多次回车键
代码
#!/usr/bin/expect -f
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
set timeout -1
spawn $env(SHELL)
match_max 100000
send -- "ssh [email protected]\r"
expect -exact "Password: "
send -- "passwordhere!!!\r"
expect -exact "887VA#"
send -- "show interface vlan 2\r"
expect -exact "Vlan2 is up, line protocol is up \r"
send -- "logout\r"
expect eof
答案1
您生成一个 shell,发送 ssh 命令,执行一些操作,然后注销 ssh 会话。你永远不会退出外壳。
我通常会编辑自动期望生成的脚本,以不生成 shell,而是实际生成我想要执行的操作。
简短的重写:
#!/usr/bin/expect -f
# default timeout is 10 seconds
spawn ssh [email protected]
expect -exact "Password: "
send -- "passwordhere!!!\r"
expect -exact "887VA#"
send -- "show interface vlan 2\r"
expect -exact "Vlan2 is up, line protocol is up \r"
send -- "logout\r"
expect eof
关于脚本的逻辑:如果线路协议是,您应该做什么不是向上?