115 lines
3.5 KiB
Python
Executable file
115 lines
3.5 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso-8859-15 -*-
|
|
|
|
""" Génération de la configuration pour le dhcp
|
|
|
|
Copyright (C) Frédéric Pauget
|
|
Licence : GPLv2
|
|
"""
|
|
|
|
from iptools import AddrInNet, param
|
|
from gen_confs import gen_config
|
|
|
|
class dhcp(gen_config) :
|
|
""" Génération du fichier de configuration pour dhcpd (DHCPD_CONF)
|
|
Le fichier comporte une partie par réseau servi, chaque réseau
|
|
servi doit être une clef du dictionnaire reseaux, la valeur correspondante
|
|
est une chaine décrivant les options spécifiques à ce réseau.
|
|
Les options communes sont celles de base_dhcp.
|
|
|
|
Chaque machines possède ensuite une entrée de la forme de host_template
|
|
"""
|
|
######################################PARTIE DE CONFIGURATION
|
|
|
|
# Fichier à écire
|
|
DHCPD_CONF='/etc/dhcpd.conf'
|
|
|
|
# Réseaux servis avec leurs options spécifiques
|
|
reseaux = { '138.231.136.0/21' :
|
|
"""option routers 138.231.136.4;
|
|
option domain-name-servers 138.231.136.6, 138.231.136.10, 138.231.136.9;
|
|
option domain-name "crans.org";
|
|
option option-119 "crans.org wifi.crans.org";""",
|
|
|
|
'138.231.148.0/22' :
|
|
"""option routers 138.231.148.1;
|
|
option domain-name-servers 138.231.148.1;
|
|
option domain-name "wifi.crans.org";
|
|
option option-119 "wifi.crans.org crans.org";"""
|
|
}
|
|
|
|
# Options communes à toutes les réseaux servis
|
|
base_dhcp="""
|
|
subnet %(network)s netmask %(netmask)s {
|
|
default-lease-time 86400;
|
|
option subnet-mask %(netmask)s;
|
|
option broadcast-address %(broadcast)s;
|
|
%(OPTIONS_RESEAU)s
|
|
option time-servers 138.231.136.6;
|
|
option ntp-servers 138.231.136.6;
|
|
option smtp-server 138.231.136.6;
|
|
option netbios-name-servers 138.231.136.6;
|
|
option netbios-dd-server 138.231.136.6;
|
|
option netbios-node-type 8;
|
|
option ip-forwarding off;
|
|
option option-252 "http://www.crans.org/proxy.pac" ;
|
|
deny unknown-clients;
|
|
not authoritative;
|
|
%(HOSTs)s
|
|
}
|
|
"""
|
|
|
|
host_template="""
|
|
host %(nom)s {
|
|
hardware ethernet %(mac)s;
|
|
fixed-address %(ip)s;
|
|
option host-name "%(nom)s";
|
|
}
|
|
"""
|
|
|
|
### Verbosité
|
|
# Si =1 ralera (chaine warnings) si machines hors zone trouvée
|
|
# Si =0 ralera seulement si réseau vide
|
|
verbose = 1
|
|
|
|
restart_cmd = '/etc/init.d/dhcp restart'
|
|
|
|
######################################FIN PARTIE DE CONFIGURATION
|
|
|
|
def __str__(self) :
|
|
return 'dhcp'
|
|
|
|
def _gen(self) :
|
|
warnings =''
|
|
|
|
### Construction de la partie du fichier contenant les machines
|
|
hosts={}
|
|
|
|
self.anim.iter=len(self.machines)
|
|
for machine in self.machines :
|
|
self.anim.cycle()
|
|
t = 0
|
|
for net in self.reseaux.keys() :
|
|
if AddrInNet(machine.ip(),net) :
|
|
d = { 'nom' : machine.nom().split('.')[0] , 'mac' : machine.mac() , 'ip' : machine.ip() }
|
|
try : hosts[net] += self.host_template % d
|
|
except : hosts[net] = self.host_template % d
|
|
t = 1
|
|
if not t and self.verbose :
|
|
warnings += u'Machine ignorée (mid=%s) : ip en dehors des réseaux servis (%s)\n' % ( machine.id(), machine.ip() )
|
|
|
|
### Ecriture du fichier
|
|
fd = self._open_conf(self.DHCPD_CONF,'#')
|
|
for net, options in self.reseaux.items() :
|
|
if not hosts.has_key(net) :
|
|
warnings += u'Réseau %s ignoré : aucune machine à servir\n' % net
|
|
continue
|
|
d = param(net)
|
|
d['OPTIONS_RESEAU'] = options
|
|
d['HOSTs'] = hosts[net]
|
|
|
|
fd.write(self.base_dhcp % d)
|
|
|
|
fd.close()
|
|
|
|
return warnings
|