I want to ssh remotely to B instance from already ssh into A instance.
What are the other libraries or function in python which support my scenario for using/accessing Instance B using already connected to Instance A via localhost? If python doesn't support this what are the other ways to accomplish this?
I have tried paramiko library but I was able to connect to only one instance using:
ssh = paramiko.SSHClient()
key = paramiko.RSAKey.from_private_key_file('<PEM_FILE>.pem')
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=ip, username=username, pkey=key)
And used this command to execute commands in remote instance as I want to execute ssh
command under sudo su
:
stdin, stdout, stderr = client.exec_command('sudo su')
And now I want to ssh into another instance from this remote instance. So I tried basic ssh command:
stdin, stdout, stderr = client.exec_command('ssh -i <PEM_FILE>.pem <username>@<ip>')
But I wasn't able to ssh into B instance.
I have also tried scenario with this command:
os.system("ssh -t <PEM_FILE>.pem usernameA@hostA 'ssh -t <PEM_FILE>.pem usernameB@hostB'")
Expectation: I should be able to run commands in Instance B remotely.