I want to ssh to remote server and then become its root user to perform some actions like password change. I am looking for a fully automated interactive ssh session with the remote host so that I can pass multiple interactive commands and see step-by-step what is going on. I have the following lines of code:
import paramiko
import time
command1 = "ls"
command2='echo <root password> | sudo su-'
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password)
#Command 1
stdin, stdout,stderr = client.exec_command(command1)
print(stdout.read().decode())
stdin.flush()
#Command 2
stdin,stdout,stderr = client.exec_command(command2)
print(stderr.read().decode())
time.sleep(3)
stdin.flush()
#Command 3
stdin,stdout,stderr = client.exec_command('id')
print(stdout.read().decode())
client.close()
It can successfully ssh and display the 'ls' stdout. However for second command it shows error sudo: no tty present and no askpass program specified
. If I modify using client.exec_command(command2,get_pty=True)
, I have no clue, as it runs indefinitely. Any help please.