1
+ import re
2
+ import sys
1
3
import click
2
4
import subprocess
5
+ from shlex import join
3
6
4
7
def run_command (command , pager = False ):
5
- click .echo (click .style ("Command: " , fg = 'cyan' ) + click .style (command , fg = 'green' ))
6
- p = subprocess .Popen (command , shell = True , text = True , stdout = subprocess .PIPE )
8
+ command_str = join (command )
9
+ click .echo (click .style ("Command: " , fg = 'cyan' ) + click .style (command_str , fg = 'green' ))
10
+ p = subprocess .Popen (command , text = True , stdout = subprocess .PIPE )
7
11
output = p .stdout .read ()
8
12
if pager :
9
13
click .echo_via_pager (output )
@@ -21,8 +25,8 @@ def cli():
21
25
"""SONiC command line - 'debug' command"""
22
26
pass
23
27
24
-
25
- p = subprocess .check_output ([" sudo vtysh -c 'show version'" ], shell = True , text = True )
28
+ prefix_pattern = '^[A-Za-z0-9.:/]*$'
29
+ p = subprocess .check_output ([' sudo' , ' vtysh' , '-c' , 'show version' ] , text = True )
26
30
if 'FRRouting' in p :
27
31
#
28
32
# 'bgp' group for FRR ###
@@ -35,89 +39,90 @@ def bgp():
35
39
@bgp .command ('allow-martians' )
36
40
def allow_martians ():
37
41
"""BGP allow martian next hops"""
38
- command = 'sudo vtysh -c "debug bgp allow-martians"'
42
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp allow-martians" ]
39
43
run_command (command )
40
44
41
45
@bgp .command ()
42
46
@click .argument ('additional' , type = click .Choice (['segment' ]), required = False )
43
47
def as4 (additional ):
44
48
"""BGP AS4 actions"""
45
- command = 'sudo vtysh -c "debug bgp as4'
49
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp as4" ]
46
50
if additional is not None :
47
- command += " segment"
48
- command += '"'
51
+ command [- 1 ] += " segment"
49
52
run_command (command )
50
53
51
54
@bgp .command ()
52
55
@click .argument ('prefix' , required = True )
53
56
def bestpath (prefix ):
54
57
"""BGP bestpath"""
55
- command = 'sudo vtysh -c "debug bgp bestpath %s"' % prefix
58
+ if not re .match (prefix_pattern , prefix ):
59
+ sys .exit ('Prefix contains only number, alphabet, period, colon, and forward slash' )
60
+ command = ['sudo' , 'vtysh' , '-c' , "debug bgp bestpath %s" % prefix ]
56
61
run_command (command )
57
62
58
63
@bgp .command ()
59
64
@click .argument ('prefix_or_iface' , required = False )
60
65
def keepalives (prefix_or_iface ):
61
66
"""BGP Neighbor Keepalives"""
62
- command = 'sudo vtysh -c "debug bgp keepalives'
67
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp keepalives" ]
63
68
if prefix_or_iface is not None :
64
- command += " " + prefix_or_iface
65
- command += '"'
69
+ command [- 1 ] += ' ' + prefix_or_iface
66
70
run_command (command )
67
71
68
72
@bgp .command ('neighbor-events' )
69
73
@click .argument ('prefix_or_iface' , required = False )
70
74
def neighbor_events (prefix_or_iface ):
71
75
"""BGP Neighbor Events"""
72
- command = 'sudo vtysh -c "debug bgp neighbor-events'
76
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp neighbor-events" ]
73
77
if prefix_or_iface is not None :
74
- command += " " + prefix_or_iface
75
- command += '"'
78
+ command [- 1 ] += ' ' + prefix_or_iface
76
79
run_command (command )
77
80
78
81
@bgp .command ()
79
82
def nht ():
80
83
"""BGP nexthop tracking events"""
81
- command = 'sudo vtysh -c "debug bgp nht"'
84
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp nht" ]
82
85
run_command (command )
83
86
84
87
@bgp .command ()
85
88
@click .argument ('additional' , type = click .Choice (['error' ]), required = False )
86
89
def pbr (additional ):
87
90
"""BGP policy based routing"""
88
- command = 'sudo vtysh -c "debug bgp pbr'
91
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp pbr" ]
89
92
if additional is not None :
90
- command += " error"
91
- command += '"'
93
+ command [- 1 ] += " error"
92
94
run_command (command )
93
95
94
96
@bgp .command ('update-groups' )
95
97
def update_groups ():
96
98
"""BGP update-groups"""
97
- command = 'sudo vtysh -c "debug bgp update-groups"'
99
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp update-groups" ]
98
100
run_command (command )
99
101
100
102
@bgp .command ()
101
103
@click .argument ('direction' , type = click .Choice (['in' , 'out' , 'prefix' ]), required = False )
102
104
@click .argument ('prefix' , required = False )
103
105
def updates (direction , prefix ):
104
106
"""BGP updates"""
105
- command = 'sudo vtysh -c "debug bgp updates'
107
+ bgp_cmd = "debug bgp updates"
106
108
if direction is not None :
107
- command += " " + direction
109
+ bgp_cmd += ' ' + direction
108
110
if prefix is not None :
109
- command += " " + prefix
110
- command += '"'
111
+ if not re .match (prefix_pattern , prefix ):
112
+ sys .exit ('Prefix contains only number, alphabet, period, colon, and forward slash' )
113
+ bgp_cmd += ' ' + prefix
114
+ command = ['sudo' , 'vtysh' , '-c' , bgp_cmd ]
111
115
run_command (command )
112
116
113
117
@bgp .command ()
114
118
@click .argument ('prefix' , required = False )
115
119
def zebra (prefix ):
116
120
"""BGP Zebra messages"""
117
- command = 'sudo vtysh -c "debug bgp zebra'
121
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp zebra" ]
118
122
if prefix is not None :
119
- command += " prefix " + prefix
120
- command += '"'
123
+ if not re .match (prefix_pattern , prefix ):
124
+ sys .exit ('Prefix contains only number, alphabet, period, colon, and forward slash' )
125
+ command [- 1 ] += " prefix " + prefix
121
126
run_command (command )
122
127
123
128
#
@@ -132,56 +137,54 @@ def zebra():
132
137
@click .argument ('detailed' , type = click .Choice (['detailed' ]), required = False )
133
138
def dplane (detailed ):
134
139
"""Debug zebra dataplane events"""
135
- command = 'sudo vtysh -c "debug zebra dplane'
140
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra dplane" ]
136
141
if detailed is not None :
137
- command += " detailed"
138
- command += '"'
142
+ command [- 1 ] += " detailed"
139
143
run_command (command )
140
144
141
145
@zebra .command ()
142
146
def events ():
143
147
"""Debug option set for zebra events"""
144
- command = 'sudo vtysh -c "debug zebra events"'
148
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra events" ]
145
149
run_command (command )
146
150
147
151
@zebra .command ()
148
152
def fpm ():
149
153
"""Debug zebra FPM events"""
150
- command = 'sudo vtysh -c "debug zebra fpm"'
154
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra fpm" ]
151
155
run_command (command )
152
156
153
157
@zebra .command ()
154
158
def kernel ():
155
159
"""Debug option set for zebra between kernel interface"""
156
- command = 'sudo vtysh -c "debug zebra kernel"'
160
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra kernel" ]
157
161
run_command (command )
158
162
159
163
@zebra .command ()
160
164
def nht ():
161
165
"""Debug option set for zebra next hop tracking"""
162
- command = 'sudo vtysh -c "debug zebra nht"'
166
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra nht" ]
163
167
run_command (command )
164
168
165
169
@zebra .command ()
166
170
def packet ():
167
171
"""Debug option set for zebra packet"""
168
- command = 'sudo vtysh -c "debug zebra packet"'
172
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra packet" ]
169
173
run_command (command )
170
174
171
175
@zebra .command ()
172
176
@click .argument ('detailed' , type = click .Choice (['detailed' ]), required = False )
173
177
def rib (detailed ):
174
178
"""Debug RIB events"""
175
- command = 'sudo vtysh -c "debug zebra rib'
179
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra rib" ]
176
180
if detailed is not None :
177
- command += " detailed"
178
- command += '"'
181
+ command [- 1 ] += " detailed"
179
182
run_command (command )
180
183
181
184
@zebra .command ()
182
185
def vxlan ():
183
186
"""Debug option set for zebra VxLAN (EVPN)"""
184
- command = 'sudo vtysh -c "debug zebra vxlan"'
187
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra vxlan" ]
185
188
run_command (command )
186
189
187
190
else :
@@ -193,49 +196,49 @@ def vxlan():
193
196
def bgp (ctx ):
194
197
"""debug bgp on"""
195
198
if ctx .invoked_subcommand is None :
196
- command = 'sudo vtysh -c "debug bgp"'
199
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp" ]
197
200
run_command (command )
198
201
199
202
@bgp .command ()
200
203
def events ():
201
204
"""debug bgp events on"""
202
- command = 'sudo vtysh -c "debug bgp events"'
205
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp events" ]
203
206
run_command (command )
204
207
205
208
@bgp .command ()
206
209
def updates ():
207
210
"""debug bgp updates on"""
208
- command = 'sudo vtysh -c "debug bgp updates"'
211
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp updates" ]
209
212
run_command (command )
210
213
211
214
@bgp .command ()
212
215
def as4 ():
213
216
"""debug bgp as4 actions on"""
214
- command = 'sudo vtysh -c "debug bgp as4"'
217
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp as4" ]
215
218
run_command (command )
216
219
217
220
@bgp .command ()
218
221
def filters ():
219
222
"""debug bgp filters on"""
220
- command = 'sudo vtysh -c "debug bgp filters"'
223
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp filters" ]
221
224
run_command (command )
222
225
223
226
@bgp .command ()
224
227
def fsm ():
225
228
"""debug bgp finite state machine on"""
226
- command = 'sudo vtysh -c "debug bgp fsm"'
229
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp fsm" ]
227
230
run_command (command )
228
231
229
232
@bgp .command ()
230
233
def keepalives ():
231
234
"""debug bgp keepalives on"""
232
- command = 'sudo vtysh -c "debug bgp keepalives"'
235
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp keepalives" ]
233
236
run_command (command )
234
237
235
238
@bgp .command ()
236
239
def zebra ():
237
240
"""debug bgp zebra messages on"""
238
- command = 'sudo vtysh -c "debug bgp zebra"'
241
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug bgp zebra" ]
239
242
run_command (command )
240
243
241
244
#
@@ -248,32 +251,31 @@ def zebra():
248
251
249
252
@zebra .command ()
250
253
def events ():
251
- """debug option set for zebra events"""
252
- command = 'sudo vtysh -c "debug zebra events"'
254
+ command = ['sudo' , 'vtysh' , '-c' , "debug zebra events" ]
253
255
run_command (command )
254
256
255
257
@zebra .command ()
256
258
def fpm ():
257
259
"""debug zebra FPM events"""
258
- command = 'sudo vtysh -c "debug zebra fpm"'
260
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra fpm" ]
259
261
run_command (command )
260
262
261
263
@zebra .command ()
262
264
def kernel ():
263
265
"""debug option set for zebra between kernel interface"""
264
- command = 'sudo vtysh -c "debug zebra kernel"'
266
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra kernel" ]
265
267
run_command (command )
266
268
267
269
@zebra .command ()
268
270
def packet ():
269
271
"""debug option set for zebra packet"""
270
- command = 'sudo vtysh -c "debug zebra packet"'
272
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra packet" ]
271
273
run_command (command )
272
274
273
275
@zebra .command ()
274
276
def rib ():
275
277
"""debug RIB events"""
276
- command = 'sudo vtysh -c "debug zebra rib"'
278
+ command = [ 'sudo' , ' vtysh' , '-c' , "debug zebra rib" ]
277
279
run_command (command )
278
280
279
281
0 commit comments