4 class ParamsError(Exception):
5 def __init__(self,reason):
12 "class that handles dhcpd config"
15 self.host_list = dict()
16 self.glob = { 'domain_name' : 'my_domain_name',
19 'net_address' : '192.168.0.0',
20 'net_mask' : '255.255.255.0',
21 'net_start' : '192.168.0.100',
22 'net_end' : '192.168.0.200',
23 'net_gateway' : '192.168.0.1'}
26 def to_file_format(self):
27 #bajo los parametros globales
28 glob_file = open('dhcpd_global.template','r')
29 glob_tmp = glob_file.read()
31 conf = glob_tmp % self.glob
33 host_file = open('dhcpd_host.template','r')
34 host_tmp = host_file.read()
36 for h , v in self.host_list.iteritems():
37 conf = conf + '\n' + (host_tmp % v)
40 def add_host(self, args):
41 #deberia indexar por hostname o por ip?
43 self.host_list[args[0]] = {"host_name": args[0], "host_ip": args[1], "host_mac": args[2]}
45 raise ParamsError('Wrong number of parameters')
47 def mod_host(self, args):
48 #deberia indexar por hostname o por ip?
50 self.host_list[args[0]] = {"host_name": args[0], "host_ip": args[1], "host_mac": args[2]}
52 raise ParamsError('Wrong number of parameters')
54 def remove_host(self, hostname):
55 if hostname in self.host_list:
56 del(self.host_list[hostname])
58 raise ParamsError("No such host")
61 if pair[0] in self.glob:
62 self.glob[pair[0]] = pair[1]
64 raise ParamsError("Parameter " + pair[0] + " not found")
67 #esto seria para poner en una interfaz
68 #y seria el hook para arrancar el servicio
72 #esto seria para poner en una interfaz
73 #y seria el hook para arrancar el servicio
77 #esto seria para poner en una interfaz
78 #y seria que hace el pickle deberia llamarse
80 output = open('dhcpd_config.pkl', 'wb')
81 pickle.dump(config, output)
84 def show_params(self):
86 for k , v in self.glob.iteritems():
87 string = string + k + ' : ' + v + '\n'
92 for k , v in self.host_list.iteritems():
93 string = string + k + ' : ' + v["host_ip"] + ' : ' + v["host_mac"] + '\n'
97 if __name__ == '__main__':
102 arguments = ('my_name','192.168.0.102','00:12:ff:56')
103 config.add_host(arguments)
105 arguments = ('my_name','192.168.0.192','00:12:ff:56')
106 config.mod_host(arguments)
108 arguments = ('nico','192.168.0.188','00:00:00:00')
109 config.add_host(arguments)
111 config.set(('domain_name','baryon.com.ar'))
112 config.set(('sarasa','baryon.com.ar'))
114 except ParamsError, inst:
119 conf_file = open('dhcpd.conf','w')
120 conf_file.write(config.to_file_format())