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