Fix #179 - missing host variable in fqdn evaluation

This commit is contained in:
Jeff Forcier 2013-09-27 16:08:59 -07:00
parent 0ba34035c3
commit 05abcc40f5
2 changed files with 35 additions and 15 deletions

View File

@ -35,9 +35,10 @@ class LazyFqdn(object):
Returns the host's fqdn on request as string. Returns the host's fqdn on request as string.
""" """
def __init__(self, config): def __init__(self, config, host=None):
self.fqdn = None self.fqdn = None
self.config = config self.config = config
self.host = host
def __str__(self): def __str__(self):
if self.fqdn is None: if self.fqdn is None:
@ -54,19 +55,27 @@ class LazyFqdn(object):
fqdn = None fqdn = None
address_family = self.config.get('addressfamily', 'any').lower() address_family = self.config.get('addressfamily', 'any').lower()
if address_family != 'any': if address_family != 'any':
family = socket.AF_INET if address_family == 'inet' \ try:
else socket.AF_INET6 family = socket.AF_INET if address_family == 'inet' \
results = socket.getaddrinfo(host, else socket.AF_INET6
None, results = socket.getaddrinfo(
family, self.host,
socket.SOCK_DGRAM, None,
socket.IPPROTO_IP, family,
socket.AI_CANONNAME) socket.SOCK_DGRAM,
for res in results: socket.IPPROTO_IP,
af, socktype, proto, canonname, sa = res socket.AI_CANONNAME
if canonname and '.' in canonname: )
fqdn = canonname for res in results:
break af, socktype, proto, canonname, sa = res
if canonname and '.' in canonname:
fqdn = canonname
break
# giaerror -> socket.getaddrinfo() can't resolve self.host
# (which is from socket.gethostname()). Fall back to the
# getfqdn() call below.
except socket.gaierror:
pass
# Handle 'any' / unspecified # Handle 'any' / unspecified
if fqdn is None: if fqdn is None:
fqdn = socket.getfqdn() fqdn = socket.getfqdn()
@ -216,7 +225,7 @@ class SSHConfig (object):
remoteuser = user remoteuser = user
host = socket.gethostname().split('.')[0] host = socket.gethostname().split('.')[0]
fqdn = LazyFqdn(config) fqdn = LazyFqdn(config, host)
homedir = os.path.expanduser('~') homedir = os.path.expanduser('~')
replacements = {'controlpath': replacements = {'controlpath':
[ [

View File

@ -329,3 +329,14 @@ IdentityFile id_dsa22
paramiko.util.lookup_ssh_host_config(host, config), paramiko.util.lookup_ssh_host_config(host, config),
values values
) )
def test_12_config_addressfamily_and_lazy_fqdn(self):
"""
Ensure the code path honoring non-'all' AddressFamily doesn't asplode
"""
test_config = """
AddressFamily inet
IdentityFile something_%l_using_fqdn
"""
config = paramiko.util.parse_ssh_config(cStringIO.StringIO(test_config))
assert config.lookup('meh') # will die during lookup() if bug regresses