[mac_prises] En place, avec script d'analyse.
Je commence à en avoir marre des gens qui spoofent. J'ai donc pris en charge l'écriture de mac_prises cette nuit. Je le mets en test vers la mailing list test@lists.crans.org, avis aux amateurs.
This commit is contained in:
parent
baf4aa3645
commit
df49d0b6dc
6 changed files with 305 additions and 90 deletions
|
@ -10,7 +10,8 @@ from commands import getstatusoutput
|
|||
|
||||
sys.path.append('/usr/scripts/gestion')
|
||||
import annuaires_pg
|
||||
import psycopg2
|
||||
import time
|
||||
|
||||
|
||||
# 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)
|
||||
|
@ -18,7 +19,7 @@ import psycopg2
|
|||
# (plante lamentablement quand j'essaye avec mon compte sur vo, sous
|
||||
# ipython. Mais si je sudo ipython, ça marche...
|
||||
|
||||
def liste_prises_macs(switch):
|
||||
def liste_chambres_macs(switch):
|
||||
u'''
|
||||
Fonction générant un dictionnaire (macs) contenant pour chaque prise une
|
||||
liste des macs qui y sont actives.
|
||||
|
@ -33,28 +34,32 @@ def liste_prises_macs(switch):
|
|||
|
||||
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:
|
||||
if data:
|
||||
for port in data:
|
||||
if port == '':
|
||||
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)
|
||||
mac = data[port]
|
||||
uplink = annuaires_pg.uplink_prises[bat]
|
||||
prise = num_switch*100+port
|
||||
if prise in uplink:
|
||||
continue
|
||||
|
||||
result = annuaires_pg.reverse(bat, prise)
|
||||
if result:
|
||||
chbre = bat+result[0]
|
||||
if chbre in liste_chbres:
|
||||
macs[chbre].extend(mac)
|
||||
else:
|
||||
macs[chbre] = []
|
||||
macs[chbre].extend(mac)
|
||||
liste_chbres.append(chbre)
|
||||
del chbre
|
||||
else:
|
||||
# On droppe, c'est des bornes wifi ou autres.
|
||||
pass
|
||||
else:
|
||||
print "Pas de données pour %s" % (switch)
|
||||
return macs
|
||||
|
||||
def walk(host, oid):
|
||||
|
@ -67,8 +72,21 @@ def walk(host, oid):
|
|||
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
|
||||
try:
|
||||
pport, pmac = ligne.split('Hex-STRING: ')
|
||||
|
||||
port = int(pport.replace('STATISTICS-MIB::hpSwitchPortFdbAddress.', '').split('.')[0])
|
||||
mac = pmac.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])
|
||||
|
||||
if not result.has_key(port):
|
||||
result[port] = [mac]
|
||||
else:
|
||||
result[port].append(mac)
|
||||
except:
|
||||
print "Ligne moisie : %s de l'hôte : %s" % (ligne, host)
|
||||
return result
|
||||
|
||||
|
||||
|
@ -85,18 +103,14 @@ def __exec(cmd):
|
|||
|
||||
if __name__ == '__main__':
|
||||
switchs = sys.argv[1:]
|
||||
date = time.strftime('%F %T')
|
||||
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()
|
||||
macs = liste_chambres_macs(switch)
|
||||
|
||||
# if not os.path.isdir("bat%s/%d"%(bat, num_switch)):
|
||||
# os.makedirs("bat%s/%d"%(bat, num_switch))
|
||||
print macs
|
||||
|
||||
# fichier =
|
||||
# for chambre in macs.keys():
|
||||
# for mac in macs[chambre]:
|
||||
#
|
||||
# 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]))
|
||||
# curseur.execute(requete, (date, chambre, mac))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue