IP scanning in python

In order to find the IPs using python, we are going to install nmap module. You can installing it, by typing in the terminal the following command : sudo apt-get install python-nmap


ip_scan.py

import nmap

nm = nmap.PortScanner() 
cidr2 ='192.168.1.99/24'
a = nm.scan(hosts=cidr2, arguments='-sP') 

for k,v in a['scan'].iteritems(): 
    if str(v['status']['state']) == 'up':
        print str(v)
        try:    print str(v['addresses']['ipv4']) + ' => ' + str(v['addresses']['mac'])
        except: print str(v['addresses']['ipv4'])

Output:
{'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': '', 'addresses': {u'ipv4': u'192.168.1.7'}}
192.168.1.7
{'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': '', 'addresses': {u'ipv4': u'192.168.1.6'}}
192.168.1.6
{'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': '', 'addresses': {u'ipv4': u'192.168.1.9'}}
192.168.1.9
{'status': {'state': u'up', 'reason': u'syn-ack'}, 'hostname': '', 'addresses': {u'ipv4': u'192.168.1.1'}}
192.168.1.1
{'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': '', 'addresses': {u'ipv4': u'192.168.1.8'}}
192.168.1.8

Comments

Popular posts from this blog

Vertex Cover (python implementation)

Transportation Problem (Vogel algorithm python)