def test_string(self): data = iscconf.parse("""\ xxx { xxx "hello world"; }""") self.assertEqual(data, { ('xxx',): { ('xxx',): 'hello world', }, })
def test_list(self): data = iscconf.parse("""\ host s10557.dc2 { fixed-address 10.242.15.6, 127.0.0.1; hardware ethernet 00:1b:78:cf:b3:90; }""") self.assertEqual(data, { ('host', 's10557.dc2'): { ('fixed-address',): ['10.242.15.6', '127.0.0.1'], ('hardware', 'ethernet'): '00:1b:78:cf:b3:90', }, })
def read_dhcp_conf(self, file='dhcpd.conf'): """ Reads a local dhcp.conf file and converts into a useable dict Args: file (str, optional): local file to convert Returns: dict: Nested dictonary that holds conf info """ try: with open(file, 'r') as f: return self._join_sets(iscconf.parse(f.read())) except iscconf.parser.SyntaxError as ex: raise DhcpException('Dhcp configuration file is invalid: {}'.format(ex))
def handle(self, filename, *args, **options): data = iscconf.parse(open(filename, 'r').read()) for key, info in data.iteritems(): if not key[0] == 'host': continue mac = MACAddressField.normalize(info[('hardware', 'ethernet')]) for entry in DHCPEntry.objects.filter(mac=mac): entry.delete() addresses = info[('fixed-address',)] if not isinstance(addresses, list): addresses = [addresses] for address in addresses: print('Importing %s = %s' % (address, mac)) entry = DHCPEntry(ip=address.strip(), mac=mac) entry.save()
def handle(self, filename, *args, **options): data = iscconf.parse(open(filename, 'r').read()) for key, info in data.iteritems(): if not key[0] == 'host': continue mac = MACAddressField.normalize(info[('hardware', 'ethernet')]) for entry in DHCPEntry.objects.filter(mac=mac): entry.delete() addresses = info[('fixed-address', )] if not isinstance(addresses, list): addresses = [addresses] for address in addresses: print('Importing %s = %s' % (address, mac)) entry = DHCPEntry(ip=address.strip(), mac=mac) entry.save()
def test_single_comments(self): data = iscconf.parse("""\ # comment host s10557.dc2 { # comment fixed-address 10.242.15.6; hardware ethernet 00:1b:78:cf:b3:90; } # comment """) self.assertEqual(data, { ('host', 's10557.dc2'): { ('fixed-address',): '10.242.15.6', ('hardware', 'ethernet'): '00:1b:78:cf:b3:90', }, })
def test_multi(self): data = iscconf.parse("""\ host s10557.dc2 { fixed-address 10.242.15.6; hardware ethernet 00:1b:78:cf:b3:90; } host s10498.dc2 { fixed-address 10.242.15.5; hardware ethernet 00:1a:4b:d0:7c:60; }""") self.assertEqual(data, { ('host', 's10557.dc2'): { ('fixed-address',): '10.242.15.6', ('hardware', 'ethernet'): '00:1b:78:cf:b3:90', }, ('host', 's10498.dc2'): { ('fixed-address',): '10.242.15.5', ('hardware', 'ethernet'): '00:1a:4b:d0:7c:60', }, })
#!/usr/bin/python # A simple example use of iscconf to parse DHCP configuration files import iscconf with open('dhcpd.conf') as f: conf = iscconf.parse(f.read()) print(conf)
def test_empty(self): data = iscconf.parse('') self.assertEqual(data, {})
def test_mac_2E(self): data = iscconf.parse("2E:2C:45:E8:C9:23;") self.assertEqual(data, { (): '2e:2c:45:e8:c9:23', })
def test_empty_comment(self): data = iscconf.parse('# xxx') self.assertEqual(data, {})
def test_listen(self): data = iscconf.parse("""listen-on port 1053 { "any"; };""") self.assertEqual(data, { ('listen-on', 'port', 1053): {(): 'any'}, })
def test_no_key(self): data = iscconf.parse("""allow-query { "none"; };""") self.assertEqual(data, { ('allow-query',): {(): 'none'}, })
metavar="/tmp/dhcpstatus_output.json", default=stdout, type=FileType("w"), help="JSON output file for DHCP statistics", ) arse.add_argument("isp_name", metavar="ISP-name", help="Name of shared-network to create statistics for, can be 'any'") args = arse.parse_args() indent = None if args.indent: indent = 4 try: parsed_dhcp_config = parse(args.configuration.read()) except Exception as e: print(str(e), file=stderr) arse.print_usage() exit(1) # Start building the output dict by reading subnet info for each ISP defined # in dhcpd.conf as a shared-network. json_isp = {} for item in parsed_dhcp_config: try: (k, v) = item except: continue if k == "shared-network": last_isp = v