forked from LibreSchool/server-config
Cambiamenti definitivi su installazione e funzionamento di mac2hostname.
This commit is contained in:
parent
b17f90b217
commit
cbdb82d182
@ -1,77 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""API to map MAC addresses to hostnames"""
|
||||
|
||||
from contextlib import contextmanager, closing
|
||||
from subprocess import check_output
|
||||
from sqlite3 import connect
|
||||
from bottle import route, run, request
|
||||
from json import dumps
|
||||
import re
|
||||
import os
|
||||
|
||||
__author__ = "Enrico Bacis"
|
||||
__email__ = "enrico.bacis@gmail.com"
|
||||
|
||||
# Aggiunta di Emiliano Vavassori <syntaxerrormmm-AT-gmail.com>
|
||||
# Configuring default directories for the program
|
||||
dbdir = '/var/lib/mac2hostname'
|
||||
logdir = '/var/log/mac2hostname'
|
||||
piddir = '/var/run/mac2hostname'
|
||||
|
||||
# Will create a PID file to be used with init.d script
|
||||
pid = os.getpid()
|
||||
op = open(piddir + '/service.pid', 'w')
|
||||
op.write("%s" % pid)
|
||||
op.close()
|
||||
|
||||
@contextmanager
|
||||
def getcursor(db = dbdir + '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)')
|
||||
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):
|
||||
mac, base = normalizemac(mac), base or 'lab'
|
||||
with getcursor() as cursor:
|
||||
(newid,) = cursor.execute('SELECT COALESCE(MAX(id)+1, 1) FROM client').fetchone()
|
||||
cursor.execute('INSERT OR IGNORE INTO client VALUES (%s, "%s-%s", "%s")' % (newid, base, newid, mac))
|
||||
(hostname,) = cursor.execute('SELECT hostname FROM client WHERE mac = "%s"' % mac)
|
||||
return hostname
|
||||
|
||||
def getmac(ip):
|
||||
check_output(['ping', '-c1', '-t2', ip])
|
||||
arp = check_output(['arp', '-n', ip])
|
||||
return re.search(r'(([\da-fA-F]{1,2}\:){5}[\da-fA-F]{1,2})', arp).group(1)
|
||||
|
||||
@route('/hosts')
|
||||
def hosts():
|
||||
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 FROM client ORDER BY id')], indent=4)
|
||||
|
||||
@route('/mac2hostname')
|
||||
def mac2hostname():
|
||||
mac, base = request.query.mac, request.query.base
|
||||
if not mac:
|
||||
return 'Usage: GET /mac2hostname?mac=XX_XX_XX_XX_XX_XX[&base=YYY]'
|
||||
return gethostname(mac, base)
|
||||
|
||||
@route('/whatsmyhostname')
|
||||
def whatsmyhostname():
|
||||
ip = request.query.ip or request['REMOTE_ADDR']
|
||||
return gethostname(getmac(ip), request.query.base)
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_tables()
|
||||
run(host='0.0.0.0', port=3000)
|
@ -6,37 +6,34 @@
|
||||
yum: pkg=python-bottle state=present
|
||||
sudo: yes
|
||||
|
||||
- name: Creazione directory necessarie
|
||||
file: path={{ item }} state=directory
|
||||
with_items:
|
||||
- /var/lib/mac2hostname
|
||||
- /var/run/mac2hostname
|
||||
- /var/log/mac2hostname
|
||||
- /usr/share/mac2hostname
|
||||
- name: Installazione mac2hostname
|
||||
yum: pkg=http://syntaxerrormmm.zapto.org/downloads/rpm/mac2hostname-1.0.0-1.el6.noarch.rpm state=present
|
||||
sudo: yes
|
||||
|
||||
- name: Copia dello script
|
||||
copy: src=mac2hostname.py dest=/usr/share/mac2hostname/mac2hostname.py mode=0755
|
||||
- name: Aggiungo il servizio ai servizi gestiti da NethServer
|
||||
shell: /sbin/e-smith/db configuration set mac2hostname service status enabled TCPPort 3000 access private
|
||||
sudo: yes
|
||||
|
||||
- name: Finalizzazione della copia
|
||||
file: src=/usr/share/mac2hostname/mac2hostname.py dest=/usr/bin/mac2hostname state=link
|
||||
- name: Default per NetServer - Cartella defaults
|
||||
shell: mkdir -p /etc/e-smith/db/configuration/defaults/mac2hostname
|
||||
sudo: yes
|
||||
|
||||
- name: Aggiunto oggetto servizio mac2hostname
|
||||
shell: /sbin/e-smith/db fwservices set mac2hostname fwservice Description "mac2hostname service" Ports 3000 Protocol tcp
|
||||
- name: Default per NethServer - Tipo
|
||||
shell: echo "service" > /etc/e-smith/db/configuration/defaults/mac2hostname/type
|
||||
sudo: yes
|
||||
|
||||
- name: Attivo modifiche a livello di servizio
|
||||
shell: /sbin/e-smith/signal-event firewall-objects-modify
|
||||
- name: Default per NethServer - Stato
|
||||
shell: echo "enabled" > /etc/e-smith/db/configuration/defaults/mac2hostname/status
|
||||
sudo: yes
|
||||
|
||||
- name: Aggiungo regola firewall mac2hostname
|
||||
shell: /sbin/e-smith/db fwrules set 1 rule Action accept Description "mac2hostname allow local traffic" Dst any Log none Position 64 service "fwservice;mac2hostname" Src "role;green" status enabled
|
||||
- name: Aggiunta servizio
|
||||
shell: /sbin/e-smith/signal-event runlevel-adjust
|
||||
sudo: yes
|
||||
|
||||
- name: Attivo modifiche firewall
|
||||
- name: Aggiunta servizio di rete e cambio firewall
|
||||
shell: /sbin/e-smith/signal-event firewall-adjust
|
||||
sudo: yes
|
||||
|
||||
|
||||
- name: Avvio di mac2hostname
|
||||
shell: service mac2hostname start
|
||||
sudo: yes
|
||||
|
Loading…
x
Reference in New Issue
Block a user