64 lines
1.8 KiB
Python
Executable file
64 lines
1.8 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso-8859-15 -*-
|
|
|
|
import sys
|
|
sys.path.append('/usr/scripts/gestion')
|
|
from annuaires import reverse, all_switchs
|
|
from hptools import hpswitch
|
|
from time import sleep
|
|
from os import system
|
|
import threading
|
|
|
|
# mise en forme d'une adresse mac
|
|
def format_mac(unformated_mac):
|
|
return ":".join([i.zfill(2) for i in unformated_mac.split(":")]).lower()
|
|
|
|
|
|
# classe d'interrogation des switchs
|
|
class interroge_switch (threading.Thread) :
|
|
def __init__ (self, switch, mac = None) :
|
|
threading.Thread.__init__(self)
|
|
self.switch = switch
|
|
self.mac = mac
|
|
self.reponse=None
|
|
self.start()
|
|
|
|
def run (self) :
|
|
sw = hpswitch(self.switch)
|
|
prise = None
|
|
iteration = 3
|
|
while (prise==None) & (iteration > 0):
|
|
prise = sw.where_is_mac(self.mac)
|
|
iteration = iteration-1
|
|
if (prise != None):
|
|
nom=sw.nom(None,prise)
|
|
if nom.find("uplink")==-1:
|
|
self.reponse = ("%-10s => prise %-2s : %s" % (self.switch.encode('iso-8859-15').replace('.adm.crans.org',''), str(prise), nom))
|
|
|
|
# interrogation des switchs en parallele
|
|
def get_trace(mac):
|
|
|
|
tableau=[]
|
|
|
|
for switch in ['backbone'] + all_switchs():
|
|
tableau.append(interroge_switch(switch, mac))
|
|
|
|
while len(threading.enumerate()) > 1 :
|
|
sleep(1)
|
|
|
|
resultat ='tracage de %s\n' % mac
|
|
|
|
tracage=''
|
|
for t in tableau:
|
|
if t.reponse:
|
|
tracage += t.reponse +"\n"
|
|
if tracage=='':
|
|
tracage = "mac inconnue des switchs"
|
|
|
|
return (resultat + tracage)
|
|
|
|
# on interroge les switchs et on fait un whos sur la mac
|
|
if __name__ == '__main__':
|
|
mac = format_mac(sys.argv[1])
|
|
print get_trace(mac)
|
|
print system('/usr/scripts/gestion/whos.py -a mac=%s' % mac)
|