diff --git a/clear/main.py b/clear/main.py index 6a4d39a278..91cbf7a874 100755 --- a/clear/main.py +++ b/clear/main.py @@ -250,8 +250,8 @@ def clear_vlan_fdb(vlanid): @click.argument('linenum') def line(linenum): """Clear preexisting connection to line""" - # TODO: Stub - return + cmd = "consutil clear " + str(linenum) + run_command(cmd) if __name__ == '__main__': cli() diff --git a/consutil/lib.py b/consutil/lib.py index 2a15c05e31..0617acf5eb 100644 --- a/consutil/lib.py +++ b/consutil/lib.py @@ -20,7 +20,7 @@ # runs command, exit if stderr is written to, returns stdout otherwise # input: cmd (str), output: output of cmd (str) -def popenWrapper(cmd): +def run_command(cmd): proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) output = proc.stdout.read() error = proc.stderr.read() @@ -40,7 +40,7 @@ def checkDevice(linenum): # returns a sorted list of all devices (whose name matches DEVICE_PREFIX) def getAllDevices(): cmd = "ls " + DEVICE_PREFIX + "*" - output = popenWrapper(cmd) + output = run_command(cmd) devices = output.split('\n') devices = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, devices)) @@ -52,7 +52,7 @@ def getAllDevices(): # maps line number to (pid, process start time) def getBusyDevices(): cmd = 'ps -eo pid,lstart,cmd | grep -E "(mini|pico)com"' - output = popenWrapper(cmd) + output = run_command(cmd) processes = output.split('\n') # matches any number of spaces then any number of digits @@ -80,7 +80,7 @@ def getBusyDevices(): def getBaud(linenum): checkDevice(linenum) cmd = "sudo stty -F " + DEVICE_PREFIX + str(linenum) - output = popenWrapper(cmd) + output = run_command(cmd) match = re.match(r"^speed (\d+) baud;", output) if match != None: diff --git a/consutil/main.py b/consutil/main.py index 995b76247a..8fc1970aad 100644 --- a/consutil/main.py +++ b/consutil/main.py @@ -31,7 +31,17 @@ def line(): @click.argument('linenum') def clear(linenum): """Clear preexisting connection to line""" - click.echo("clear line linenum") + checkDevice(linenum) + linenum = str(linenum) + + busyDevices = getBusyDevices() + if linenum in busyDevices: + pid, _ = busyDevices[linenum] + cmd = "sudo kill -SIGTERM " + pid + click.echo("Sending SIGTERM to process " + pid) + run_command(cmd) + else: + click.echo("No process is connected to line " + linenum) # 'connect' subcommand @consutil.command()