Keep alive mechanism for ssh over ssh in Paramiko - python

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.

Related

How to re-conect a remote conection trough SSH in VSCode?

I am having an issue with my remote SSH connection in VSCode. When I try to connect, it appears to be working normally, but after a short time it becomes disconnected and I need to reconnect. I have tried to look for a solution on how to reconnect the remote connection in VSCode, but so far I have not been successful. I would like to be able to reconnect to the remote connection in order to continue working on my processes without having to restart them. I have checked my ssh_config file, but I am not sure if there is a setting I need to change in order to reconnect to the remote connection. I would like to post this issue on StackOverflow to see if there is a solution that can help me reconnect to the remote connection in VSCode. I've checked this post but the "Reaload" functiĆ³n will throw all my variables and procecess. Can someone help me please? Or at least tell me "there is not a solutiĆ³n, just reload an change your ssh config file like the previous post".

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).

How to connect to ssh server from another server

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

socket.getaddrinfo fails if network started offline

I'm having a problem with a Python script which should check if the user is connected to a wifi network with a captive portal. Specifically, the script is long-running, attempting to connect to example.org every 60 seconds.
The problem is that if the network starts offline (meaning the wifi isn't connected at the start of the script), socket.getaddrinfo will always fail with the error "Name or service not known", even once the wifi is connected, until the Python script is restarted. (This isn't a DNS thing -- all requests fail.)
Because both urllib and requests use sockets, it's totally impossible to download an example page once Python gets into this state.
Is there a way around this or a way to reset sockets so it works properly once the network fails?
To be clear, here's a repro:
Disconnect wifi
Run an interactive Python session
import urllib and urllib.open("http://stackoverflow.com/") -- fails as expected
Reconnect wifi
urllib.open("http://example.com/")
Expected: Returned HTML from example.com
Actual: socket.gaierror: [Errno -2] Name or service not known
If you're not connected to an access point when running the script, and don't have an IP address assigned to your device socket.getaddrinfo will fail. Maybe it's still connecting when you run the script. The domain name cannot be resolved because you are not connected to the network, thus no DNS.
Does it fail when you're actually connected to the network? Does curl http://icanhazip.com work at the point when the script fails? Or if you run ifconfig does your device have an IP? (I'm assuming you're on a *nix box).

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.

Categories