From d18b8cf1e9aab43abc9f6750fd9fb15f356435e4 Mon Sep 17 00:00:00 2001 From: Jan Brauer Date: Thu, 12 Jul 2012 17:12:26 +0200 Subject: [PATCH] Fix #33 - parse config as described by manpage (cherry picked from commit 011805eae07ee7be6140b95f6d8669763c55b3d9) --- paramiko/config.py | 11 ++++++++--- tests/test_util.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/paramiko/config.py b/paramiko/config.py index 75c54b4..1c9816d 100644 --- a/paramiko/config.py +++ b/paramiko/config.py @@ -104,11 +104,16 @@ class SSHConfig (object): @type hostname: str """ matches = [x for x in self._config if fnmatch.fnmatch(hostname, x['host'])] - # sort in order of shortest match (usually '*') to longest - matches.sort(lambda x,y: cmp(len(x['host']), len(y['host']))) + # Move * to the end + _star = matches[0] + del matches[0] + matches.append(_star) + ret = {} ret = {} 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) del ret['host'] return ret diff --git a/tests/test_util.py b/tests/test_util.py index e3c9dfe..ed0607f 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -159,3 +159,21 @@ class UtilTest (unittest.TestCase): x = rng.read(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'} + )