scripts/gestion/numeros_disponibles.py
bos 961663c979 Nettement plus rapide !
darcs-hash:20070521174313-c992d-0208c36726b291fe3ab1cc85526bbd4b61c82368.gz
2007-05-21 19:43:13 +02:00

73 lines
1.7 KiB
Python
Executable file

#! /usr/bin/env python
# -*- coding: iso-8859-15 -*-
"""
Mise a jour periodique des listes d'ip et d'id disponibles.
Copyright (C) Alexandre Bos, largement pompe sur ldap_crans.py
Licence : GPLv2
"""
from config import NETs
from iptools import AddrInNet
noms_fichiers = {
'ip_fil' : '/usr/scripts/var/numeros_disponibles/ip_fixes',
'ip_wifi-adh' : '/usr/scripts/var/numeros_disponibles/ip_wifi-adh'
}
def lister_ip_utilisees():
liste = []
from ldap_crans import crans_ldap
db = crans_ldap()
r = db.search('mid=*')
machines = r['machineFixe'] + r['machineWifi'] + r['machineCrans']
for m in machines:
liste.append(m.ip())
return liste
def _update_ip(plage, fichier, occupees):
net = NETs[plage]
pool_ip = [] # Pool d'IP à tester
for ne in net:
ip = ne.split('/')[0]
ip = ip.split('.')
n = []
for i in ip:
n.append(int(i))
while 1:
if n[3] < 255:
n[3] += 1
else:
n[2] += 1
n[3] = 0
if n[2] == 255: break
ip = "%d.%d.%d.%d" % tuple(n)
if not AddrInNet(ip, ne):
# On est allé trop loin
break
pool_ip.append(ip)
resultat = ''
for ip in pool_ip:
if ip not in occupees :
resultat += '%s\n' % ip
f = open(noms_fichiers[fichier],'w')
f.write(resultat)
f.close
def update_ip_fixe(occupees):
_update_ip('fil', 'ip_fil', occupees)
def update_ip_wifi_adh(occupees):
_update_ip('wifi-adh','ip_wifi-adh', occupees)
if __name__ == "__main__":
occupees = lister_ip_utilisees()
update_ip_fixe(occupees)
update_ip_wifi_adh(occupees)