0

サーバーへの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"

show interfaces 1と からの応答だけをファイル「log.txt」に出力するように変更するにはどうすればよいshow interfaces 2 ですか?

4

1 に答える 1

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
   }
}
于 2013-02-20T11:20:39.593 に答える