Skip to content

Commit 8f3b22e

Browse files
authored
[connect][sonic-clear] Align the exit code with consutil for line commands (sonic-net#1256)
* [connect][sonic-clear] Align the exit code with consutil for line commands * Fix comments and update document * Update doc Signed-off-by: Jing Kan [email protected]
1 parent cfb7a22 commit 8f3b22e

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

clear/main.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import configparser
22
import os
33
import subprocess
4+
import sys
45

56
import click
67

@@ -93,11 +94,12 @@ def get_routing_stack():
9394
routing_stack = get_routing_stack()
9495

9596

96-
def run_command(command, pager=False, return_output=False):
97+
def run_command(command, pager=False, return_output=False, return_exitstatus=False):
9798
# Provide option for caller function to Process the output.
9899
proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE)
99100
if return_output:
100-
return proc.communicate()
101+
output = proc.communicate()
102+
return output if not return_exitstatus else output + (proc.returncode,)
101103
elif pager:
102104
#click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
103105
click.echo_via_pager(proc.stdout.read())
@@ -367,7 +369,9 @@ def clear_vlan_fdb(vlanid):
367369
def line(target, devicename):
368370
"""Clear preexisting connection to line"""
369371
cmd = "consutil clear {}".format("--devicename " if devicename else "") + str(target)
370-
run_command(cmd)
372+
(output, _, exitstatus) = run_command(cmd, return_output=True, return_exitstatus=True)
373+
click.echo(output)
374+
sys.exit(exitstatus)
371375

372376
#
373377
# 'nat' group ("clear nat ...")

connect/main.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import configparser
22
import os
33
import pexpect
4+
import sys
45

56
import click
67

@@ -71,6 +72,8 @@ def run_command(command, display_cmd=False):
7172

7273
proc = pexpect.spawn(command)
7374
proc.interact()
75+
proc.close()
76+
return proc.exitstatus
7477

7578
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?'])
7679

@@ -93,7 +96,7 @@ def connect():
9396
def line(target, devicename):
9497
"""Connect to line LINENUM via serial connection"""
9598
cmd = "consutil connect {}".format("--devicename " if devicename else "") + str(target)
96-
run_command(cmd)
99+
sys.exit(run_command(cmd))
97100

98101
#
99102
# 'device' command ("connect device")
@@ -103,7 +106,7 @@ def line(target, devicename):
103106
def device(devicename):
104107
"""Connect to device DEVICENAME via serial connection"""
105108
cmd = "consutil connect -d " + devicename
106-
run_command(cmd)
109+
sys.exit(run_command(cmd))
107110

108111
if __name__ == '__main__':
109112
connect()

doc/Command-Reference.md

+18
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,24 @@ Optionally, you can connect with a remote device name by specifying the `-d` or
20522052
Press ^A ^X to disconnect
20532053
```
20542054
2055+
**connect device**
2056+
2057+
This command allows user to connect to a remote device via console line with an interactive cli.
2058+
2059+
- Usage:
2060+
```
2061+
connect device <devicename>
2062+
```
2063+
2064+
The command is same with `connect line --devicename <devicename>`
2065+
2066+
- Example:
2067+
```
2068+
admin@sonic:~$ connect line 1
2069+
Successful connection to line 1
2070+
Press ^A ^X to disconnect
2071+
```
2072+
20552073
### Console clear commands
20562074
20572075
**sonic-clear line**

0 commit comments

Comments
 (0)