1 #!/usr/bin/env python3 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: GPL-2.0 OR BSD-3-Cl 2 # SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 3 3 4 import argparse 4 import argparse 5 import json 5 import json 6 import pprint 6 import pprint 7 import time 7 import time 8 8 9 from lib import YnlFamily, Netlink, NlError 9 from lib import YnlFamily, Netlink, NlError 10 10 11 11 12 class YnlEncoder(json.JSONEncoder): 12 class YnlEncoder(json.JSONEncoder): 13 def default(self, obj): 13 def default(self, obj): 14 if isinstance(obj, bytes): 14 if isinstance(obj, bytes): 15 return bytes.hex(obj) 15 return bytes.hex(obj) 16 if isinstance(obj, set): 16 if isinstance(obj, set): 17 return list(obj) 17 return list(obj) 18 return json.JSONEncoder.default(self, 18 return json.JSONEncoder.default(self, obj) 19 19 20 20 21 def main(): 21 def main(): 22 description = """ 22 description = """ 23 YNL CLI utility - a general purpose netlin 23 YNL CLI utility - a general purpose netlink utility that uses YAML 24 specs to drive protocol encoding and decod 24 specs to drive protocol encoding and decoding. 25 """ 25 """ 26 epilog = """ 26 epilog = """ 27 The --multi option can be repeated to incl 27 The --multi option can be repeated to include several do operations 28 in the same netlink payload. 28 in the same netlink payload. 29 """ 29 """ 30 30 31 parser = argparse.ArgumentParser(descripti 31 parser = argparse.ArgumentParser(description=description, 32 epilog=ep 32 epilog=epilog) 33 parser.add_argument('--spec', dest='spec', 33 parser.add_argument('--spec', dest='spec', type=str, required=True) 34 parser.add_argument('--schema', dest='sche 34 parser.add_argument('--schema', dest='schema', type=str) 35 parser.add_argument('--no-schema', action= 35 parser.add_argument('--no-schema', action='store_true') 36 parser.add_argument('--json', dest='json_t 36 parser.add_argument('--json', dest='json_text', type=str) 37 37 38 group = parser.add_mutually_exclusive_grou 38 group = parser.add_mutually_exclusive_group() 39 group.add_argument('--do', dest='do', meta 39 group.add_argument('--do', dest='do', metavar='DO-OPERATION', type=str) 40 group.add_argument('--multi', dest='multi' 40 group.add_argument('--multi', dest='multi', nargs=2, action='append', 41 metavar=('DO-OPERATION' 41 metavar=('DO-OPERATION', 'JSON_TEXT'), type=str) 42 group.add_argument('--dump', dest='dump', 42 group.add_argument('--dump', dest='dump', metavar='DUMP-OPERATION', type=str) 43 group.add_argument('--list-ops', action='s 43 group.add_argument('--list-ops', action='store_true') 44 group.add_argument('--list-msgs', action=' 44 group.add_argument('--list-msgs', action='store_true') 45 45 46 parser.add_argument('--sleep', dest='sleep 46 parser.add_argument('--sleep', dest='sleep', type=int) 47 parser.add_argument('--subscribe', dest='n 47 parser.add_argument('--subscribe', dest='ntf', type=str) 48 parser.add_argument('--replace', dest='fla 48 parser.add_argument('--replace', dest='flags', action='append_const', 49 const=Netlink.NLM_F_RE 49 const=Netlink.NLM_F_REPLACE) 50 parser.add_argument('--excl', dest='flags' 50 parser.add_argument('--excl', dest='flags', action='append_const', 51 const=Netlink.NLM_F_EX 51 const=Netlink.NLM_F_EXCL) 52 parser.add_argument('--create', dest='flag 52 parser.add_argument('--create', dest='flags', action='append_const', 53 const=Netlink.NLM_F_CR 53 const=Netlink.NLM_F_CREATE) 54 parser.add_argument('--append', dest='flag 54 parser.add_argument('--append', dest='flags', action='append_const', 55 const=Netlink.NLM_F_AP 55 const=Netlink.NLM_F_APPEND) 56 parser.add_argument('--process-unknown', a 56 parser.add_argument('--process-unknown', action=argparse.BooleanOptionalAction) 57 parser.add_argument('--output-json', actio 57 parser.add_argument('--output-json', action='store_true') 58 parser.add_argument('--dbg-small-recv', de 58 parser.add_argument('--dbg-small-recv', default=0, const=4000, 59 action='store', nargs= 59 action='store', nargs='?', type=int) 60 args = parser.parse_args() 60 args = parser.parse_args() 61 61 62 def output(msg): 62 def output(msg): 63 if args.output_json: 63 if args.output_json: 64 print(json.dumps(msg, cls=YnlEncod 64 print(json.dumps(msg, cls=YnlEncoder)) 65 else: 65 else: 66 pprint.PrettyPrinter().pprint(msg) 66 pprint.PrettyPrinter().pprint(msg) 67 67 68 if args.no_schema: 68 if args.no_schema: 69 args.schema = '' 69 args.schema = '' 70 70 71 attrs = {} 71 attrs = {} 72 if args.json_text: 72 if args.json_text: 73 attrs = json.loads(args.json_text) 73 attrs = json.loads(args.json_text) 74 74 75 ynl = YnlFamily(args.spec, args.schema, ar 75 ynl = YnlFamily(args.spec, args.schema, args.process_unknown, 76 recv_size=args.dbg_small_r 76 recv_size=args.dbg_small_recv) 77 if args.dbg_small_recv: 77 if args.dbg_small_recv: 78 ynl.set_recv_dbg(True) 78 ynl.set_recv_dbg(True) 79 79 80 if args.ntf: 80 if args.ntf: 81 ynl.ntf_subscribe(args.ntf) 81 ynl.ntf_subscribe(args.ntf) 82 82 83 if args.sleep: 83 if args.sleep: 84 time.sleep(args.sleep) 84 time.sleep(args.sleep) 85 85 86 if args.list_ops: 86 if args.list_ops: 87 for op_name, op in ynl.ops.items(): 87 for op_name, op in ynl.ops.items(): 88 print(op_name, " [", ", ".join(op. 88 print(op_name, " [", ", ".join(op.modes), "]") 89 if args.list_msgs: 89 if args.list_msgs: 90 for op_name, op in ynl.msgs.items(): 90 for op_name, op in ynl.msgs.items(): 91 print(op_name, " [", ", ".join(op. 91 print(op_name, " [", ", ".join(op.modes), "]") 92 92 93 try: 93 try: 94 if args.do: 94 if args.do: 95 reply = ynl.do(args.do, attrs, arg 95 reply = ynl.do(args.do, attrs, args.flags) 96 output(reply) 96 output(reply) 97 if args.dump: 97 if args.dump: 98 reply = ynl.dump(args.dump, attrs) 98 reply = ynl.dump(args.dump, attrs) 99 output(reply) 99 output(reply) 100 if args.multi: 100 if args.multi: 101 ops = [ (item[0], json.loads(item[ 101 ops = [ (item[0], json.loads(item[1]), args.flags or []) for item in args.multi ] 102 reply = ynl.do_multi(ops) 102 reply = ynl.do_multi(ops) 103 output(reply) 103 output(reply) 104 except NlError as e: 104 except NlError as e: 105 print(e) 105 print(e) 106 exit(1) 106 exit(1) 107 107 108 if args.ntf: 108 if args.ntf: 109 ynl.check_ntf() 109 ynl.check_ntf() 110 output(ynl.async_msg_queue) 110 output(ynl.async_msg_queue) 111 111 112 112 113 if __name__ == "__main__": 113 if __name__ == "__main__": 114 main() 114 main()
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.