import paramiko
import time
hostname = "myserver.com"
username = input ('Enter Username:' )
password = input ('Enter Password:' )
command = "'sudo -u anotheruser -i'; 'whoami'"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect (hostname,
port=22,username=username,
password=password,
look_for_keys=False,
allow_agent=False)
stdin, stdout, stderr = ssh.exec_command(command=command,get_pty=True)
print ('Command Sent:')
stdin.write("mysudopassoword\n")
print ("Password Sent")
stdin.flush()
time.sleep(2)
print(f'STDOUT:{stdout.read().decode("utf8")}')
print(f'STDERR:{stderr.read().decode("utf8")}')
if stderr.channel.recv_exit_status() != 0:
print (f"Following error occured:{stderr.readlines()}")
else:
print ("All good")
stdin.close()
stdout.close()
stderr.close()
ssh.close
I am trying to run the commands on remote server logging as a different user. The above code points password sent but it hangs in there forever.