This repository was archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmac2hostname.py
More file actions
executable file
·67 lines (54 loc) · 2.39 KB
/
mac2hostname.py
File metadata and controls
executable file
·67 lines (54 loc) · 2.39 KB
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
#!/usr/bin/env python
"""API to map MAC addresses to hostnames"""
from contextlib import contextmanager, closing
from subprocess import Popen, PIPE
from sqlite3 import connect
from bottle import route, run, request
from json import dumps
import re
__author__ = "Enrico Bacis"
__email__ = "enrico.bacis@gmail.com"
@contextmanager
def getcursor(db='mac2hostname.db'):
with connect(db) as connection:
with closing(connection.cursor()) as cursor:
yield cursor
def init_tables():
with getcursor() as cursor:
cursor.execute('CREATE TABLE IF NOT EXISTS client (id INT PRIMARY KEY,'
'hostname TEXT NOT NULL UNIQUE, mac TEXT UNIQUE, role TEXT)')
cursor.execute('CREATE INDEX IF NOT EXISTS idxmac ON client(mac)')
def normalizemac(mac):
return ':'.join(x.zfill(2) for x in mac.split(':')).upper()
def gethostname(mac, base=None, role=None):
mac, base, role = normalizemac(mac), base or 'lab', role or 'default'
with getcursor() as cursor:
(newid,) = cursor.execute('SELECT COALESCE(MAX(id)+1, 1) FROM client').fetchone()
data = (newid, '%s-%s' % (base, newid), mac, role)
cursor.execute('INSERT OR IGNORE INTO client VALUES (?,?,?,?)', data)
(hostname,) = cursor.execute('SELECT hostname FROM client WHERE mac = "%s"' % mac)
return hostname
def getmac(ip):
Popen(['ping', '-c1', '-t2', ip], stdout=PIPE).communicate()
arp = Popen(['arp', '-n', ip], stdout=PIPE).communicate()[0]
return re.search(r'(([\da-fA-F]{1,2}\:){5}[\da-fA-F]{1,2})', arp).group(1)
@route('/hosts')
def hosts():
where = 'WHERE role = "%s"' % request.query.role if request.query.role else ''
with getcursor() as cursor:
return dumps([dict((meta[0], data)
for meta, data in zip(cursor.description, row))
for row in cursor.execute('SELECT hostname, mac, role FROM client ' +
where + ' ORDER BY id ')], indent=4)
@route('/mac2hostname')
def mac2hostname():
if not request.query.mac:
return 'Usage: GET /mac2hostname?mac=XX_XX_XX_XX_XX_XX[&base=YYY]'
return gethostname(request.query.mac, request.query.base, request.query.role)
@route('/whatsmyhostname')
def whatsmyhostname():
ip = request.query.ip or request['REMOTE_ADDR']
return gethostname(getmac(ip), request.query.base, request.query.role)
if __name__ == '__main__':
init_tables()
run()