102 lines
3.4 KiB
Python
Executable file
102 lines
3.4 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# -*- coding: utf8 -*-
|
|
#
|
|
#
|
|
# PEB - 01/04/2012
|
|
#
|
|
|
|
import os, sys, re
|
|
from commands import getstatusoutput
|
|
|
|
sys.path.append('/usr/scripts/gestion')
|
|
import annuaires_pg
|
|
import psycopg2
|
|
|
|
# nécessite apparemment que l'objet conn soit bien créé lors de l'exec
|
|
# de annuaires_pg, il faut être root (ou dans je ne sais quel groupe)
|
|
# pour que l'authentification de l'user crans avec psycopg2 se fasse
|
|
# (plante lamentablement quand j'essaye avec mon compte sur vo, sous
|
|
# ipython. Mais si je sudo ipython, ça marche...
|
|
|
|
def liste_prises_macs(switch):
|
|
u'''
|
|
Fonction générant un dictionnaire (macs) contenant pour chaque prise une
|
|
liste des macs qui y sont actives.
|
|
'''
|
|
liste_bats = ['a', 'b', 'c', 'g', 'h', 'i', 'j', 'm', 'p']
|
|
|
|
split = switch.replace('.adm.crans.org', '').split('-')
|
|
bat, num_switch = split[0][-1], int(split[1][0])
|
|
if bat not in liste_bats:
|
|
print 'Le bâtiment '+bat+' ne figure pas dans la liste des bâtiments.'
|
|
data = walk(switch, 'STATISTICS-MIB::hpSwitchPortFdbAddress')
|
|
|
|
liste_chbres = []
|
|
macs = {}
|
|
for i in data:
|
|
if i == '':
|
|
continue
|
|
else:
|
|
port = int(i.replace('STATISTICS-MIB::hpSwitchPortFdbAddress.', '').split('.')[0])
|
|
mac = data[i].replace(' ', '').lower().replace('"', '')
|
|
if not re.match('([0-9a-f]{2}){6}', mac):
|
|
mac = mac.encode('hex').lower()
|
|
mac = "%s:%s:%s:%s:%s:%s" % (mac[0:2], mac[2:4], mac[4:6], mac[6:8], mac[8:10], mac[10:12])
|
|
uplink = annuaires_pg.uplink_prises[bat]
|
|
prise = num_switch*100+port
|
|
if prise in uplink:
|
|
continue
|
|
|
|
chbre = chbre_prises(bat, prise)
|
|
|
|
if chbre in liste_chbres:
|
|
macs[chbre].append(mac+'\n')
|
|
else:
|
|
macs[chbre] = []
|
|
macs[chbre].append(mac+'\n')
|
|
liste_chbres.append(chbre)
|
|
return macs
|
|
|
|
def walk(host, oid):
|
|
u'''
|
|
Hack sale remplaçant la fonction walk contenue dans hptools, qui
|
|
splitte suivant des espaces, ce qui engendre des failles.
|
|
Ici, snmpwalk -Ox (au lieu de -Oq) fait qu'on récupère bien des macs,
|
|
et on splitte suivant le keyword Hex-STRING, qui n'est pas redondant, lui.
|
|
'''
|
|
received = __exec('snmpwalk -Ox -v 1 -c public %s %s' % (host, oid)).split('\n')
|
|
result = {}
|
|
for ligne in received:
|
|
pport, pmac = ligne.split('Hex-STRING: ')
|
|
result[pport] = pmac
|
|
return result
|
|
|
|
|
|
def __exec(cmd):
|
|
u'''
|
|
Hack sale pour pas loader inutilement hptools.py
|
|
Exécute une commande et retourne son status et son output.
|
|
'''
|
|
status, response = getstatusoutput(cmd)
|
|
if status:
|
|
response = response.replace('snmpget: ','')
|
|
print 'Erreur : '+response+' : '+cmd
|
|
return response
|
|
|
|
if __name__ == '__main__':
|
|
switchs = sys.argv[1:]
|
|
for switch in switchs:
|
|
macs = liste_prises_macs(switch)
|
|
|
|
split = switch.replace('.adm.crans.org', '').split('-')
|
|
bat, num_switch = split[0][-1], int(split[1][0])
|
|
|
|
pgsql = psycopg2.connect(database="mac_prises", user="crans")
|
|
curseur = pgsql.cursor()
|
|
|
|
# if not os.path.isdir("bat%s/%d"%(bat, num_switch)):
|
|
# os.makedirs("bat%s/%d"%(bat, num_switch))
|
|
#
|
|
# for chbre in macs:
|
|
# with open('bat%s/%d/%s%03d.macs'%(bat, num_switch, bat, prise), 'w') as f:
|
|
# f.writelines(sorted(macs[prise]))
|