141 lines
4.5 KiB
Python
Executable file
141 lines
4.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
|
||
from ldap_crans import hostname
|
||
|
||
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
|
||
if hostname == 'rouge' :
|
||
DHCPD_CONF = '/etc/dhcp3/dhcpd.conf'
|
||
else :
|
||
DHCPD_CONF='/etc/dhcpd.conf'
|
||
|
||
# Options g-Aénérales-b
|
||
if hostname == 'rouge' :
|
||
base_conf = """option option-252 code 252 = text ;
|
||
option option-119 code 119 = text ;
|
||
"""
|
||
else :
|
||
base_conf = ''
|
||
|
||
# Réseaux servis avec leurs options spécifiques
|
||
if hostname == 'zamok':
|
||
reseaux = { '138.231.136.0/21' :
|
||
"""option routers 138.231.136.4;
|
||
option domain-name-servers 138.231.136.3, 138.231.136.10, 138.231.136.9;
|
||
option domain-name "crans.org";
|
||
option option-119 "crans.org wifi.crans.org";""" }
|
||
elif hostname == 'nectaris':
|
||
reseaux = { '138.231.148.0/22' :
|
||
"""option routers 138.231.148.1;
|
||
not authoritative;
|
||
option domain-name-servers 138.231.148.1;
|
||
option domain-name "crans.org";
|
||
option option-119 "crans.org wifi.crans.org";"""
|
||
}
|
||
elif hostname == 'rouge':
|
||
reseaux = { '138.231.136.0/21' :
|
||
"""authoritative;
|
||
option routers 138.231.136.4;
|
||
option domain-name-servers 138.231.136.3, 138.231.136.10, 138.231.136.9;
|
||
option domain-name "crans.org";
|
||
option option-119 "crans.org wifi.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.3;
|
||
option ntp-servers 138.231.136.3;
|
||
option smtp-server 138.231.136.3;
|
||
option netbios-name-servers 138.231.136.1;
|
||
option netbios-dd-server 138.231.136.1;
|
||
option netbios-node-type 2;
|
||
option ip-forwarding off;
|
||
option option-252 "http://www.crans.org/proxy.pac" ;
|
||
deny unknown-clients;
|
||
%(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
|
||
|
||
if hostname == 'zamok':
|
||
restart_cmd = '/etc/init.d/dhcp restart'
|
||
elif hostname == 'nectaris':
|
||
restart_cmd = 'kill $(ps auxwwc | awk \'($11 == "dhcpd") {print $2}\') ; sleep 1 ; dhcpd em0'
|
||
elif hostname == 'rouge' :
|
||
restart_cmd = '/etc/init.d/dhcp3-server restart'
|
||
else:
|
||
restart_cmd = 'true'
|
||
|
||
######################################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
|
||
|
||
### Ecriture du fichier
|
||
fd = self._open_conf(self.DHCPD_CONF,'#')
|
||
fd.write(self.base_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
|