-
Notifications
You must be signed in to change notification settings - Fork 1
/
MACnames.py
executable file
·107 lines (88 loc) · 2.3 KB
/
MACnames.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
#
# Prints a list of MACs to Names.
# Prints a JSON dict if -j specified
#
# Builds list by checking
# Active DHCP leases
# DHCP Configuration
# Majordomo Configuration
#
# TODO: implement argparse
# TODO: implement -H option to add headers.
import os
import sys
import subprocess
import json
DHCP_LEASES = "/tmp/dhcp.leases"
devnull = open(os.devnull, 'w')
self = sys.argv[0]
option = sys.argv[1] if len(sys.argv) > 1 else None
def get_DHCP_leases():
leases = {}
with open(DHCP_LEASES) as lease_file:
for line in lease_file:
fields = line.split()
MAC = fields[1].upper()
name = fields[3]
leases[MAC] = name
return leases
def has_DHCP_conf(i):
return 0==subprocess.call(["uci", "get", "dhcp.@host[%d]" % i], stdout=devnull, stderr=devnull)
def get_DHCP_conf(i):
try:
name = subprocess.check_output(["uci", "get", "dhcp.@host[%d].name" % i], stderr=devnull).strip()
except:
name = None
try:
MAC = subprocess.check_output(["uci", "get", "dhcp.@host[%d].mac" % i], stderr=devnull).strip().upper()
except:
MAC = None
return (name, MAC)
def get_DHCP_confs():
conf = {}
i=0
while has_DHCP_conf(i):
(name, MAC) = get_DHCP_conf(i)
if not MAC is None:
conf[MAC] = name
i+=1
return conf
def has_MajorDomo_conf(i):
return 0==subprocess.call(["uci", "get", "majordomo.@static_name[%d]" % i], stdout=devnull, stderr=devnull)
def get_MajorDomo_conf(i):
try:
name = subprocess.check_output(["uci", "get", "majordomo.@static_name[%d].name" % i], stderr=devnull).strip()
except:
name = None
try:
MAC = subprocess.check_output(["uci", "get", "majordomo.@static_name[%d].mac" % i], stderr=devnull).strip().upper()
except:
MAC = None
return (name, MAC)
def get_MajorDomo_confs():
conf = {}
i=0
while has_MajorDomo_conf(i):
(name, MAC) = get_MajorDomo_conf(i)
if not MAC is None:
conf[MAC] = name
i+=1
return conf
# Start with the known DHCP leases
result = get_DHCP_leases()
# Augment with any DHCP conf info
conf = get_DHCP_confs()
for MAC in conf:
if not MAC in result:
result[MAC] = conf[MAC]
# Augment with any MajorDomo conf info
conf = get_MajorDomo_confs()
for MAC in conf:
if not MAC in result:
result[MAC] = conf[MAC]
if option == "-j":
print json.dumps(result)
else:
for MAC in sorted(result.iterkeys()):
print MAC, result[MAC]