Last active
July 14, 2021 10:07
-
-
Save y3nr1ng/582bcf4b0e75d3591c30af419d4ee059 to your computer and use it in GitHub Desktop.
Paramiko with .ssh/config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
client = paramiko.SSHClient() | |
client._policy = paramiko.WarningPolicy() | |
client.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': options['hostname'], 'username': options["username"]} | |
user_config = ssh_config.lookup(cfg['hostname']) | |
for k in ('hostname', 'username', 'port'): | |
if k in user_config: | |
cfg[k] = user_config[k] | |
if 'proxycommand' in user_config: | |
cfg['sock'] = paramiko.ProxyCommand(user_config['proxycommand']) | |
client.connect(**cfg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment