Device Tracker - ASUSWRT: Replaced telnet with ssh (#2079)

This commit is contained in:
froz 2016-05-15 12:20:17 -07:00 committed by Paulus Schoutsen
parent ffbc99fac2
commit 49acdaa8fd
2 changed files with 14 additions and 18 deletions

View file

@ -6,7 +6,6 @@ https://home-assistant.io/components/device_tracker.asuswrt/
""" """
import logging import logging
import re import re
import telnetlib
import threading import threading
from datetime import timedelta from datetime import timedelta
@ -19,6 +18,7 @@ from homeassistant.util import Throttle
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5) MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pexpect==4.0.1']
_LEASES_REGEX = re.compile( _LEASES_REGEX = re.compile(
r'\w+\s' + r'\w+\s' +
@ -102,24 +102,19 @@ class AsusWrtDeviceScanner(object):
def get_asuswrt_data(self): def get_asuswrt_data(self):
"""Retrieve data from ASUSWRT and return parsed result.""" """Retrieve data from ASUSWRT and return parsed result."""
from pexpect import pxssh
try: try:
telnet = telnetlib.Telnet(self.host) ssh = pxssh.pxssh()
telnet.read_until(b'login: ') ssh.login(self.host, self.username, self.password)
telnet.write((self.username + '\n').encode('ascii')) ssh.sendline('ip neigh')
telnet.read_until(b'Password: ') ssh.prompt()
telnet.write((self.password + '\n').encode('ascii')) neighbors = ssh.before.split(b'\n')[1:-1]
prompt_string = telnet.read_until(b'#').split(b'\n')[-1] ssh.sendline('cat /var/lib/misc/dnsmasq.leases')
telnet.write('ip neigh\n'.encode('ascii')) ssh.prompt()
neighbors = telnet.read_until(prompt_string).split(b'\n')[1:-1] leases_result = ssh.before.split(b'\n')[1:-1]
telnet.write('cat /var/lib/misc/dnsmasq.leases\n'.encode('ascii')) ssh.logout()
leases_result = telnet.read_until(prompt_string).split(b'\n')[1:-1] except pxssh.ExceptionPxssh as exc:
telnet.write('exit\n'.encode('ascii')) _LOGGER.exception('Unexpected response from router: %s', exc)
except EOFError:
_LOGGER.exception("Unexpected response from router")
return
except ConnectionRefusedError:
_LOGGER.exception("Connection refused by router," +
" is telnet enabled?")
return return
devices = {} devices = {}

View file

@ -171,6 +171,7 @@ paho-mqtt==1.1
panasonic_viera==0.2 panasonic_viera==0.2
# homeassistant.components.device_tracker.aruba # homeassistant.components.device_tracker.aruba
# homeassistant.components.device_tracker.asuswrt
pexpect==4.0.1 pexpect==4.0.1
# homeassistant.components.light.hue # homeassistant.components.light.hue