我正在设置备份运行配置,但没有按照我喜欢的方式执行,如何将数据转储到txt中?
#!/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
expect -exact "example@host:~\$ "
send -- "telnet xxx.xxx.xxx.xxx"
expect -exact "telnet xxx.xxx.xxx.xxx"
send -- "\r"
expect -exact "\r
Trying 180.xxx.xxx.xxx...\r
Connected to 180.xxx.xxx.xxx.\r
Escape character is '^\]'.\r
\r
\r
User Access Verification\r
\r
Username: "
send -- "exampleuser"
expect -exact "exampleuser"
send -- "\r"
expect -exact "\r
Password: "
send -- "examplepasword\r"
expect -exact "\r
CCEMMT01-01>"
send -- "enable\r"
expect -exact "enable\r
Password: "
send -- "examplepasword\r"
expect -exact "\r
CCEMMT01-01#"
send -- "sh run "
expect -exact "\r
CCEMMT01-01#sh running-config "
send -- "\r"
expect -exact "\r
CCEMMT01-01#"
send -- "exit\r"
expect -exact "exit\r
example@host:~\$ "
send -- ""
expect eof
example@host:~$
答案1
我想你的问题就在这里:
expect -exact "\r
CCEMMT01-01#"
send -- "sh run "
expect -exact "\r
CCEMMT01-01#sh running-config "
send -- "\r"
看起来您正在发送sh run
,但随后期望CCEMMT01-01#sh running-config
打印出来,但这可能不会发生。
尝试:
expect -exact "\r
CCEMMT01-01#"
send -- "sh running-config \r"
这将等待命令提示符,然后发送命令sh running-config
。