Skip to content

Instantly share code, notes, and snippets.

@tonicebrian
Created December 15, 2015 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tonicebrian/1d2577af55c8320ffd6a to your computer and use it in GitHub Desktop.
Save tonicebrian/1d2577af55c8320ffd6a to your computer and use it in GitHub Desktop.
Paramiko tunnel
# Create a SSH connection
import paramiko
import os
ssh = paramiko.SSHClient()
ssh._policy = paramiko.WarningPolicy()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_config = paramiko.SSHConfig()
user_config_file = os.path.expanduser("~/.ssh/config")
if os.path.exists(user_config_file):
with open(user_config_file) as f:
ssh_config.parse(f)
cfg = {'hostname': environment}
user_config = ssh_config.lookup(cfg['hostname'])
cfg["hostname"] = user_config["hostname"]
cfg["username"] = user_config["user"]
key=paramiko.RSAKey.from_private_key_file("/home/cebrian/.ssh/id_rsa")
cfg["pkey"] = key
if 'proxycommand' in user_config:
cfg['sock'] = paramiko.ProxyCommand(user_config['proxycommand'])
ssh.connect(**cfg)
stdin, stdout, stderr = ssh.exec_command("ls -l /tmp")
print stdout.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment