Search code examples
pythonnmapport-scanning

NMAP Service Information


With this code I can get the hostname, used protocols, ports and states of the ports. How can I also get the service information?

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 = list(nm[host][proto].keys())
        lport.sort()
        for port in lport:
            print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
            print('----------')

Solution

  • Okay guys, I know how its done now! Since we use NMAP, nmap can tell us what service is running on what port. You can extract the information with something like that for example:

    print('port : %s\tservice : %s' % (port, nm[host][proto][port]['name']))