用来扫描一些网段的ip和端口的存活
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'xiaojinsong'
# __time__ = '2018/2/28'
import sys
from nmap import PortScanner
from nmap import PortScannerError
class NmapScan():
def __init__(self, scan_hosts, scan_port):
self.scan_Hosts = scan_hosts
self.scan_Port = scan_port
self.survivedict = {}
self.deaddict = {}
def program_scan(self):
try:
nm = PortScanner()
except PortScannerError:
print('Nmap not found,', sys.exc_info()[0])
sys.exit(0)
except:
print("Unexcept error:", sys.exc_info()[0])
sys.exit(0)
for scan_host in self.scan_Hosts:
try:
nm.scan(scan_host, arguments='-v -sS -p' + self.scan_Port)
except Exception as e:
print("Scan Error: " + str(e))
survive = []
deadip = []
for host in nm.all_hosts():
if nm[host].state() == 'up':
print('-----------------------------------------------------')
print('Host : %s (%s)' % (host, nm[host].hostname()))
print('State : %s' % nm[host].state())
survive.append(host)
elif nm[host].state() == 'down':
deadip.append(host)
for proto in nm[host].all_protocols():
print('----------------------------')
print('Protocol : %s ' % proto )
lport = nm[host][proto].keys()
lport = sorted(lport)
for port in lport:
print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
self.deaddict[scan_host] = deadip
self.survivedict[scan_host] = survive
if __name__ == '__main__':
scan_hosts = ['10.200.18.0/24', '10.200.13.0/24', '10.200.19.0/24', '10.200.15.0/24']
scan_port = '21,22,23,80,8080,1521,3306'
nmapS = NmapScan(scan_hosts, scan_port)
nmapS.program_scan()
for key,value in nmapS.survivedict.items():
print(key, value)