Skip to content

Instantly share code, notes, and snippets.

@d1b
Created July 25, 2011 18:03
Show Gist options
  • Save d1b/1104732 to your computer and use it in GitHub Desktop.
Save d1b/1104732 to your computer and use it in GitHub Desktop.
import socket
f = open('/proc/net/arp', 'r')
data = f.readlines()
#print data
f.close()
def get_hostname(ip):
try:
return socket.gethostbyaddr(ip)[0]
except socket.herror, e:
#print ip, repr(e),
return None
for line in data[1:]:
ipaddr = line.split(" ")[0]
mac = line.index(":")
if mac < 0:
print ipaddr, "unknown..."
continue
end_mac = line[mac:].index(" ") + mac
hostname = get_hostname(ipaddr)
print (hostname, ipaddr), line[mac-2: end_mac]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment