[英]expect script write to file partial output
我正在嘗試將期望腳本 ssh-ing 的 output 的一部分寫入服務器到日志文件。
現在我有這個腳本:
#!/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"
我如何將它更改為 output 到文件“log.txt”,只是來自show interfaces 1
和show interfaces 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' 將包含來自兩個命令 + 命令本身 + 提示的 output。 這夠了嗎?
獲得純 output 可能更復雜,有用的技術是使用顯式括號(不確定您正在與之交互的系統是否允許類似的東西)。 在 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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.