-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodename.py
executable file
·50 lines (44 loc) · 1.19 KB
/
nodename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python
#
# Accepts a MAC or IP address as an argument and tries to find a name that is mapped to that
# MAC or IP on the current router.
#
# Uses MACnames -j and IPnames -j and dig as needed:
import re
import os
import sys
import subprocess
import socket
import json
devnull = open(os.devnull, 'w')
def isMAC(address):
return re.match("[0-9A-F]{2}([-:])[0-9A-F]{2}(\\1[0-9A-F]{2}){4}$", address.upper())
def isIP(address):
try:
socket.inet_aton(address)
return True
except socket.error:
if socket.has_ipv6:
try:
socket.inet_pton(socket.AF_INET6, address)
return True
except:
return False
else:
return False
self = sys.argv[0]
address = sys.argv[1].upper() if len(sys.argv) > 1 else None
if isMAC(address):
names = json.loads(subprocess.check_output(["MACnames", "-j"], stderr=devnull))
if address in names:
print names[address]
elif isIP(address):
names = json.loads(subprocess.check_output(["IPnames", "-j"], stderr=devnull))
if address in names:
print names[address]
else:
name = subprocess.check_output(["dig", "+short", "-x", address], stderr=devnull).replace(".\n","\n").strip()
print name
else:
print "Must specify a MAC or IP address"
sys.exit(1)