Skip to content Skip to sidebar Skip to footer

Double Ssh Tunnel Within Python

Today I am using ssh in command line to forward ports from a remote server, using an intermediate server to my local machine. This is the command I am using in shell: ssh user@remo

Solution 1:

You can try this example: https://github.com/pahaz/sshtunnel#example-4

import sshtunnel
import requests
import pandas as pd
from requests.auth import HTTPBasicAuth

with sshtunnel.open_tunnel(
    ssh_address_or_host=('remote_server', 22),
    ssh_username="user",
    remote_bind_address=('intermediate_server', 22),
    block_on_close=False
) as tunnel1:
    print('Connection to tunnel1 (intermediate_server) OK...')
    with sshtunnel.open_tunnel(
        ssh_address_or_host=('127.0.0.1', tunnel1.local_bind_port),
        remote_bind_address=('127.0.0.1', 2443),
        ssh_username='user',
        ssh_password='intermediate_server_pwd',
        block_on_close=False
    ) as tunnel2:
        url = 'https://localhost:'+tunnel2.local_bind_port+'/my_site'
        my_ds = requests.get(url, auth=HTTPBasicAuth('user', 'password'), verify=False)
        print(my_ds.content)

Post a Comment for "Double Ssh Tunnel Within Python"