Fix AsusWRT to prevent SSH key confusion (#2467)

Changed "pub_key" to "ssh_key" while maintaining backwards compatibility. Quotes were also updated to match across the file.
This commit is contained in:
Dale Higgs 2016-07-08 10:58:31 -05:00 committed by Paulus Schoutsen
parent 2ab2f68318
commit ab63fbff3f

View File

@ -62,8 +62,9 @@ def get_scanner(hass, config):
_LOGGER): _LOGGER):
return None return None
elif CONF_PASSWORD not in config[DOMAIN] and \ elif CONF_PASSWORD not in config[DOMAIN] and \
'ssh_key' not in config[DOMAIN] and \
'pub_key' not in config[DOMAIN]: 'pub_key' not in config[DOMAIN]:
_LOGGER.error("Either a public key or password must be provided") _LOGGER.error('Either a private key or password must be provided')
return None return None
scanner = AsusWrtDeviceScanner(config[DOMAIN]) scanner = AsusWrtDeviceScanner(config[DOMAIN])
@ -83,8 +84,8 @@ class AsusWrtDeviceScanner(object):
"""Initialize the scanner.""" """Initialize the scanner."""
self.host = config[CONF_HOST] self.host = config[CONF_HOST]
self.username = str(config[CONF_USERNAME]) self.username = str(config[CONF_USERNAME])
self.password = str(config.get(CONF_PASSWORD, "")) self.password = str(config.get(CONF_PASSWORD, ''))
self.pub_key = str(config.get('pub_key', "")) self.ssh_key = str(config.get('ssh_key', config.get('pub_key', '')))
self.protocol = config.get('protocol') self.protocol = config.get('protocol')
self.mode = config.get('mode') self.mode = config.get('mode')
@ -120,7 +121,7 @@ class AsusWrtDeviceScanner(object):
return False return False
with self.lock: with self.lock:
_LOGGER.info("Checking ARP") _LOGGER.info('Checking ARP')
data = self.get_asuswrt_data() data = self.get_asuswrt_data()
if not data: if not data:
return False return False
@ -138,12 +139,12 @@ class AsusWrtDeviceScanner(object):
try: try:
ssh = pxssh.pxssh() ssh = pxssh.pxssh()
if self.pub_key: if self.ssh_key:
ssh.login(self.host, self.username, ssh_key=self.pub_key) ssh.login(self.host, self.username, ssh_key=self.ssh_key)
elif self.password: elif self.password:
ssh.login(self.host, self.username, self.password) ssh.login(self.host, self.username, self.password)
else: else:
_LOGGER.error('No password or public key specified') _LOGGER.error('No password or private key specified')
return None return None
ssh.sendline(_IP_NEIGH_CMD) ssh.sendline(_IP_NEIGH_CMD)
ssh.prompt() ssh.prompt()
@ -195,16 +196,16 @@ class AsusWrtDeviceScanner(object):
telnet.write('exit\n'.encode('ascii')) telnet.write('exit\n'.encode('ascii'))
return AsusWrtResult(neighbors, leases_result, arp_result) return AsusWrtResult(neighbors, leases_result, arp_result)
except EOFError: except EOFError:
_LOGGER.error("Unexpected response from router") _LOGGER.error('Unexpected response from router')
return None return None
except ConnectionRefusedError: except ConnectionRefusedError:
_LOGGER.error("Connection refused by router, is telnet enabled?") _LOGGER.error('Connection refused by router, is telnet enabled?')
return None return None
except socket.gaierror as exc: except socket.gaierror as exc:
_LOGGER.error("Socket exception: %s", exc) _LOGGER.error('Socket exception: %s', exc)
return None return None
except OSError as exc: except OSError as exc:
_LOGGER.error("OSError: %s", exc) _LOGGER.error('OSError: %s', exc)
return None return None
def get_asuswrt_data(self): def get_asuswrt_data(self):
@ -232,7 +233,7 @@ class AsusWrtDeviceScanner(object):
match = _WL_REGEX.search(lease.decode('utf-8')) match = _WL_REGEX.search(lease.decode('utf-8'))
if not match: if not match:
_LOGGER.warning("Could not parse wl row: %s", lease) _LOGGER.warning('Could not parse wl row: %s', lease)
continue continue
host = '' host = ''
@ -242,7 +243,7 @@ class AsusWrtDeviceScanner(object):
if match.group('mac').lower() in arp.decode('utf-8'): if match.group('mac').lower() in arp.decode('utf-8'):
arp_match = _ARP_REGEX.search(arp.decode('utf-8')) arp_match = _ARP_REGEX.search(arp.decode('utf-8'))
if not arp_match: if not arp_match:
_LOGGER.warning("Could not parse arp row: %s", arp) _LOGGER.warning('Could not parse arp row: %s', arp)
continue continue
devices[arp_match.group('ip')] = { devices[arp_match.group('ip')] = {
@ -256,7 +257,7 @@ class AsusWrtDeviceScanner(object):
match = _LEASES_REGEX.search(lease.decode('utf-8')) match = _LEASES_REGEX.search(lease.decode('utf-8'))
if not match: if not match:
_LOGGER.warning("Could not parse lease row: %s", lease) _LOGGER.warning('Could not parse lease row: %s', lease)
continue continue
# For leases where the client doesn't set a hostname, ensure it # For leases where the client doesn't set a hostname, ensure it
@ -275,7 +276,7 @@ class AsusWrtDeviceScanner(object):
for neighbor in result.neighbors: for neighbor in result.neighbors:
match = _IP_NEIGH_REGEX.search(neighbor.decode('utf-8')) match = _IP_NEIGH_REGEX.search(neighbor.decode('utf-8'))
if not match: if not match:
_LOGGER.warning("Could not parse neighbor row: %s", neighbor) _LOGGER.warning('Could not parse neighbor row: %s', neighbor)
continue continue
if match.group('ip') in devices: if match.group('ip') in devices:
devices[match.group('ip')]['status'] = match.group('status') devices[match.group('ip')]['status'] = match.group('status')