expectでログ出力の制御
expectで実行する際に必要な部分だけをログに出力する方法
test.sh
#!/usr/bin/expect --
send_log "--start--\r"
spawn bash --norc
expect "\$"
send "echo test\r"
expect -re ".*\n"
log_file -noappend log
send_log "$expect_out(0,string)"
log_file
send_log "--end--\n"
exit
-
- 実行結果--
$ ./test.sh
spawn bash --norc
echo test
$cat log
$echo test
Tclsh を使い、ルータ上でスクリプトを実行する
Tclsh を使い、ルータ上でスクリプトを実行する
Cisco | 15:47
Tclsh を使うと Cisco ルータ上であってもスクリプトにより定型化した作業を実行出来るので便利です(もちろん、Tclsh の作り込みによって、それ以上のことも可能です)。詳しくは Cisco IOS Scripting with Tcl に記載があります。
Tcl シェルモードの起動
"tclsh" でシェルを起動、"tclquit" で終了します。ただし、"tclquit" を一度、実行しただけではシェルを終了できない場合が多々、あります??
Router# tclsh
Router(tcl)# tclquit
Router(tcl)# tclquit
Router#
連続した宛先に順次、Ping を実行するスクリプト
以下のスクリプトでは 10.0.0.1 〜 10.0.0.10 までに次々と、1 発ずつ Ping を実行します。
for {set i 1} {$i <= 10} {incr i} {ping ip 10.0.0.$i repeat 1}
実行例は以下の通りです。
Router# tclsh
Router(tcl)# for {set i 1} {$i <= 10} {incr i} {ping ip 10.0.0.$i repeat 1}
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.1, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 72/72/72 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.2, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 8/8/8 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.3, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 48/48/48 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.4, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 4/4/4 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.5, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 4/4/4 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.6, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 4/4/4 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.7, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 76/76/76 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.8, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 8/8/8 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.9, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 8/8/8 ms
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.10, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 8/8/8 ms
連続しない宛先に順次、Ping を実行するスクリプト
以下のスクリプトを使えば、連続しないアドレスに対して順次、Ping を実行することが出来ます。
foreach address {
10.0.0.1
172.16.0.1
192.168.0.1
} {puts [exec "ping $address repeat 1"]}
SNMP を使って MIB を取得する
Tclsh から SNMP を使って自分自身の MIB へアクセスすることも可能です。まず、SNMP のコミュニティを設定しておきます。ここでは "public" としました。
Router# configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#snmp-server community public RO
Router(config)# exit
Tclsh から "snmp_getid" で SysName や SysName などの一式を取得することが出来ます。
Router# tclsh
Router(tcl)# snmp_getid public
{
{
{
{
{
{
個別に OID を指定したい場合は "snmp_getbulk community-string non-repeaters max-repetitions oid [oid2 oid3...]" を使います。以下では CPU 負荷率を表す Cisco 拡張 MIB を取得しています。OID とその意味は、以下の通りです。
1 3 1.3.6.1.4.1.9.9.109.1.1.1.1.3
最後の 5 秒間の CPU の使用率
1 3 1.3.6.1.4.1.9.9.109.1.1.1.1.4
最後の 1 分間の CPU の使用率
1 3 1.3.6.1.4.1.9.9.109.1.1.1.1.5
最後の 5 分間の CPU の使用率
Router(tcl)# snmp_getbulk public 1 3 1.3.6.1.4.1.9.9.109.1.1.1.1.3
{
Router(tcl)# snmp_getbulk public 1 3 1.3.6.1.4.1.9.9.109.1.1.1.1.4
{
Router(tcl)# snmp_getbulk public 1 3 1.3.6.1.4.1.9.9.109.1.1.1.1.5
{
標準入力からスクリプトを作成する
"tclsh [スクリプト名]" のように指定すると、予め保存しておいたスクリプトを実行することが出来ます。ただし、IOS 上には(vi や ee のような)エディタが提供されていない為、毎回、TFTP サーバなり、外部からスクリプトをダウンロードしてくるのは不便です。こういった場合、下記のスクリプトを使うと IOS の標準入力からスクリプトを作成し、ローカルファイルシステム上に保存することが出来ます。
puts [open "ファイル名" w+] {
・
・
・
< -- ここにスクリプトを入力 -->
・
・
・
}
実行例は以下の通りです。実行前はファイルシステム上には何もありません。
Router# dir
Directory of disk0:/
No files in directory
66854912 bytes total (66854912 bytes free)
Tcl を起動し、スクリプトを入力します。
Router# tclsh
Router(tcl)# puts [open "disk0:HelloWorld.tcl" w+] {
- > puts "Hello, World!"
- > }
しかし、スクリプトの保存先が ATA フラッシュディスクの場合、(ファイルシステムの違いによるものなのか)スクリプトがすぐにはファイルシステム上に保存されないようでした(PCMCIA スロット上の Flash ディスクは、すぐに保存されていました)。
Router(tcl)# dir
Directory of disk0:/
1 -rw- 0 Feb 29 2008 23:08:30 +00:00 HelloWorld.tcl
66854912 bytes total (66854912 bytes free)
"exit" や "tclquit" すると、ファイルシステム上にスクリプトが書き込まれます。
Router(tcl)# exit
Router#dir
Directory of disk0:/
1 -rw- 27 Feb 29 2008 23:08:46 +00:00 HelloWorld.tcl
66854912 bytes total (66850816 bytes free)
実行してみます。
Router# tclsh HelloWorld.tcl
Hello, World!
作成するスクリプトには、必ずしも拡張子を付ける必要はありません。以下の例では拡張子を付与せず、スクリプトを保存&実行しています。
Router# tclsh
Router(tcl)#puts [open "flash:HelloTclsh" w+] {
- > puts "Hello, Tclsh!"
- >}
Router(tcl)# tclquit
Router# tclsh HelloTclsh
Hello, Tclsh!
コメントを書く
トラックバック - http://d.hatena.ne.jp/eco31/20091219/1261205259
リンク元
21 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja-JP-mac:official&q=mac+snow+leopard+64bit+macbook&btnG=検索&lr=lang_ja&aq=f&oq=
11 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GZEZ_jaJP310JP311&q=cisco+ios+15.0
9 http://www.google.co.jp/search?hl=ja&lr=lang_ja&client=firefox-a&rls=org.mozilla:ja:official&hs=y0B&q=Cisco ケーブル&start=10&sa=N
7 http://www.google.co.jp/search?hl=ja&safe=off&client=firefox-a&rls=org.mozilla:ja:official&hs=8Tv&q=pydev&btnG=検索&lr=lang_ja&aq=f&oq=
7 http://www.google.com/search?client=safari&rls=en&q=snow+leopard+64bit+設定&ie=UTF-8&oe=UTF-8
6 http://www.google.co.jp/search?hl=ja&client=firefox-a&channel=s&rls=org.mozilla:ja:official&hs=MmE&q=スクリーンショットをとる PNG&btnG=検索&lr=&a
6 http://www.google.com/search?hl=ja&safe=off&client=safari&rls=en&q=leopard+tftpさーば &btnG=検索&lr=&aq=&oq=
5 http://twitturls.com/
4 http://d.hatena.ne.jp/secondlife/20080218/1203303528
4 http://iphone-appli.info/data/6152
簡易port scan
#!/usr/local/bin/bash
count=$(ping -c 1 $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ "$count" = "0" ]; then
echo "$1 PING [FAILED]"
else
echo "$1 PING [OK]"
fi
apache=`echo " " | telnet $1 80 | grep Connected | awk '{ print $1 }'`
if [ "$apache" = "Connected" ]; then
echo "$1 PORT 80 [OK]"
else
echo "$1 PORT 80 [FAILED]"
fi
ssh=`echo " " | telnet $1 22 | grep Connected | awk '{ print $1 }'`
if [ "$ssh" = "Connected" ]; then
echo "$1 PORT 22 [OK]"
else
echo "$1 PORT 22 [FAILED]"
fi
smtp=`echo " " | telnet $1 25 | grep Connected | awk '{ print $1 }'`
if [ "$smtp" = "Connected" ]; then
echo "$1 PORT 25 [OK]"
else
echo "$1 PORT 25 [FAILED]"
fi
pop=`echo " " | telnet $1 110 | grep Connected | awk '{ print $1 }'`
if [ "$pop" = "Connected" ]; then
echo "$1 PORT 110 [OK]"
else
echo "$1 PORT 110 [FAILED]"
fi