-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.py
36 lines (28 loc) · 932 Bytes
/
utils.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
from subprocess import check_output
def get_mac_from_request(request):
try:
mac = get_mac_from_ip(get_ip_from_request(request))
return mac
except Exception, e:
print e
return None
def get_ip_from_request(request):
return request.remote_addr
def get_mac_from_ip(ip):
mac = get_mac_from_arp_cache(ip)
if mac:
return mac
else:
PING_COMMAND = 'ping -c 1 -W 1 ' + str(ip)
check_output(PING_COMMAND.split(" "))
return get_mac_from_arp_cache(ip)
def get_mac_from_arp_cache(ip):
ARP_CACHE_COMMAND = 'arp -a -n'
INCOMPLETE_ENTRY = '<incomplete>'
arp_entries = check_output(ARP_CACHE_COMMAND.split(" ")).split("\n")
for arp_entry in arp_entries:
if ip in arp_entry:
if INCOMPLETE_ENTRY in arp_entry:
return None
else:
return arp_entry.split(" at ")[1].split(" ")[0]