1

I am trying to display all the connected machine names using ip address, I could get the IP address by checking the connection

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((addr,80))

I have tried using s.getsockname,socket.gethostname and s.getpeername these all are returning similiar results

What should I do if I need to display the names? For example

192.168.1.1     - 192.168.1.1
192.168.1.50    - 192.168.1.50
192.168.1.113   - 192.168.1.113
192.168.1.114   - 192.168.1.114
192.168.1.139   - 192.168.1.139

I need to display this like

192.168.1.1     - tom123
192.168.1.50    - allec
192.168.1.113   - john-pc
192.168.1.114   - bob
192.168.1.139   - anny

I have tried with socket.gethostbyaddr("196.168.1.114") -- it is giving me an exception saying

print socket.gethostbyaddr("196.168.1.114")

socket.herror: [Errno 1] Unknown host

John
  • 666
  • 1
  • 9
  • 22
  • By the way, are you running this in client-side or server-side? That may make a difference –  Dec 27 '14 at 11:13
  • @PeterWood I think it isn't. In that case they are getting the hostname of a server. In this case he is working on a LAN. –  Dec 27 '14 at 11:17
  • Python lookup hostname from IP with 1 second timeout -- I have tried it already it is blowing up with an error message "Unknown host name" – John Dec 27 '14 at 11:22
  • you could try nmap http://pastebin.com/2XUmEaj3 – Padraic Cunningham Dec 27 '14 at 11:56
  • socket.herror: [Errno 1] Unknown host This is the error which i am getting after using socket.gethostbyaddr("196.168.1.114") – John Dec 28 '14 at 04:42

1 Answers1

0

I do not know if this will help, but socket.getfqdn(IP_ADDRESS) returns the hostname.

  • I have tried with this getfqdn will only print the same host ip which you are passing , it will not work , Thanks for the response – John Dec 27 '14 at 11:10
  • @user3247425 why don't you post your full code? There might be something there. Or is it all there? –  Dec 27 '14 at 11:11
  • - But here i am already getting the ipaddress which are connected -- My remaining code is all for getting those connected ips (No issues with that) - from ip address getting name is something which i need to right new – John Dec 27 '14 at 11:16
  • If you cannot resolve it using `getfqdn`, your system simply cannot resolve it using /etc/hosts or DNS. – ThiefMaster Dec 27 '14 at 11:25
  • @ThiefMaster I don't get your point. I just suggested a way that I knew and I might have worked for him. Unfortunately it didn't. –  Dec 27 '14 at 11:26
  • My point was explaining the OP what's the reason if your code doesn't work for him... – ThiefMaster Dec 27 '14 at 11:27
  • @ThiefMaster ah all right. Thanks then for your help –  Dec 27 '14 at 11:27