Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import os
- import re
- import socket
- import xml.etree.ElementTree as etree
- hosts_xml = "/opt/etc/hosts.xml"
- dhcp_hosts = "/etc/dhcp/dhcpd.hosts"
- ipset_entries = "/etc/iptables/ipset.entries"
- bool_flags = ['service', 'voip', 'ext']
- def err_msg(msg):
- sys.stderr.write("Fail: " + str(msg) + "\n")
- sys.exit(-1)
- # getting attributes of hosts
- def get_hosts(root):
- hosts = []
- for item in root.iter('host'):
- host = {}
- # main attributes
- try:
- host['name'] = item.attrib['name']
- host['ip'] = item.attrib['ip']
- host['mac'] = item.attrib['mac'].lower()
- except KeyError as key:
- print("Next host does't have attribute ", key, ":", sep="")
- etree.dump(item)
- continue
- # check IP address
- try:
- socket.inet_aton(host['ip'])
- except socket.error:
- print("This host has a incorrectly value of a IP address:")
- etree.dump(item)
- continue
- # check MAC address
- if not (re.match("[0-9a-f]{2}([-:][0-9a-f]{2}){5}$", host['mac'])):
- print("This host has a incorrectly value of a MAC address:")
- etree.dump(item)
- continue
- # other attributes
- host['mac_wifi'] = item.attrib.get('mac_wifi', '').lower()
- if ((not (re.match("[0-9a-f]{2}([-:][0-9a-f]{2}){5}$", host['mac_wifi'])))
- and (host['mac_wifi'] != '')):
- print("This host has a incorrectly value of a MAC Wi-Fi address:")
- etree.dump(item)
- host['desc'] = item.attrib.get('desc', 'Unknow')
- # boolean attributes
- for attrib in bool_flags:
- attrib_value = item.attrib.get(attrib, 'false').lower()
- if attrib_value not in ['true', 'false']:
- print("This host has a incorrectly value attribute '", attrib, "':", sep="")
- etree.dump(item)
- host[attrib] = (attrib_value == 'true')
- hosts.append(host)
- return hosts
- def get_dhcp_host(name, ip, mac, desc):
- return ("host "+ name + " { # " + desc + "\n"
- " hardware ethernet " + mac + ";\n"
- " fixed-address " + ip + ";\n"
- "}\n")
- if __name__ == "__main__":
- try:
- tree = etree.parse(hosts_xml)
- dhcp_f = open(dhcp_hosts, 'w')
- ipset_f = open(ipset_entries, 'w')
- except IOError as error:
- err_msg(error)
- hosts = get_hosts(tree.getroot())
- # sorting by IP
- for host in sorted(hosts, key=lambda host: socket.inet_aton(host['ip'])):
- # write hosts in dhcp.hosts and ipset.entries
- dhcp_f.write(get_dhcp_host(host['name'], host['ip'], host['mac'], host['desc']))
- ipset_f.write("# " + host['desc'] + "\n")
- ipset_f.write("add check_mac " + host['ip'] + "," + host['mac'] + "\n")
- if (host['mac_wifi'] != ''):
- dhcp_f.write(get_dhcp_host(host['name'] + "-wifi", host['ip'], host['mac_wifi'], host['desc'] + " wifi"))
- ipset_f.write("add check_mac_wifi " + host['ip'] + "," + host['mac_wifi'] + "\n")
- for flag in bool_flags:
- if (host[flag]): ipset_f.write("add " + flag + " " + host['ip'] + "\n")
- ipset_f.write("\n")
- dhcp_f.close()
- ipset_f.close()
- # reload dhcpd and ipset
- os.system("/etc/init.d/isc-dhcp-server restart")
- os.system("/usr/sbin/ipset flush")
- os.system("/usr/sbin/ipset restore < " + ipset_entries)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement