Python script to ping ip in a subnet

from commands import getoutput
from multiprocessing import Process
from sys import exit

class PING_SWEEP(object):

def pinger(self, host_num):
 """thread pinger function"""
 hostadrr = host.split('.')[:-1]#removes . from user input
 hostadrr = '.'.join(hostadrr) + '.' + repr(host_num) # adds . and last octet in the input
 line = getoutput("ping -n -c 2 %s 2> /dev/null" % hostadrr) # tries to ping to the ip address
 not_alive_host = []
 alive_host = []

if line.find(hostadrr) and line.find("bytes from") > -1: # Host Active

if line.find(hostadrr) and line.find("Unreachable") > -1: # No response from host

for x in range(len(not_alive_host)):
 print not_alive_host[x]

def ping_sweeper(self):
 for host_num in range(1, 254):
 ping = Process(target=self.pinger, args=(host_num,))

if __name__ == '__main__':
 host=raw_input("Enter subnet you want to ping [valid input is]: ")
 except KeyboardInterrupt:

This entry was posted in Python. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s