Where is the problem?
import nmap
I installed nmap and python, and when I use import nmap there is no any problem. But when us
Note about nmap
I used nmap to search the mask 192.168.1.0/24, but it didnt seam to find ALL ip´s. Eg: my laptop on 192.168.1.119 wasnt found, so I ended up using a combination of:
def ping(self, ip):
# Use the system ping command with count of 1 and wait time of 1.
ret = subprocess.call(['ping', '-c', '1', '-W', '1', ip],
stdout=open('/dev/null', 'w'),
stderr=open('/dev/null', 'w'))
return ret == 0 # Return True if our ping command succeeds
inside a multithreaded Pinger
Pinger I got from: http://blog.boa.nu/2012/10/python-threading-example-creating-pingerpy.html
I created my own IpInfo class to store information and search for open ports on each IP, and here I use nmap: (Code is "work in progress", but you will get the idea. Ideas to tune performance would be nice)
class IpInfo(object):
ip = None
hostname = None
ports = []
lastSeenAt = strftime("%Y-%m-%d %H:%M:%S", gmtime())
def findHostName(self):
if(ip):
self.hostname = str(socket.gethostbyaddr(ip)[0])
else:
raise NameError('IP missing')
def findOpenPorts(self):
print('findOpenPorts')
nm = nmap.PortScanner()
nm.scan(host)
nm.command_line()
nm.scaninfo()
for proto in nm[self.ip].all_protocols():
print('----------')
print('Protocol : %s' % proto)
lport = nm[self.ip][proto].keys() #<------ This 'proto' was changed from the [proto] to the ['tcp'].
lport.sort()
for port in lport:
if(nm[self.ip][proto][port]['state'] == 'open'):
self.ports.append(port)