diff --git a/gestion/locate-wifi.py b/gestion/locate-wifi.py new file mode 100755 index 00000000..510e6f85 --- /dev/null +++ b/gestion/locate-wifi.py @@ -0,0 +1,84 @@ +#! /usr/bin/env python +# -*- coding: iso-8859-15 -*- + +import sys +sys.path.append('/usr/scripts/gestion') +from whos import borne_clients, borne_etat +from ldap_crans import crans_ldap +from time import sleep +from affich_tools import coul +import threading +from os import getuid + +# il faut être root +if getuid() : + print 'Il faut être root !' + sys.exit(1) + +# connexion à la base de données +db = crans_ldap() + +# décompostion des arguments +try : + mac = ":".join([i.zfill(2) for i in sys.argv[1].split(":")]).upper() +except : + mac = None + +# dit si une mac a déja été traitée +mac_done=[] +def done (mac) : + global mac_done + if mac in mac_done : + return True + else : + mac_done.append(mac) + return False + +# classe d'interrogation des bornes +class interroge_borne (threading.Thread) : + def __init__ (self, db, borne, mac = None) : + threading.Thread.__init__(self) + self.borne = borne + self.mac = mac + self.db = db + self.start() + + def aff_client (self, mac, rssi) : + if done(mac) : + return + + res = db.search("mac=%s" % mac)['machine'] + if not res: + client_nom = '????' + coul_rssi = 'rouge' + rssi = 0 + else: + # On va choisir la bonne couleur pour le RSSI + if rssi > -77: + coul_rssi = 'vert' + elif rssi > -90: + coul_rssi = 'jaune' + else: + coul_rssi = 'rouge' + print '%-10s %-30s (%-15s, RSSI: %s)' % (self.borne.nom().split('.')[0], res[0].proprietaire().Nom(), res[0].nom().split('.')[0],coul("%d" % rssi, coul_rssi)) + + def run (self) : + nom = self.borne.nom() + if not borne_etat(nom) : + return + clients = borne_clients(nom) + for (mac, rssi) in clients : + if not self.mac or self.mac == mac.lower() : + self.aff_client(mac,rssi) + +# on onterroge trois fois car il donne pas toujours les clients +for i in range(0,3) : + # on interroge les bornes + resultat = {} + bornes = db.search('canal=*')['machine'] + for borne in bornes : + interroge_borne(db, borne, mac) + +# on attend la fin de threads +while len(threading.enumerate()) > 1 : + sleep(1)