A
A
Alexander2020-08-12 23:27:38
Python
Alexander, 2020-08-12 23:27:38

Local port forwarding with host jumping Python?

Good afternoon!
Help me figure out how to perform port forwarding through multiple hosts in python?
Scheme: [localhost]-[hostA]-[hostB]..[hostN]-[dest_host]
CLI like:

ssh -t -gL 6000:localhost:6000 hostA ssh -t -gL 6000:localhost:6000 hostB ssh -t -gL 6000:dest_host:6000 hostN


Using sshtunnel it turns out to go to hostA , how to open the next tunnel from this tunnel?
Below is test code, move to 1 host, how to move to next ?
from sshtunnel import SSHTunnelForwarder
import requests


with SSHTunnelForwarder(
         ('192.168.1.3', 22),
         ssh_password='password',
         ssh_username='username',
         local_bind_address=('127.0.0.1', 6000),
         remote_bind_address=('dest_host', 6000)  # or localhost если сервис тут же
    ) as server:
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
    r = requests.get('http://127.0.0.1:6000', headers=headers).content
    print(r)


I did not find working examples for sshtunnel and paramiko, basically you need a tunnel to a remote host and then a connection to the database either on localhost or on an external address.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-08-20
@shabelski89

Dear connoisseurs, Sergey Karbivnichiy Sergey Gornostaev Sergey Pankov , can we really pass by?

S
Sergey Pankov, 2020-08-20
@trapwalker

Forward sequentially through all tunnels, except for the last one, not port 6000, but 22. The last tunnel will forward the target port. All connections, starting from the second, must be on localhost, and ssh ports will have to be bound to different local ports.
Look, in the CLI you start ssh every time on a new remote machine. Technically, you can do this on one machine, each time attaching port 22 of the next remote machine to the next local port.
Haven't tried it myself. so far no time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question