Tclsh を使い、ルータ上でスクリプトを実行する
Tclsh を使うと Cisco ルータ上であってもスクリプトにより定型化した作業を実行出来るので便利です(もちろん、Tclsh の作り込みによって、それ以上のことも可能です)。詳しくは Cisco IOS Scripting with Tcl に記載があります。
Tcl シェルモードの起動¶
"tclsh" でシェルを起動、"tclquit" で終了します。ただし、"tclquit" を一度、実行しただけではシェルを終了できない場合が多々、あります??
連続した宛先に順次、Ping を実行するスクリプト¶
以下のスクリプトでは 10.0.0.1 〜 10.0.0.10 までに次々と、1 発ずつ Ping を実行します。
実行例は以下の通りです。
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 を実行することが出来ます。
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 などの一式を取得することが出来ます。
個別に 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 の標準入力からスクリプトを作成し、ローカルファイルシステム上に保存することが出来ます。
実行例は以下の通りです。実行前はファイルシステム上には何もありません。
Tcl を起動し、スクリプトを入力します。 しかし、スクリプトの保存先が 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)
実行してみます。
作成するスクリプトには、必ずしも拡張子を付ける必要はありません。以下の例では拡張子を付与せず、スクリプトを保存&実行しています。