Add support for variable expansion in SSHConfig
This commit is contained in:
parent
fb24d79695
commit
31482a46d6
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue