Fix #33 - parse config as described by manpage

(cherry picked from commit 011805eae07ee7be6140b95f6d8669763c55b3d9)
This commit is contained in:
Jan Brauer 2012-07-12 17:12:26 +02:00 committed by Jeff Forcier
parent d3dc9fcb19
commit d18b8cf1e9
2 changed files with 26 additions and 3 deletions

View File

@ -104,11 +104,16 @@ class SSHConfig (object):
@type hostname: str @type hostname: str
""" """
matches = [x for x in self._config if fnmatch.fnmatch(hostname, x['host'])] matches = [x for x in self._config if fnmatch.fnmatch(hostname, x['host'])]
# sort in order of shortest match (usually '*') to longest # Move * to the end
matches.sort(lambda x,y: cmp(len(x['host']), len(y['host']))) _star = matches[0]
del matches[0]
matches.append(_star)
ret = {}
ret = {} ret = {}
for m in matches: for m in matches:
ret.update(m) for k,v in m.iteritems():
if not k in ret:
ret[k] = v
ret = self._expand_variables(ret, hostname) ret = self._expand_variables(ret, hostname)
del ret['host'] del ret['host']
return ret return ret

View File

@ -159,3 +159,21 @@ class UtilTest (unittest.TestCase):
x = rng.read(32) x = rng.read(32)
self.assertEquals(len(x), 32) self.assertEquals(len(x), 32)
def test_7_host_config_expose_issue_33(self):
test_config_file = """
Host www13.*
Port 22
Host *.example.com
Port 2222
Host *
Port 3333
"""
f = cStringIO.StringIO(test_config_file)
config = ssh.util.parse_ssh_config(f)
host = 'www13.example.com'
self.assertEquals(
ssh.util.lookup_ssh_host_config(host, config),
{'hostname': host, 'port': '22'}
)