I am trying to get the output from a command using this code
import time
import paramiko
host = "myserver.com"
username = "user"
password = "pass"
ssh = paramiko.SSHClient()
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=username, password=password)
commands = ["sudo su - abc", "mkdir foo"]
channel = ssh.invoke_shell()
time.sleep(1)
channel.recv(9999)
time.sleep(1)
for command in commands:
channel.send(command + "\n")
while not channel.recv_ready():
time.sleep(0.1)
time.sleep(0.1)
output = channel.recv(9999)
print(output.decode("utf-8"))
time.sleep(0.1)
channel.close()
I am using invoke_shell because I need to run commands using another user in a session, but when I run the commands the output is different from exec_command()
exec_command() show the output from the command, while channel.recv(9999) shows the output from the terminal.
Is there a way to get the output from the command as exec_command() provides? Thank you for the attention.