Skip to content

Instantly share code, notes, and snippets.

@bitprophet

bitprophet/ssh_config.patch Secret

Created Aug 20, 2011
Embed
What would you like to do?
diff --git a/fabric/network.py b/fabric/network.py
index 73a8f75..3181867 100644
--- a/fabric/network.py
+++ b/fabric/network.py
@@ -8,6 +8,7 @@ import re
import threading
import socket
import sys
+import os
from fabric.utils import abort
@@ -82,10 +83,21 @@ def normalize(host_string, omit_port=False):
return ('', '') if omit_port else ('', '', '')
# Get user, host and port separately
r = host_regex.match(host_string).groupdict()
- # Add any necessary defaults in
- user = r['user'] or env.get('user')
- host = r['host']
- port = r['port'] or '22'
+ if not env.get('use_ssh_config'):
+ # Add any necessary defaults in
+ user = r['user'] or env.get('user')
+ host = r['host']
+ port = r['port'] or '22'
+ elif os.path.exists(env.get('local_ssh_config')):
+ ssh_config = ssh.SSHConfig()
+ ssh_config.parse(open(env['local_ssh_config']))
+ _ssh_config = ssh_config.lookup(r['host'])
+ user = r['user'] or _ssh_config.get('user') or env.get('user')
+ if not _ssh_config.get('hostname'):
+ host = r['host']
+ else:
+ host = _ssh_config.get('hostname')
+ port = r['port'] or _ssh_config.get('port') or '22'
if omit_port:
return user, host
return user, host, port
diff --git a/fabric/state.py b/fabric/state.py
index c1f0f62..3e295f5 100644
--- a/fabric/state.py
+++ b/fabric/state.py
@@ -203,6 +203,14 @@ env_options = [
action='store_true',
default=False,
help="force use of pseudo-terminal in run/sudo"
+ ),
+
+ # Using .ssh/config file
+ make_option('--ssh-config',
+ dest='use_ssh_config',
+ action='store_true',
+ default=False,
+ help='force usage .ssh/config file'
)
]
@@ -240,6 +248,8 @@ env = _AttributeDict({
'use_shell': True,
'user': None,
'version': get_version('short'),
+ 'local_home': os.environ.get('HOME'),
+ 'local_ssh_config': os.path.join(os.environ.get('HOME'), '.ssh/config'),
})
# Add in option defaults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment