2

I am using python2.7 and trying to scan my network and print the host names of PCs on my n/w , i looked up on official docs of nmap , i tried but its not working i don't know why . Kindly guide me through the process or tell me where i am going wrong . Thanks in advance :)

hosts_list = [(x, nm[x]['status']['state'],nm[x].hostname()) for x in nm.all_hosts()]
   for host, status,name in hosts_list:
      print('{0}:{1}:{2}'.format(host, status,name))
vivkv
  • 931
  • 2
  • 14
  • 29

2 Answers2

0

From the first line of your link:

This is a python class to use nmap and access scan results from python3

And you said that you're using 2.7... Check that, may be your problem.

inigoD
  • 1,681
  • 14
  • 26
  • I tried libnmap but it is only able to show my local computer's name as localhost it doesnn't shows names of other PCs on my n/w . what should i do now ? any suggestions ? – vivkv Dec 16 '15 at 16:47
0

you can use sacket to get host name

import socket
import nmap

nm = nmap.PortScanner()
nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389')

hosts_list = [(x, nm[x]['status']['state'],socket.gethostbyaddr(x)[0]) for x in nm.all_hosts() if socket.gethostbyaddr(x)[0]]
for host, status,name in hosts_list:
  print('{0}:{1}:{2}'.format(host, status,name))

output:

192.168.1.1:up:TP-LINK
192.168.1.102:up:android-XXX
192.168.1.105:up:DESKTOP-XXX
192.168.1.107:up:android-XXX
Sakhri Houssem
  • 975
  • 2
  • 16
  • 32