summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOlle Lundberg <geek@nerd.sh>2012-03-30 15:59:58 +0200
committerJeff Forcier <jeff@bitprophet.org>2012-09-23 16:19:31 -0700
commit6b8284640e6b6b99cd68e2ed6bf0e572ed88e58a (patch)
tree5c0b968b8f661cfbf6df7ede8d0a8d7d11df5408
parent697524a79f2ca4e09248e455493d1bc684e63c06 (diff)
Add support for variable expansion in SSHConfig
(cherry picked from commit 31482a46d6c360e00284a7ccfd68362891ab316b)
-rw-r--r--paramiko/config.py49
1 files changed, 45 insertions, 4 deletions
diff --git a/paramiko/config.py b/paramiko/config.py
index d30080ad..75c54b4a 100644
--- a/paramiko/config.py
+++ b/paramiko/config.py
@@ -21,7 +21,10 @@ L{SSHConfig}.
"""
import fnmatch
+import os
+import socket
+SSH_PORT=22
class SSHConfig (object):
"""
@@ -115,18 +118,56 @@ class SSHConfig (object):
Return a dict of config options with expanded substitutions
for a given hostname.
- For the moment only expansion of the %h substitution in the
- hostname config is supported.
+ Please refer to man ssh_config(5) for the parameters that
+ are replaced.
@param config: the config for the hostname
@type hostname: dict
@param hostname: the hostname that the config belongs to
@type hostname: str
"""
- #TODO: Add support for expansion of all substitution parameters
- #TODO: see man ssh_config(5)
+
if 'hostname' in config:
config['hostname'] = config['hostname'].replace('%h',hostname)
else:
config['hostname'] = hostname
+
+ if 'port' in config:
+ port = config['port']
+ else:
+ port = SSH_PORT
+
+ user = os.getenv('USER')
+ if 'user' in config:
+ remoteuser = config['user']
+ else:
+ remoteuser = user
+
+ host = socket.gethostname().split('.')[0]
+ fqdn = socket.getfqdn()
+ homedir = os.path.expanduser('~')
+ replacements = {'controlpath' :
+ [
+ ('%h', config['hostname']),
+ ('%l', fqdn),
+ ('%L', host),
+ ('%n', hostname),
+ ('%p', port),
+ ('%r', remoteuser),
+ ('%u', user)
+ ],
+ 'identityfile' :
+ [
+ ('~', homedir),
+ ('%d', homedir),
+ ('%h', config['hostname']),
+ ('%l', fqdn),
+ ('%u', user),
+ ('%r', remoteuser)
+ ]
+ }
+ for k in config:
+ if k in replacements:
+ for find, replace in replacements[k]:
+ config[k] = config[k].replace(find, str(replace))
return config