Skip to content

Commit

Permalink
[connect][sonic-clear] Align the exit code with consutil for line com…
Browse files Browse the repository at this point in the history
…mands (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 jika@microsoft.com
  • Loading branch information
Blueve authored Nov 26, 2020
1 parent cfb7a22 commit 8f3b22e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
10 changes: 7 additions & 3 deletions clear/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import configparser
import os
import subprocess
import sys

import click

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 ...")
Expand Down
7 changes: 5 additions & 2 deletions connect/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import configparser
import os
import pexpect
import sys

import click

Expand Down Expand Up @@ -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', '-?'])

Expand All @@ -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")
Expand All @@ -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()
18 changes: 18 additions & 0 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <devicename>
```

The command is same with `connect line --devicename <devicename>`

- Example:
```
admin@sonic:~$ connect line 1
Successful connection to line 1
Press ^A ^X to disconnect
```

### Console clear commands

**sonic-clear line**
Expand Down

0 comments on commit 8f3b22e

Please sign in to comment.