Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import nmap
- from socket import *
- nm = nmap.PortScanner()
- def scanNet(networkAddress, networkBits, numberPortsToScan):
- target = ('{0}/{1}').format(networkAddress, networkBits)
- settings = ('-T4 --top-ports {0}').format(numberPortsToScan)
- nm.scan(hosts=target, arguments=settings)
- def getLiveHosts(index=None):
- hostList = []
- for host in nm.all_hosts():
- hostList.append({'ip': host, 'hostname': nm[host].hostname()})
- if isinstance(index, int):
- return hostList[index]
- else:
- return hostList
- def getHostPorts(host):
- openPorts = []
- for proto in nm[host].all_protocols():
- for openPort in nm[host][proto]:
- if (nm[host][proto][openPort]['state'] == 'open'):
- openPorts.append(openPort)
- return openPorts
- def captureBanner(host, port)
- conn = socket(AF_INET, SOCK_STREAM)
- conn.connect((target, port))
- if port == 80 || port == 8080 || port == 8000:
- conn.send('GET HTTP/1.1 \r\n')
- else:
- conn.send('Hello, is it me you\'re looking for? \r\n')
- banner = conn.recv(1024)
- return str(banner)
- def printHostInfo(hostId):
- host = getLiveHosts(hostId):
- print("{0} | {1}".format(liveHost['ip'], liveHost['hostname']))
- for port in getHostPorts(liveHost['ip']):
- print("\tOpen Port: " + port)
- print("\t\t" + captureBanner(liveHost['ip'], port))
- print("--- --- --- --- --- ---")
- def printHostsInfo(hostIndex = None):
- if isinstance(hostIndex, int):
- hostList =getLiveHosts(hostIndex);
- else:
- hostList = getLiveHosts()
- for host in hostList:
- print("{0} | {1}".format(host['ip'], host['hostname']))
- for port in getHostPorts(host['ip']):
- print("\tOpen Port: " + port)
- print("\t\t" + captureBanner(host['ip'], port))
- print("--- --- --- --- --- ---")
- print("---------------------------\n\n")
- scanNet('192.168.1.200', 32, 5)
- printHostsInfo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement