我正在尝试将 expect 脚本 ssh-ing 到服务器的部分输出写入日志文件。
现在我有这个脚本:
#!/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
的响应和
显示接口(interface) 2
?
最佳答案
...
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
}
}
关于linux - 期望脚本写入文件部分输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10078153/