numeros_disponibles: ip_adm + boucle + propre
This commit is contained in:
parent
407c1e4799
commit
d4cde74149
1 changed files with 22 additions and 28 deletions
|
@ -11,6 +11,8 @@ Licence : GPLv2
|
|||
import sys
|
||||
from config import NETs
|
||||
from iptools import AddrInNet
|
||||
import netaddr
|
||||
|
||||
try:
|
||||
from dialog import Dialog
|
||||
except ImportError:
|
||||
|
@ -18,14 +20,6 @@ except ImportError:
|
|||
|
||||
repertoire = '/usr/scripts/var/numeros_disponibles/'
|
||||
|
||||
"""
|
||||
Un petit hack de rien du tout pour s'assurer qu'on n'attribue
|
||||
pas ces adresses. Certains services risquent de continuer
|
||||
d'essayer de se connecter a ces adresses
|
||||
"""
|
||||
ancien_vlan_adm = ['10.231.136.0/24']
|
||||
|
||||
|
||||
def lister_ip_dispo(plage):
|
||||
f = open(repertoire + 'ip_' + plage)
|
||||
lignes = f.readlines()
|
||||
|
@ -48,25 +42,14 @@ def update_ip(plage, 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] < 254:
|
||||
n[3] += 1
|
||||
else:
|
||||
n[2] += 1
|
||||
n[3] = 1
|
||||
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)
|
||||
ne = netaddr.IPNetwork(ne)
|
||||
for ip in ne:
|
||||
# avoid .255 and .0 (even for non-/24 nets)
|
||||
if (ip.value & 255) in [0,255]:
|
||||
continue
|
||||
pool_ip.append(str(ip))
|
||||
|
||||
resultat = ''.join('%s\n' % ip for ip in pool_ip if ip not in occupees and not AddrInNet(ip, ancien_vlan_adm))
|
||||
resultat = ''.join('%s\n' % ip for ip in pool_ip if ip not in occupees)
|
||||
|
||||
f = open(repertoire + 'ip_' + plage,'w')
|
||||
f.write(resultat)
|
||||
|
@ -78,6 +61,17 @@ def update_ip_fixe(occupees):
|
|||
def update_ip_wifi_adh(occupees):
|
||||
update_ip('wifi-adh','ip_wifi-adh', occupees)
|
||||
|
||||
TO_COMPUTE = [
|
||||
'wifi',
|
||||
'wifi-adh',
|
||||
'serveurs',
|
||||
'adherents',
|
||||
'bornes',
|
||||
'adm',
|
||||
'personnel-ens',
|
||||
'fil',
|
||||
]
|
||||
|
||||
if __name__ == "__main__":
|
||||
if "--cron" in sys.argv:
|
||||
cron = True
|
||||
|
@ -88,9 +82,9 @@ if __name__ == "__main__":
|
|||
dlg.gauge_start(text="Recherche des machines...", backtitle="numeros_disponibles")
|
||||
ip_occupees = lister_ip_utilisees()
|
||||
done = 1
|
||||
for net in NETs.keys():
|
||||
for net in TO_COMPUTE:
|
||||
if not cron:
|
||||
dlg.gauge_update(int(done*100/(len(NETs)+1)), text="IP libres dans %s" % net, update_text=True)
|
||||
dlg.gauge_update(int(done*100/(len(TO_COMPUTE)+1)), text="IP libres dans %s" % net, update_text=True)
|
||||
update_ip(net, ip_occupees)
|
||||
done += 1
|
||||
if not cron:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue