PFNA- Pinging bulk devices using python

Published: 30 November 2018
on channel: Srihari Yenugukonda
3,386
58

Pinging a bunch of devices in a go is a regular task every network or system admin comes across. Here is a python script to automate that

Script
===================
import subprocess

def ping_ip(ip):
(output, error) = subprocess.Popen((['ping', ip, '-c', '2']), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
#for windows use -n instead of -c after ping, ip.
if b'bytes from' in output:
return "UP"
elif b'Destination Host Unreachable' in output:
return "DOWN"
elif error:
return "DNS Error"
else:
return "UNKNOWN"

l = ''
with open('devices.txt') as f:
for ip in f:
ip = ip.strip('\n')
response = ping_ip(ip)
result = ('%s,%s \n' % (ip, response))
l = l+result
file = open('output.txt', 'w')
file.write(l)
file.close()


On this page of the site you can watch the video online PFNA- Pinging bulk devices using python with a duration of hours minute second in good quality, which was uploaded by the user Srihari Yenugukonda 30 November 2018, share the link with friends and acquaintances, this video has already been watched 3,386 times on youtube and it was liked by 58 viewers. Enjoy your viewing!