我正在尝试将ssh-ing服务器的expect脚本输出的一部分写入日志文件 现在我有这个脚本:
#!/usr/bin/expect -f
spawn telnet 192.168.20.222
match_max 10000
expect "*?to continue*"
send -- "\r"
send -- "show interfaces 1 \r"
expect -- "*?2626#*"
send -- "show interfaces 2 \r"
expect -- "*?2626#*"
send -- "exit \r"
expect -- "*?2626>*"
send -- "exit \r"
expect "*?y/n*"
send -- "y \r"
如何将其更改为输出到文件'log.txt'只是来自show interfaces 1
的响应和
show interfaces 2
?
答案 0 :(得分:1)
...
log_file "log.txt" ;#enable loging to file as well
send -- "show interfaces 1 \r"
expect -- "*?2626#*"
send -- "show interfaces 2 \r"
expect -- "*?2626#*"
log_file; #disable logging
...
'log.txt'将包含来自命令+命令本身+提示的输出。 这还够吗?
获得纯输出可能更复杂,有用的技术是使用显式括号(不确定您正在与之交互的系统是否允许类似的东西)。在shell中它看起来像:
send -- "show interfaces 1\r echo '<XYZ>'\r"
expect "<XYZ>"; #first echoed when the command was typed
expect {
"<XYZ>" {
#for the second time echoed when the command finished
#here the $expect_out(0,string) contains the command output + the bracket
regexp {(.*)<XYZ>} $expect_out(0,string) match pure_cmd_output
}
}