-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·285 lines (241 loc) · 7.75 KB
/
cli.py
File metadata and controls
executable file
·285 lines (241 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
from subprocess import check_output
import platform
import socket
import sys
import os
import nosh
from nosh import (
CLI,
TextToken,
IPv4AddressToken,
InterfaceAddressToken,
InterfaceToken,
StringToken,
IntToken,
)
def prompt_cb() -> str:
return "{}@{}>".format(os.getlogin(), socket.gethostname())
def fork_and_exec(args: list[str]):
try:
pid = os.fork()
except OSError as e:
print(f"failed to fork: {e}", file=sys.stderr)
if not pid:
try:
os.execvp(args[0], args)
except Exception as e:
print(f"{' '.join(args)}: {e}", file=sys.stderr)
else:
try:
os.waitpid(pid, 0)
except KeyboardInterrupt:
pass
print()
def act_cli_exit(priv, args):
raise EOFError
def act_show_interfaces(priv, args):
print(check_output(["ifconfig"], text=True))
def act_show_interfaces_interface(priv, args):
print(check_output(["ifconfig", args.pop()], text=True))
def act_show_system(priv, args):
print(check_output(["uname", "-a"], text=True))
def act_show_system_version(priv, args):
os = platform.system()
if os == "Darwin":
print(check_output(["sw_vers"], text=True))
elif os == "Linux":
print(check_output(["lsb_release", "-a"], text=True))
def act_show_ip_route(priv, args):
os = platform.system()
if os == "Darwin":
print(check_output(["netstat", "-rnfinet"], text=True))
elif os == "Linux":
print(check_output(["ip", "route", "show"], text=True))
def act_ping(priv, args: list[str]):
cmd = ["ping"]
option_idx = []
for idx, key in enumerate(args):
if key == "count":
cmd += ["-c", args[idx + 1]]
option_idx += [idx, idx + 1]
elif key == "wait":
cmd += ["-W", args[idx + 1]]
option_idx += [idx, idx + 1]
for i in sorted(option_idx, reverse=True):
args.pop(i) # remove ping options from args
target = args.pop()
if target == "ping":
# All options removed, then the text is 'ping'. this means
# <targe> is not specified.
print("<target> must be specified")
return
cmd.append(target)
fork_and_exec(cmd)
def act_print_args(priv, args):
print(f"execute command for: {args}")
def main():
cli = CLI(prompt_cb=prompt_cb)
show_tokens = {
"class": TextToken,
"text": "show",
"desc": "Show information",
"leaves": [
{
"class": TextToken,
"text": "interface",
"desc": "Show interface information",
"action": act_show_interfaces,
"leaves": [
{
"class": InterfaceToken,
"action": act_show_interfaces_interface,
}
],
},
{
"class": TextToken,
"text": "system",
"desc": "Show system information",
"action": act_show_system,
},
{
"class": TextToken,
"text": "ip",
"desc": "Show ip information",
"leaves": [
{
"text": "route",
"desc": "Show ip route information",
"class": TextToken,
"action": act_show_ip_route,
},
],
},
],
}
show_tokens = nosh.instantiate(show_tokens)
cli.append(show_tokens)
show_system_version = TextToken(
text="version", desc="Show system version", action=act_show_system_version
)
cli.insert(["show", "system"], show_system_version)
set_tokens = {
"class": TextToken,
"text": "set",
"desc": "Set parameters",
"leaves": [
{
"class": TextToken,
"text": "interfaces",
"desc": "Set interface parameters",
"leaves": [
{
"class": InterfaceToken,
"leaves": [
{
"class": TextToken,
"text": "address",
"desc": "IP address for this interface",
"leaves": [
{
"class": InterfaceAddressToken,
"action": act_print_args,
}
],
},
{
"class": TextToken,
"text": "mtu",
"desc": "MTU for this interface",
"leaves": [
{
"class": IntToken,
"mark": "<mtu>",
"desc": "MTU value",
"action": act_print_args,
}
],
},
],
}
],
},
{
"class": TextToken,
"text": "route-map",
"desc": "Set a route-map",
"leaves": [
{
"class": StringToken,
"mark": "<route-map>",
"desc": "Name to identify a route-map",
"action": act_print_args,
}
],
},
{
"class": TextToken,
"text": "router-id",
"desc": "Set router-id",
"leaves": [
{
"class": IPv4AddressToken,
"mark": "<router-id>",
"desc": "Router Identifier",
"action": act_print_args,
}
],
},
],
}
cli.append(nosh.instantiate(set_tokens))
# ping command
pn = TextToken(text="ping", desc="Ping remote target")
ping_count_token = {
"class": TextToken,
"text": "count",
"desc": "Number of ping requests",
"leaves": [
{
"class": IntToken,
"mark": "<Number>",
"desc": "Number of ping requests",
"action": act_ping,
}
],
}
cn = nosh.instantiate(ping_count_token)
ping_wait_token = {
"class": TextToken,
"text": "wait",
"desc": "Wait time for ping response",
"action": act_ping,
"leaves": [
{
"class": IntToken,
"mark": "<Second>",
"desc": "Seconds for waiting ping response",
"action": act_ping,
}
],
}
wn = nosh.instantiate(ping_wait_token)
ping_target_token = {
"class": StringToken,
"mark": "<target>",
"desc": "Ping target",
"action": act_ping,
}
tn = nosh.instantiate(ping_target_token)
cli.append(pn)
pn.append(tn, cn, wn)
tn.append(cn, wn)
cli.insert(["ping", "count", IntToken], wn, tn)
cli.insert(["ping", "wait", IntToken], cn, tn)
# exit command
cli.append(TextToken(text="exit", desc="Exit from CLI", action=act_cli_exit))
cli.append(TextToken(text="quit", desc="Exit from CLI", action=act_cli_exit))
cli.cli()
if __name__ == "__main__":
main()