Automating Ping to Multiple Hosts

PicOS switches support ping, which may be used to test connectivity to remote IP addresses. Users often want to test connectivity to all subnets in their network. This can be accomplished manually by pinging IP addresses in all subnets one-by-one, but that method is error-prone and tedious.

This section describes how to write a simple re-usable script to ping a number of IP addresses at once. This script is especially useful when troubleshooting connectivity in a network, and user needs to ping a number of IP addresses again and again for verification.

User can create the script once and use it again and again from the PicOS L2/L3 operation mode.

This requires a text editor to create the script and save it as a file on user's PicOS switch. PicOS includes the vi text editor, which can be run from the Linux shell on user's PicOS switch. We choose to call our script pingAll.sh though user may choose any other name. The .sh file extension is not mandatory, though we recommend using it to make it obvious to anyone that the file is a shell script.

admin@Leaf-1$vi pingAll.sh

Inside the vi editor, press i to be able to insert text. Paste the following lines of text (after modifying them for user's network):

ip[0]='192.168.42.2'
ip[1]='192.168.42.4'
ip[2]='192.168.42.5'
ip[3]='192.168.42.9'
ip[4]='192.168.42.20'
ip[5]='192.168.42.40'
ip[6]='192.168.42.60'
ip[7]='192.168.42.100'
ip[8]='192.168.42.110'
ip[9]='192.168.42.120'
ip[10]='192.168.42.130'
ip[11]='192.168.42.240'
ip[12]='192.168.42.22'

for ((i=0; i <=12; i++))
do
   ping -c 3 ${ip[$i]}
done

Press Esc and then enter :wq to save the file and exit the vi editor.

Some information about the script follows. The ip[] array has thirteen elements (ip[0]ip[12]) and each element holds an IP address. User can change both the IP addresses and the number of array elements. The script will send three ping requests to each IP address in the ip[] array, one by one. If user is familiar with shell scripting or programming in C-like languages, the script should be self-descriptive. Even if user is an absolute beginner to programming and scripting, user should be able to modify and use the script after some research.  

List the contents of user's home directory.

admin@Leaf-1$ls
pingAll.sh

Make the new file pingAll.sh executable.

admin@Leaf-1$chmod +x pingAll.sh

Enter the PicOS L2/L3 operation mode.

admin@Leaf-1$cli
Synchronizing configuration...OK.
Pica8 PicOS Version 2.6
Welcome to PicOS L2/L3 on Leaf-1
admin@Leaf-1>

Run the script from PicOS L2/L3 operation mode.

admin@Leaf-1> bash /home/admin/pingAll.sh
PING 192.168.42.2 (192.168.42.2) 56(84) bytes of data.
64 bytes from 192.168.42.2: icmp_req=1 ttl=64 time=4.66 ms
64 bytes from 192.168.42.2: icmp_req=2 ttl=64 time=0.848 ms
64 bytes from 192.168.42.2: icmp_req=3 ttl=64 time=0.910 ms

--- 192.168.42.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.848/2.142/4.669/1.787 ms
PING 192.168.42.4 (192.168.42.4) 56(84) bytes of data.
64 bytes from 192.168.42.4: icmp_req=1 ttl=64 time=8.27 ms
64 bytes from 192.168.42.4: icmp_req=2 ttl=64 time=1.98 ms
64 bytes from 192.168.42.4: icmp_req=3 ttl=64 time=2.94 ms

--- 192.168.42.4 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 1.986/4.401/8.273/2.765 ms
PING 192.168.42.5 (192.168.42.5) 56(84) bytes of data.
64 bytes from 192.168.42.5: icmp_req=1 ttl=64 time=6.59 ms
64 bytes from 192.168.42.5: icmp_req=2 ttl=64 time=3.22 ms
64 bytes from 192.168.42.5: icmp_req=3 ttl=64 time=1.81 ms

--- 192.168.42.5 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 1.812/3.876/6.594/2.006 ms

<Some output omitted>

Copyright © 2024 Pica8 Inc. All Rights Reserved.