-
Notifications
You must be signed in to change notification settings - Fork 5
/
dhcp_debug.py
executable file
·98 lines (83 loc) · 2.46 KB
/
dhcp_debug.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
#!/usr/bin/python
import datetime
def parse_leases(lease_file):
leases = {}
lease = {}
in_lease = False
for line in lease_file:
if line.lstrip().startswith('#'):
continue
tokens = line.split()
if len(tokens) == 0:
continue
key = tokens[0].lower()
if key == 'lease':
if in_lease:
raise Exception('Parsing error')
else:
in_lease = True
lease = {'id': None, 'ip' : tokens[1], 'count': 1, 'client-hostname': ''}
elif key == 'hardware':
if not in_lease:
raise Exception('Parsing error')
else:
in_lease = True
lease['id'] = tokens[2].replace(';','')
elif key == 'uid' or key == 'client-hostname':
if not in_lease:
raise Exception('Parsing error')
else:
lease[key] = tokens[1].replace(';','')
elif key == '}':
if in_lease:
if lease['id'] not in leases:
leases[lease['id']] = lease
else:
leases[lease['id']]['count'] += 1
leases[lease['id']]['ip'] = lease['ip']
if leases[lease['id']]['client-hostname'] == '':
leases[lease['id']]['client-hostname'] = lease['client-hostname']
lease = {}
in_lease = False
return leases
def count_active_leases(all_leases, now):
count = 0
for lease in all_leases.itervalues():
if timestamp_inbetween(now, lease['starts'], lease['ends']):
count += 1
return count
def print_unique_leases(all_leases, gwname):
print "------------"+gwname+"------------"
#for lease in all_leases.itervalues():
# print "ID:" + lease['id'] + ', COUNT: ' + str(lease['count'])
i = 0
for lease in sorted(all_leases.items(),key=lambda x: x[1]['count'],reverse=True):
lease = lease[1]
#print lease
print "ID:" + lease['id'] + ', COUNT: ' + str(lease['count']) + ', IP: '+lease['ip']+', HOSTNAME: ' + lease['client-hostname']
i += 1
if i > 5:
break
def timestamp_inbetween(now, start, end):
return start < now < end
lease_files = [
{
'file' : 'dhcp_parad0x.leases',
'name' : 'parad0x'
},
{
'file' : 'dhcp_c1024.leases',
'name' : 'c1024'
},
{
'file' : 'dhcp_fussel.leases',
'name' : 'fussel'
},
{
'file' : 'dhcp_descilla.leases',
'name' : 'des2'
}
]
for file in lease_files:
print_unique_leases(parse_leases(open(file['file'], 'r')), file['name'])
# scp [email protected]:/var/lib/dhcp/dhcpd.leases dhcp_c1024.leases && scp [email protected]:/var/lib/dhcp/dhcpd.leases dhcp_parad0x.leases && scp [email protected]:/var/lib/dhcp/dhcpd.leases dhcp_fussel.leases && scp [email protected]:/var/lib/dhcp/dhcpd.leases dhcp_descilla.leases