#!/usr/bin/env python3 import sys import argparse import json parser = argparse.ArgumentParser(description='Parse output from ifstatus and exatract addresses and prefixes') parser.add_argument('-t', '--tag', help='Prepend interface name to result') # The "interface" here is the Luci term, i.e. "lan", "wan" etc # parser.add_argument('interface', nargs='?') args = parser.parse_args() try: ifstatus_dict = json.load(sys.stdin) except json.JSONDecodeError as exc: print(exc.msg, " at line/column ", lineno, "/", colno) exit(1) for key in ['ipv4-address', 'ipv6-address', 'ipv6-prefix', 'ipv6-prefix-assignment']: if key in ifstatus_dict: ip_dict = ifstatus_dict[key] # print(key, ': ', ip_dict) if key == 'ipv4-address' or key == 'ipv6-address': for item in ip_dict: # print(item) print(args.tag if args.tag else '', item['address']) elif key == 'ipv6-prefix' or key == 'ipv6-prefix-assignment': for item in ip_dict: # print(item) print(args.tag if args.tag else '', item['address'] + '/' + str(item['mask'])) if 'local-address' in item: print(args.tag if args.tag else '', item['local-address']['address'])