Skip to content

Instantly share code, notes, and snippets.

@jerryan999
Created November 22, 2018 03:27
Show Gist options
  • Save jerryan999/8c3fcf998ced78a7fce135ca603fa41e to your computer and use it in GitHub Desktop.
Save jerryan999/8c3fcf998ced78a7fce135ca603fa41e to your computer and use it in GitHub Desktop.
import os
from sshtunnel import SSHTunnelForwarder
def connect_tunnel():
local_port = 4599
os.system("kill $( lsof -i:{} -t )".format(local_port))
with SSHTunnelForwarder(
('34.230.115.45', 22), # Remote server ip and port
ssh_username="jerry",
ssh_pkey="/Users/jerry/.ssh/id_ali.rsa",
ssh_private_key_password="aliyun",
remote_bind_address=('127.0.0.1', 27017),
local_bind_address=('0.0.0.0', local_port),
set_keepalive = 3
) as server:
server.start() #start ssh server
print('Server connected via SSH')
# Connect to PostgreSQL
local_port = str(server.local_bind_port)
return local_port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment