I'm using netmiko
to SSH into an Ubuntu machine to run a Python script that takes a few minutes to finish running. The Python script does not write anything to stdout
until just before it has finished running.
However, the current implementation appears to be terminating the SSH connection early thinking that the Python script has already finished running when it has not.
What is a good way to wait till the Python script has completed running (including having crashed with an exception) before closing the SSH connection.
from netmiko import ConnectHandler
import time
device = {
"device_type": "linux",
# ...
}
with ConnectHandler(**device) as ssh:
ssh.write_channel("python slow_task.py\n")
outputs = []
while True:
time.sleep(10)
output = ssh.read_channel()
if not output:
break
print(output.strip())
outputs.append(output)