How to connect to ssh server from another server - python

I've been trying to connect to first an out-of-band management server (in my case, since I'm connecting to DELL-servers, an iDRAC) and through that connect to the main server itself. I've got it to work when I do it manually, using, in the (windows) terminal:
putty.exe -ssh 'username'#'iDRAC-IP'
followed by PuTTY window opening where I type in the password, followed by
connect
which connects to the server itself, and then I type in the username and password for the server, completing the process.
When I've been writing my script in python, I'm using paramiko, http://www.paramiko.org/, suggested here on stackoverflow, and following this example: https://www.ivankrizsan.se/2016/04/24/execute-shell-commands-over-ssh-using-python-and-paramiko/, and it works just splendid for the iDRAC (the first server I connect to). It also works when I type in
stdin, stdout, stderr = ssh_client.exec_command('connect')
because I am still in my first server (ssh_client) (I can tell this is working because when I try to connect to the server manually afterwards, it is occupied). But after that it stops working, since when doing 'connect' I am no longer in ssh_client, but in a different server.
So my question is - how do I connect to a server from another server (in this case being the out-of-band management server) and log in to this one?

You can use ssh tunnel to do so.
this post may resolve your problem:
PyCharm: Configuring multi-hop remote Interpreters via SSH

Related

Using existing SSH session with Paramiko

Imagine that you have a client and a server and that the client connects to the server using SSH (with or without Paramiko). Is it possible for Paramiko to utilize the already open SSH connection instead of creating a new one, so that another script can be ran on the server (and transfer a file back to the client for example) using the same existing connection? The goal is to avoid establishing multiple connections.
This is not really a programming/Python/Paramiko question. It's not possible with SSH at all. So it's not possible with Python/Paramiko either.
See
Transfer files to/from session I'm logged in with PuTTY or
Can you use SFTP to transfer files "backwards" to client during SSH on host?
Though if you connect with a (Python or whatever) script from the client machine, the remote script can talk to the local script to exchange the data (and the local script can save the data to a file).

Keep alive mechanism for ssh over ssh in Paramiko

I'm using Paramiko interactive shell (invoke_shell() method) for opening session between my local host and remote host and sending commands to it.
Sometimes I need to open from my remote host an ssh connection to another remote host (this is like a bridge or SSH via SSH from local host to remote_host_2 via remote_host_1).
For better understanding this is a picture how it should behave:
| local host | -(1)-> | remote host | -(2)-> | destination host |
1: is open using invoke_shell() paramiko method so it will create some paramiko.Channel obj.
2: is open using Linux command i.e. ssh remote_user#remote_ip
This is done this way because sometimes I have no direct connection to destination host and in case you are opening it via remote host then user decision should be made (entering another password, answering yes/no on some questions etc..)
Now my question is regarding keeping my connection alive.
I read on keep_alive mechanism that paramiko has but it doesn't do what I want because paramiko knows my connection ends in remote_host and not in destination host so in case destination host is dead I will not receive any notification regarding it and commands execution will fail.
The only solution that came to my mind is sending empty command on this channel (\n) and trying to read the output from the channel before executing the desirable command on it. but this means that I can affect my channel on one hand and my command execution time is now twice longer.
Now my question is, is there another way to perform this connection so this keep alive mechanism will work?
p.s. I read that there exists some ServerAliveInterval=30 flag that can help me to keep my ssh interactive connection alive but I don't understand how can I use it to validate it doesn't became dead.
The correct way is to implement the jump using port forwarding.
See Connecting to a server via another server using Paramiko
Then you will have a complete control over both connections and you will be able to use native Paramiko features to keep the connection alive and to check its status.

Execute python code over SSH

I need to write a python script which connects to the host over SSH and then somehow connects to a service sitting on localhost and performs a little interactive session.
What first came to mind is to use Paramiko to do a local port forwarding and then use Pythons's sockets library to communicate with the service.
But working with Paramiko was quite a challenge and I haven't figured out how to fix some issues.
So I switched to pxssh and used just simple scenario:
conn.sendline('telnet {} {}'.format('localhost', port)
conn.expect('PASSWORD:')
conn.sendline(password)
...
But that telnet thing really bothers me.
And I think it's possible to establish SSH connection in such a manner that from Python's code prospective I just do data = open('somefile').read() which actually opens a somefile on a remote host and all traffic is being encrypted because of SSH.

Differentiate between SSH connection and authentication in Bash / Python

This isn't tied to a specific code sample that I have, but many of them. I'm using Python Fabric to make connections to various servers over SSH from a single host that I'm running my script on. Sometimes it connects just fine and that is the end of it. However, sometimes it can make a connection but gives me a "[serverName] Login password for 'userUser'" error. I don't have access to this server, so even though the connection is being made, I can't authenticate to it. Is there any way to make Bash / Fab / Python realize that I can't connect?
I would investigate the following ssh_config options:
KbdInteractiveAuthentication no
PasswordAuthentication no
See man ssh_config for more info on these options.
You can set these options in your ~/.ssh/config file or on the ssh command line using -o. I imagine using the ~/.ssh/config file would be transparent to Fabric.

Call linux ssh client with password through paramico connection

I am trying to connect to a Linux box using paramiko and from that session execute an ssh session to establish a reverse tunnel to another machine.
Using keyfiles works fine, but it's not an actual option, and connecting to the other box to establish the connection manually also won't be possible.
I am able to connect to the machine and execute non-interactive commands, and have been successful in executing interactive commands as well. I am however unable to "trick" ssh into working interactively this way, by using the example found here
Any feedback will be appreciated.

Categories