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
 alive_host.append(hostadrr)
 

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

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,))
 ping.start()

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

Advertisements
This entry was posted in Python. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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