Im trying to connect via SSH (paramiko) on switches and execute commands to backup the config of it. I saw some similar questions and tried a bunch of them but i didnt find a way to continously read the stdout line after an stdin for my code. I only can read the stdout after i shutdown the stdin with stdin.channel.shutdown_write().
My code looks like this:
import paramiko
def line_buffered(f):
line_buf = ""
while not f.channel.exit_status_ready():
line_buf += f.read(1).decode()
if line_buf.endswith('\n'):
yield line_buf
line_buf = ''
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
client.connect("192.168.18.96", username="admin", password="password")
print("SSH Connection completed.")
stdin, stdout, stderr = client.exec_command('')
print("Start executing...")
stdin.write("something\n")
for line in line_buffered(stdout):
print(line)
print("-----------------")
print("finished")
stdin.close()
stdout.close()
stderr.close()
client.close()
Output:
SSH Connection completed.
Start executing...
(M4300-52G) >something
^
% Invalid input detected at '^' marker.
Still missing the finished