2012-04-09 39 views
0

我正在嘗試將期望腳本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 1show interfaces 2的反應如何?

回答

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'將包含來自命令+命令本身+提示的輸出。 這是否足夠?獲得純輸出可能會更復雜,有用的技術是使用明確的括號(不確定您正在與之交互的系統是否允許類似的東西)。在外殼上它看起來像:

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 
    } 
}