这次我们使用python-nmap包进行计算机端口扫描,有liunux基础的同学一定知道nmap的强大,不废话,上代码:
# coding=utf8
import nmap
import sys
def main():
scan_row = []
input_data = raw_input('Please input hosts and port: ')
scan_row = input_data.split(" ")
if len(scan_row) != 2:
print(" Input error. Please input again.")
sys.exit(0)
hosts = scan_row[0]
port = scan_row[1]
try:
nm = nmap.PortScanner()
except nmap.PortScannerError as e:
print('Nmap not found. e: %s' % e)
sys.exit(0)
except Exception as e:
print('Unexpected error. e:%s' % e)
sys.exit(0)
try:
nm.scan(hosts=hosts, arguments=' -v -sS -p ' + port)
except Exception as e:
print('Scan error.')
for host in nm.all_hosts():
print('---------------------------------------------------------')
print('Host : %s (%s)' % (host, nm[host].hostname()))
print('State : %s' % nm[host].state())
for proto in nm[host].all_protocols():
print('-------------')
print('Protocol : %s' % proto)
lport = nm[host][proto].keys()
lport.sort()
for port in lport:
print('Port : %s/state :%s ' % (port, nm[host][proto][port]['state']))
if __name__ == '__main__':
main()
这里我们pip 安装python-namp包,具体的详细方法请参考官网源码包中实例。