In this session of A Few Easy Steps, we will build a multi-device ping tool in Python.
Session Prerequisites:
- You must have Python 2.7 installed on the host your are running this from.
- You need at least two host to test against
Our goals for this session are:
- Build a quick ping testing tool with Python
Lets Get Started:
Open your text editor.
Copy the following code snippet into the text editor:
# Import required libraries
import os
#
# Open Text file with host list
with open('device_file1.txt') as fname:
# Set Variable Switches to read each line int file we opened
switches = fname.read().splitlines()
# Create a for loop named switch that iterates throug the lines in switches
for switch in switches:
# Run a ping against the switch loop with 1 ping each
response = os.system("ping -c 1 " + switch)
#
#and then check the response and print is up or is down
if response == 0:
print switch, 'is up!'
else:
print switch, 'is down!'
Save the files as ping.py
Create a file called device_file1.txt
Input the hosts you want to ping as follows:
10.0.0.2
10.0.0.6
10.0.0.10
10.0.0.14
10.0.0.18
10.0.0.22
10.0.0.26
10.0.0.30
10.0.0.34
10.0.0.38
Save device_file1.txt to the same directory as ping.py
Open your terminal application
Navigate to the directory that ping.py and device_file1.txt are located
run this command
python ping.py
Your output should look something like this:
PING 10.0.0.2 (10.0.0.2): 56 data bytes
64 bytes from 10.0.0.2: icmp_seq=0 ttl=255 time=23.609 ms
— 10.0.0.2 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 23.609/23.609/23.609/0.000 ms
10.0.0.2 is up!
PING 10.0.0.6 (10.0.0.6): 56 data bytes
64 bytes from 10.0.0.6: icmp_seq=0 ttl=255 time=19.619 ms
— 10.0.0.6 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 19.619/19.619/19.619/0.000 ms
10.0.0.6 is up!
PING 10.0.0.10 (10.0.0.10): 56 data bytes
64 bytes from 10.0.0.10: icmp_seq=0 ttl=255 time=25.017 ms
— 10.0.0.10 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 25.017/25.017/25.017/0.000 ms
10.0.0.10 is up!
PING 10.0.0.14 (10.0.0.14): 56 data bytes
64 bytes from 10.0.0.14: icmp_seq=0 ttl=255 time=23.815 ms
— 10.0.0.14 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 23.815/23.815/23.815/0.000 ms
10.0.0.14 is up!
PING 10.0.0.18 (10.0.0.18): 56 data bytes
64 bytes from 10.0.0.18: icmp_seq=0 ttl=255 time=18.658 ms
— 10.0.0.18 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 18.658/18.658/18.658/0.000 ms
10.0.0.18 is up!
PING 10.0.0.22 (10.0.0.22): 56 data bytes
64 bytes from 10.0.0.22: icmp_seq=0 ttl=255 time=16.355 ms
— 10.0.0.22 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 16.355/16.355/16.355/0.000 ms
10.0.0.22 is up!
PING 10.0.0.26 (10.0.0.26): 56 data bytes
64 bytes from 10.0.0.26: icmp_seq=0 ttl=255 time=26.548 ms
— 10.0.0.26 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 26.548/26.548/26.548/0.000 ms
10.0.0.26 is up!
PING 10.0.0.30 (10.0.0.30): 56 data bytes
64 bytes from 10.0.0.30: icmp_seq=0 ttl=255 time=16.895 ms
— 10.0.0.30 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 16.895/16.895/16.895/0.000 ms
10.0.0.30 is up!
PING 10.0.0.34 (10.0.0.34): 56 data bytes
64 bytes from 10.0.0.34: icmp_seq=0 ttl=255 time=27.015 ms
— 10.0.0.34 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 27.015/27.015/27.015/0.000 ms
10.0.0.34 is up!
PING 10.0.0.38 (10.0.0.38): 56 data bytes
64 bytes from 10.0.0.38: icmp_seq=0 ttl=255 time=41.792 ms
— 10.0.0.38 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 41.792/41.792/41.792/0.000 ms
10.0.0.38 is up!
Download the files from this post at My GitHub Repository .
3 comments