Cisco デバイスにログインするための小さな Expect スクリプトを作成しました。ログインしたら、コマンドとgrep
出力を繰り返し実行したいと思います。
#!/usr/bin/expect
send_user "Device name: "
expect_user -re "(.*)\n"
set host $expect_out(1,string)
send_user "Username: "
expect_user -re "(.*)\n"
set user $expect_out(1,string)
stty -echo
send_user -- "Password: "
expect_user -re "(.*)\n"
set pass $expect_out(1,string)
stty echo
send_user "show int "
expect_user -re "(.*)\n"
set intf $expect_out(1,string)
send_user "\n"
spawn telnet $host
expect "Username:"
send "$user\r"
expect "Password:"
send "$pass\r"
expect ">"
この時点で、デバイスにログインしました。コマンド「show int xxx」を繰り返し実行し、特定の行の出力を grep します。grep
Expect にも のようなコマンドもないので、特定の行からコマンドをsleep
実行してループすることができます。このようにExpectとBashを混在させるにはどうすればよいですか?show int
grepping
更新: スクリプトはほぼ完成しました。この最後のハードルを乗り越えたら、完全なスクリプトを投稿します。行set bytesnow [exec grep "packets input" \< showint | cut -d \ -f 9]
がエラーをスローしています。
child process exited abnormally
while executing
"exec grep "packets input" < \showint | cut -d \ -f 9"
しかし、私が書いたテスト スクリプトでは問題なく動作します。ファイル ./showint があり、コマンド ラインでそのコマンドを実行すると正常に動作しますか? 何が問題なのかわかりませんか?
更新: さらなる調査 (http://wiki.tcl.tk/8489) によりgrep
、ステータス コード 1 で終了することがわかりました。これは、パターンの一致が見つからなかったことを意味します。コマンドはコマンド ラインから正常に動作しますか? /full/path/to/showint でも。
END : 私は自分がいかに愚かであったかを理解することで間違いを修正しました。以下に回答します。助けてくれてありがとう :D