From 8f3b22ed11e166ed24dcd2fb1ac61e227140c2af Mon Sep 17 00:00:00 2001 From: Blueve <672454911@qq.com> Date: Thu, 26 Nov 2020 15:09:42 +0800 Subject: [PATCH] [connect][sonic-clear] Align the exit code with consutil for line commands (#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 jika@microsoft.com --- clear/main.py | 10 +++++++--- connect/main.py | 7 +++++-- doc/Command-Reference.md | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/clear/main.py b/clear/main.py index a0561ba89203..9d762322069b 100755 --- a/clear/main.py +++ b/clear/main.py @@ -1,6 +1,7 @@ import configparser import os import subprocess +import sys import click @@ -93,11 +94,12 @@ def get_routing_stack(): routing_stack = get_routing_stack() -def run_command(command, pager=False, return_output=False): +def run_command(command, pager=False, return_output=False, return_exitstatus=False): # Provide option for caller function to Process the output. proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) if return_output: - return proc.communicate() + output = proc.communicate() + return output if not return_exitstatus else output + (proc.returncode,) elif pager: #click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green')) click.echo_via_pager(proc.stdout.read()) @@ -367,7 +369,9 @@ def clear_vlan_fdb(vlanid): def line(target, devicename): """Clear preexisting connection to line""" cmd = "consutil clear {}".format("--devicename " if devicename else "") + str(target) - run_command(cmd) + (output, _, exitstatus) = run_command(cmd, return_output=True, return_exitstatus=True) + click.echo(output) + sys.exit(exitstatus) # # 'nat' group ("clear nat ...") diff --git a/connect/main.py b/connect/main.py index 38cd136d7882..52d17a270413 100755 --- a/connect/main.py +++ b/connect/main.py @@ -1,6 +1,7 @@ import configparser import os import pexpect +import sys import click @@ -71,6 +72,8 @@ def run_command(command, display_cmd=False): proc = pexpect.spawn(command) proc.interact() + proc.close() + return proc.exitstatus CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?']) @@ -93,7 +96,7 @@ def connect(): def line(target, devicename): """Connect to line LINENUM via serial connection""" cmd = "consutil connect {}".format("--devicename " if devicename else "") + str(target) - run_command(cmd) + sys.exit(run_command(cmd)) # # 'device' command ("connect device") @@ -103,7 +106,7 @@ def line(target, devicename): def device(devicename): """Connect to device DEVICENAME via serial connection""" cmd = "consutil connect -d " + devicename - run_command(cmd) + sys.exit(run_command(cmd)) if __name__ == '__main__': connect() diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index a2ab1cab16b4..843bb8a59300 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -2052,6 +2052,24 @@ Optionally, you can connect with a remote device name by specifying the `-d` or Press ^A ^X to disconnect ``` +**connect device** + +This command allows user to connect to a remote device via console line with an interactive cli. + +- Usage: + ``` + connect device + ``` + +The command is same with `connect line --devicename ` + +- Example: + ``` + admin@sonic:~$ connect line 1 + Successful connection to line 1 + Press ^A ^X to disconnect + ``` + ### Console clear commands **sonic-clear line**