+++ /dev/null
-ddns-update-style none;
-
-option domain-name baryon.com.ar;
-option domain-name-servers my_ns1, my_ns2;
-
-authoritative;
-
-log-facility local7;
-
-subnet 192.168.0.0 netmask 255.255.255.0 {
- range 192.168.0.100 192.168.0.200;
- option routers 192.168.0.1;
-}
-
-host my_name {
- fixed-address 192.168.0.192;
- hardware ethernet 00:12:ff:56;
-}
-
-host nico {
- fixed-address 192.168.0.188;
- hardware ethernet 00:00:00:00;
-}
+++ /dev/null
-
-import pickle
-
-class ParamsError(Exception):
- def __init__(self,reason):
- self.reason = reason
-
- def __str__(self):
- return repr(reason)
-
-class dhcpd:
- "class that handles dhcpd config"
-
- def __init__(self):
- self.host_list = dict()
- self.glob = { 'domain_name' : 'my_domain_name',
- 'dns_1' : 'my_ns1',
- 'dns_2' : 'my_ns2',
- 'net_address' : '192.168.0.0',
- 'net_mask' : '255.255.255.0',
- 'net_start' : '192.168.0.100',
- 'net_end' : '192.168.0.200',
- 'net_gateway' : '192.168.0.1'}
-
-
- def to_file_format(self):
- #bajo los parametros globales
- glob_file = open('dhcpd_global.template','r')
- glob_tmp = glob_file.read()
- glob_file.close()
- conf = glob_tmp % self.glob
- #bajo los hosts
- host_file = open('dhcpd_host.template','r')
- host_tmp = host_file.read()
- host_file.close()
- for h , v in self.host_list.iteritems():
- conf = conf + '\n' + (host_tmp % v)
- return conf
-
- def add_host(self, args):
- #deberia indexar por hostname o por ip?
- if len(args) == 3:
- self.host_list[args[0]] = {"host_name": args[0], "host_ip": args[1], "host_mac": args[2]}
- else:
- raise ParamsError('Wrong number of parameters')
-
- def mod_host(self, args):
- #deberia indexar por hostname o por ip?
- if len(args) == 3:
- self.host_list[args[0]] = {"host_name": args[0], "host_ip": args[1], "host_mac": args[2]}
- else:
- raise ParamsError('Wrong number of parameters')
-
- def remove_host(self, hostname):
- if hostname in self.host_list:
- del(self.host_list[hostname])
- else:
- raise ParamsError("No such host")
-
- def set(self, pair):
- if pair[0] in self.glob:
- self.glob[pair[0]] = pair[1]
- else:
- raise ParamsError("Parameter " + pair[0] + " not found")
-
- def start(self):
- #esto seria para poner en una interfaz
- #y seria el hook para arrancar el servicio
- pass
-
- def stop(self):
- #esto seria para poner en una interfaz
- #y seria el hook para arrancar el servicio
- pass
-
- def commit(self):
- #esto seria para poner en una interfaz
- #y seria que hace el pickle deberia llamarse
- #al hacerse un commit
- output = open('dhcpd_config.pkl', 'wb')
- pickle.dump(config, output)
- output.close()
-
- def show_params(self):
- string = ''
- for k , v in self.glob.iteritems():
- string = string + k + ' : ' + v + '\n'
- return string
-
- def show_hosts(self):
- string = ''
- for k , v in self.host_list.iteritems():
- string = string + k + ' : ' + v["host_ip"] + ' : ' + v["host_mac"] + '\n'
- return string
-
-
-if __name__ == '__main__':
-
- config = dhcpd()
-
- try :
- arguments = ('my_name','192.168.0.102','00:12:ff:56')
- config.add_host(arguments)
-
- arguments = ('my_name','192.168.0.192','00:12:ff:56')
- config.mod_host(arguments)
-
- arguments = ('nico','192.168.0.188','00:00:00:00')
- config.add_host(arguments)
-
- config.set(('domain_name','baryon.com.ar'))
- config.set(('sarasa','baryon.com.ar'))
-
- except ParamsError, inst:
- print inst.reason
-
- config.commit()
-
- conf_file = open('dhcpd.conf','w')
- conf_file.write(config.to_file_format())
- conf_file.close()
+++ /dev/null
-(i__main__
-dhcpd
-p0
-(dp1
-S'glob'
-p2
-(dp3
-S'net_end'
-p4
-S'192.168.0.200'
-p5
-sS'net_mask'
-p6
-S'255.255.255.0'
-p7
-sS'domain_name'
-p8
-S'baryon.com.ar'
-p9
-sS'dns_1'
-p10
-S'my_ns1'
-p11
-sS'dns_2'
-p12
-S'my_ns2'
-p13
-sS'net_start'
-p14
-S'192.168.0.100'
-p15
-sS'net_gateway'
-p16
-S'192.168.0.1'
-p17
-sS'net_address'
-p18
-S'192.168.0.0'
-p19
-ssS'host_list'
-p20
-(dp21
-S'my_name'
-p22
-(dp23
-S'host_name'
-p24
-g22
-sS'host_mac'
-p25
-S'00:12:ff:56'
-p26
-sS'host_ip'
-p27
-S'192.168.0.192'
-p28
-ssS'nico'
-p29
-(dp30
-g24
-g29
-sg25
-S'00:00:00:00'
-p31
-sg27
-S'192.168.0.188'
-p32
-sssb.
\ No newline at end of file
+++ /dev/null
-ddns-update-style none;
-
-option domain-name %(domain_name)s;
-option domain-name-servers %(dns_1)s, %(dns_2)s;
-
-authoritative;
-
-log-facility local7;
-
-subnet %(net_address)s netmask %(net_mask)s {
- range %(net_start)s %(net_end)s;
- option routers %(net_gateway)s;
-}
+++ /dev/null
-host %(host_name)s {
- fixed-address %(host_ip)s;
- hardware ethernet %(host_mac)s;
-}
--- /dev/null
+# vim: set encoding=utf-8 et sw=4 sts=4 :
+
+try:
+ import cPickle as pickle
+except ImportError:
+ import pickle
+
+from mako.template import Template
+from mako.runtime import Context
+
+class Host:
+ def __init__(self, name, ip, mac):
+ self.name = name
+ self.ip = ip
+ self.mac = mac
+ def __repr__(self):
+ return 'Host(name="%s", ip="%s", mac="%s")' % (
+ self.name, self.ip, self.mac)
+
+class HostHandler:
+
+ def __init__(self, hosts):
+ self.hosts = hosts
+
+ def add(self, name, ip, mac):
+ #deberia indexar por hostname o por ip? o por mac? :)
+ # Mejor por nada...
+ self.hosts[name] = Host(name, ip, mac)
+
+ def update(self, name, ip=None, mac=None):
+ if ip is not None:
+ self.hosts[name].ip = ip
+ if mac is not None:
+ self.hosts[name].mac = mac
+
+ def delete(self, name):
+ del self.hosts[name]
+
+ def list(self):
+ return ','.join(self.hosts)
+
+ def show(self):
+ hosts = self.hosts.values()
+ return '\n'.join('%s,%s,%s' % (h.name, h.ip, h.mac) for h in hosts)
+
+class DhcpHandler:
+ r"""class that handles DHCP service using dhcpd program"""
+
+ def __init__(self):
+ self.hosts = dict()
+ self.vars = dict(
+ domain_name = 'my_domain_name',
+ dns_1 = 'my_ns1',
+ dns_2 = 'my_ns2',
+ net_address = '192.168.0.0',
+ net_mask = '255.255.255.0',
+ net_start = '192.168.0.100',
+ net_end = '192.168.0.200',
+ net_gateway = '192.168.0.1',
+ )
+ self.host = HostHandler(self.hosts)
+
+ def set(self, param, value):
+ if param in self.vars:
+ self.vars[param] = value
+ else:
+ raise KeyError("Parameter " + param + " not found")
+
+ def list(self):
+ return ','.join(self.vars)
+
+ def show(self):
+ return '\n'.join(('%s,%s' % (k, v) for (k, v) in self.vars.items()))
+
+ def start(self):
+ #esto seria para poner en una interfaz
+ #y seria el hook para arrancar el servicio
+ pass
+
+ def stop(self):
+ #esto seria para poner en una interfaz
+ #y seria el hook para arrancar el servicio
+ pass
+
+ def commit(self):
+ #esto seria para poner en una interfaz
+ #y seria que hace el pickle deberia llamarse
+ #al hacerse un commit
+ pickle.dump(self.vars, file('pickled/vars.pkl', 'wb'), 2)
+ pickle.dump(self.hosts, file('pickled/hosts.pkl', 'wb'), 2)
+ tpl = Template(filename='templates/dhcpd.conf')
+ ctx = Context(file('generated/dhcpd.conf', 'w'),
+ hosts=self.hosts.values(), **self.vars)
+ tpl.render_context(ctx)
+
+if __name__ == '__main__':
+
+ config = DhcpHandler()
+
+ config.host.add('my_name','192.168.0.102','00:12:ff:56')
+
+ config.host.update('my_name','192.168.0.192','00:12:ff:56')
+
+ config.host.add('nico','192.168.0.188','00:00:00:00')
+
+ config.set('domain_name','baryon.com.ar')
+
+ try:
+ config.set('sarasa','baryon.com.ar')
+ except KeyError, e:
+ print 'Error:', e
+
+ config.commit()
+
+ print 'Variables:', config.list()
+ print config.show()
+
+ print 'Hosts:', config.host.list()
+ print config.host.show()
+
+ vars = pickle.load(file('pickled/vars.pkl'))
+ hosts = pickle.load(file('pickled/hosts.pkl'))
+ print 'Pickled vars:', vars
+ print 'Pickled hosts:', hosts
+
--- /dev/null
+
+ddns-update-style none;
+
+option domain-name ${domain_name};
+option domain-name-servers ${dns_1}, ${dns_2};
+
+authoritative;
+
+log-facility local7;
+
+subnet ${net_address} netmask ${net_mask} {
+ range ${net_start} ${net_end};
+ option routers ${net_gateway};
+}
+
+% for host in hosts:
+host ${host.name} {
+ fixed-address ${host.ip};
+ hardware ethernet ${host.mac};
+}
+
+% endfor