Part 1 of this tutorial showed how to ping multiple IP addresses using TCL script. This tutorial will further refine the script which was in Part 1. Instead of foreach loop, we will be using for loop to make the script shorter.

TCL Script

Here is the sample script

for { set i 1 } { $i <= 10 } { incr i } {
ping 192.168.1.$i re 2
}


When the loop is run for the first time, the variable i=1. The value of i will be incremented until i=10, then the loop will exit.
The above script will ping IP addresses 192.168.1.1 through 192.168.1.10

Executing the Script

Router#tclsh
Router(tcl)#for { set i 1 } { $i <= 10 } { incr i } {
+>(tcl)#ping 192.168.1.$i re 2
+>(tcl)#}

Sample output

Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.1, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 4/4/4 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 4/4/4 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.3, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/1/1 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.4, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.5, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/1/1 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.6, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/1/1 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.7, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 4/4/4 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.8, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.9, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/1/1 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.10, timeout is 2 seconds:
!!

That's it for this tutorial. The variable can also be tweaked to ping the whole subnet.