python-nmap打印扫描重复

时间:2019-07-26 03:16:32

标签: python nmap

我是python的新手,正在学习如何用Python编写nmap程序。在大多数情况下,一切都很好,但是每次我选择选项2扫描网络状态时,一切都会好起来;我看到结果打印了4-5次。是我做错了什么

import nmap

nm = nmap.PortScanner()

print("Welcome to Python-nmap program. Have fun")
print("<-------------------------------------->")

ip_addr = input('Please enter target: ')
nm.scan(ip_addr, '20-23,80,443')

selection = input("""Please select option below
                 1) Common port scan such as 21, 22, 80, 443
                 2) Check active IP
                 3) Scan whole network for active ports and IP
""""")

print('Please select: ', selection)

if selection == '1':
    for hosts in nm.all_hosts():
        print('Nmap Version', nm.nmap_version())
        print('Host: %s(%s)' % (hosts, nm[hosts].hostname()))
        print('State: %s' % nm[hosts].state())
        for proto in nm[hosts].all_protocols():
            print('Protocol : %s' % proto)

            lport = nm[hosts][proto].keys()
            for port in lport:
                print('port: %s\state : %s' % (port, nm[hosts][proto][port]['state']))

if selection == '2':
    for hosts in nm.all_hosts():
        host_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
        for host, status in host_list:
            print(host, status, "\n Nmap Version", nm.nmap_version())

0 个答案:

没有答案