72 lines
2.6 KiB
Python
Executable File
72 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import dnsmgr
|
|
import sys
|
|
|
|
from json import dumps
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description='List DNS zones.',
|
|
formatter_class=lambda prog: argparse.HelpFormatter(
|
|
prog, max_help_position=45, width=140))
|
|
parser.add_argument('-a', '--all-zones', help='do not ignore zones that are not managed', action='store_true')
|
|
parser.add_argument('-c', '--config', help='path to config file', default=dnsmgr.DEFAULT_CFGFILE)
|
|
parser.add_argument('-d', '--decode', help='decode internationalized domain names (IDN)', action='store_true')
|
|
output = parser.add_mutually_exclusive_group()
|
|
output.add_argument('-j', '--json', help='print json format', action='store_true')
|
|
output.add_argument('-J', '--json-pretty', help='print pretty json format', action='store_true')
|
|
output.add_argument('-r', '--raw', help='print raw format', action='store_true')
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
manager = dnsmgr.DNSManager(cfgfile=args.config)
|
|
except RuntimeError as e:
|
|
dnsmgr.printe(f'config: {e}')
|
|
sys.exit(100)
|
|
|
|
try:
|
|
zones = manager.all_zones if args.all_zones else manager.zones
|
|
except RuntimeError as e:
|
|
dnsmgr.printe(e)
|
|
sys.exit(150)
|
|
|
|
zones.sort(key=lambda zone: zone.origin.to_unicode() if args.decode else zone.origin.to_text())
|
|
|
|
if args.raw:
|
|
for zone in zones:
|
|
name = zone.origin.to_unicode(True) if args.decode else zone.origin.to_text(True)
|
|
managed = zone.cfgfile is not None
|
|
print(f'{name}\t{zone.view}\t{zone.status}\t{managed}')
|
|
elif args.json or args.json_pretty:
|
|
json_output = [{
|
|
'zone': zone.origin.to_unicode(True) if args.decode else zone.origin.to_text(True),
|
|
'view': zone.view,
|
|
'status': zone.status,
|
|
'managed': zone.cfgfile is not None} for zone in zones]
|
|
if args.json_pretty:
|
|
print(dumps(json_output, indent=2))
|
|
else:
|
|
print(dumps(json_output))
|
|
|
|
else:
|
|
field_names = ['Zone', 'View', 'Status']
|
|
if args.all_zones:
|
|
field_names.append('Managed')
|
|
|
|
rows = []
|
|
for zone in zones:
|
|
name = zone.origin.to_unicode(True) if args.decode else zone.origin.to_text(True)
|
|
row = [name, zone.view, zone.status]
|
|
if args.all_zones:
|
|
row.append(zone.cfgfile is not None)
|
|
rows.append(row)
|
|
print(dnsmgr.prettytable(field_names, rows))
|
|
print(f'\nTotal: {len(rows)}\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|