0

I have executed commands on server using ssh. Now I have to do another ssh to different IP while keeping old ssh active.

This new IP is port forward which will then used to do SFTP.

Issue I am facing is both ssh connections are on same port so not able to do second ssh. Which is failing the SFTP.

Any support for same will be helpful.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=username, password=password, port=22)
time.sleep(3)
#Invoke shell to open multiple channel with in one SSH
chan = ssh.invoke_shell()
chan.send(ip1+'\n')
time.sleep(5)
chan.send(pass1+'\n')
time.sleep(10)

chan.send("ssh "+ip2+'\n')

time.sleep(10)
chan.send(pass2+'\n')
time.sleep(5)
#Execute command
chan.send(cmd)

#connect to another ip to do sftp
ssh1 = paramiko.SSHClient()
ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("127.0.0.4", username=usr2, password=pass2, port=22)
sftp=ssh.open_sftp()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Romita Thakur
  • 233
  • 1
  • 3
  • 8

1 Answers1

1

It looks like misunderstanding. Your code does not do any port forwarding.

For the correct code, see Nested SSH using Python Paramiko.

If you need SFTP, not shell, just do:

jhost.open_sftp()

instead of

jhost.exec_command(command)

Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992