Skip to content

Instantly share code, notes, and snippets.

@tbrlpld
Created April 15, 2020 17:38
Show Gist options
  • Save tbrlpld/14e36296b7c667ce480c85a04f0683dd to your computer and use it in GitHub Desktop.
Save tbrlpld/14e36296b7c667ce480c85a04f0683dd to your computer and use it in GitHub Desktop.
Establish ssh tunnel via Python
import os
import time
from sshtunnel import SSHTunnelForwarder
# Set the environment variable that contains the passphrase for the given
# private key file.
KEY_PASSPHRASE = os.environ.get("SSH_PRIVATE_KEY_PASSPHRASE")
server = SSHTunnelForwarder(
("138.68.41.80", 22),
ssh_username="tibor",
ssh_pkey="/Users/tibor/.ssh/id_rsa",
ssh_private_key_password=KEY_PASSPHRASE,
remote_bind_address=("127.0.0.1", 5432),
local_bind_address=("127.0.0.1", 63333),
)
server.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
print("Service canceled with keyboard interrupt.")
server.stop()
break
server.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment