I tried using the following script in order to open ssh to a jump-host and from there to make ssh connection to another host. I based my script on the one in this link However I get the following exception:
Traceback (most recent call last):
File "/Users/myusername/pythonProject/script/stackoverflow.py", line 21, in <module>
second_jump.connect(second_jump_add, username='myusername', sock=second_channel)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/client.py", line 426, in connect
self._auth(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/client.py", line 749, in _auth
raise saved_exception
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/client.py", line 725, in _auth
self._transport.auth_publickey(username, key)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/transport.py", line 1507, in auth_publickey
return self.auth_handler.wait_for_response(my_event)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/auth_handler.py", line 250, in wait_for_response
raise e
paramiko.ssh_exception.AuthenticationException: Authentication failed.
Process finished with exit code 1
The manual connection is usually done by opening terminal and typing the following command in order to connect the first jump server: ssh myusername@x.x.x.x
Next, in order to connect the second jump server,I type: ssh myusername@y.y.y.y No use of password whatsoever.
import paramiko
first_jump = paramiko.SSHClient()
first_jump.load_system_host_keys()
first_jump_add = 'x.x.x.x'
second_jump_add = 'y.y.y.y'
try:
first_jump.connect(first_jump_add, username='myusername')
except paramiko.ssh_exception.NoValidConnectionsError:
first_jump.connect(first_jump_add, username='myusername')
finally:
second_jump_transport = first_jump.get_transport()
second_channel = second_jump_transport.open_channel("direct-tcpip", (second_jump_add, 22), (first_jump_add, 22))
second_jump = paramiko.SSHClient()
second_jump.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# qa_jump.load_system_host_keys() (was not sure if this one is needed, but i got the same exception either if I uncomment it.)
second_jump.connect(second_jump_add, username='myusername', sock=second_channel)
sj_stdin, sj_stout, sj_stderr = second_jump.exec_command("echo hello")
print('connected to second-jump')
print(sj_stout.read())
second_jump.close()
first_jump.close()