Connect to a host via SSH and go to another with ssh - Python tunnel

Asked

Viewed 373 times

2

The code below (main.py) provides access to a host through SSH and creates a tunnel for the IP to be accessed by 127.0.0.1:

import paramiko
from sshtunnel import SSHTunnelForwarder
from paramiko import SSHClient

class SSH:
    def __init__(self):
        self.ssh = SSHClient()
        self.ssh.load_system_host_keys()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(hostname='127.0.0.1',port='22',username='teste',password='teste')

        def exec_cmd(self,cmd):
            stdin,stdout,stderr = self.ssh.exec_command(cmd)
            if stderr.channel.recv_exit_status() != 0:
                print (stderr.read())
            else:
                print (stdout.read())


server = SSHTunnelForwarder(
    '192.168.1.1',
    ssh_username="teste2",
    ssh_password="teste2",
    remote_bind_address=('127.0.0.1', 22),
    local_bind_address=('0.0.0.0', 10022)
)

server.start()

if __name__ == '__main__':
    ssh = SSH()
    stdin,stdout,stderr = ssh.ssh.exec_command("hostname")
    retorno = stdout.read()
    print (retorno)

server.stop()

I am trying to access a specific machine but for this, I have to access a machine initially for network reasons.

The topology accessed by Putty is: access the initial connection (the one for which you created the network rules) and create an SSH tunnel for the IP you want. With this accessing again Putty informing IP 127.0.0.1 plus the port of your tunnel will reach the desired machine.

Basically initial access and go to another machine inside the network

----------------------------------------------------------------------
                            |
-------------+              |    +----------+               +---------+
      HOST   |              |    |   HOST   |               |   HOST  |
    MAIN.PY  | -- SSH ----> |    |  INITIAL | -- TUNNEL --> |  WANTED |
-------------+              |    +----------+               +---------+
                            |
----------------------------------------------------------------------

As stated in the above code, I can only access the first host. Can anyone help me?

  • 1

    the SSH tunnel concept does not work as you imagine, when you create a tunnel via SSH when you log via ssh on the server, if you have a tunnel configured for example in the Puty client, you could access the port configured in the tunnel as if it were a local port, what you want is to access one OS to gain access to another OS

  • Perfect Ederwander, just what I want. Access an OS to gain access to another OS for network reasons. But I wanted to do it in python

  • 1

    then right the SSHTunnelForwarder doesn’t do what you want ... boy, I even imagined a way here to do what you need, but it’s too gambiarra huahuahua

  • Gee, I thought I could do it. So kk, I thought I’d plug into the first OS and send him SSH commands.. But it’s gambit..

  • 1

    is well that way it will be your way, only that there is no way you in theory to enter the password via python to be sent to the second OS, you must make a trust host ssh between the two OS’s, so you can log from the first OS using SSH to the second without typing the second SSH password, then just send the ssh commands from the first pro OS Second ssh usuariotrust@servidor 'ls -l'

  • I kind of have this kk gambiarra in mind. But I have a question, in case I connect in the first OS, is there any way I can open a tunnel to the other OS right?. I use a ready-made automation platform and she makes the same idea, initiates the connection and uses the connection to open a tunnel to the OS I want. Only now I want to do it in python..

  • 1

    tunnel in SSH is not that here what you imagine it to be, in fact the only way is that you log yourself in the first SO and have the first SO send SSH commands to the second SO, for each command it opens an SSH and closes (only opens and closes each command in the second OS, in the first vc gets the active connection always via pytho) ... this platform must have a user with trust key allowing some user to send commands to the second OS without having to enter password ...

  • Got it, so in case I would open a normal SSH connection, and send remote SSH commands to the other OS to get access right? Could you shed some light on how I do it in python? kk

Show 3 more comments

1 answer

2


try to make the following change:

    server = SSHTunnelForwarder(
    '192.168.1.1',
    ssh_username="teste2",
    ssh_password="teste2",
    remote_bind_address=('<IP do HOST Destino>', 22),
    local_bind_address=('127.0.0.1', 10022) # Aqui você deixa o ip de loopback
)

I hope I’ve helped

  • Perfect Leandro, it worked. Thank you so much for your help.

  • 1

    You specify the password of the host that created the tunnel here: self.ssh.connect(hostname='127.0.0.1',port='10022',username='root',password='root')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.