mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00:00
Cleanup the asuswrt component (#8359)
* Get the list of wireless interfaces from nvram so tri-band routers work * ARP checks don't work reliably for ap mode so remove it, associated list seems pretty solid * Match on 'mac' instead of 'ip' since we can't use it for ap mode * The `client_info_tmp` nvram reference, Asus removed it some time ago * Update AsusWrtResult
This commit is contained in:
parent
c48c2b00a8
commit
a7d5a8d93e
@ -60,20 +60,11 @@ _LEASES_REGEX = re.compile(
|
|||||||
r'(?P<host>([^\s]+))')
|
r'(?P<host>([^\s]+))')
|
||||||
|
|
||||||
# Command to get both 5GHz and 2.4GHz clients
|
# Command to get both 5GHz and 2.4GHz clients
|
||||||
_WL_CMD = '{ wl -i eth2 assoclist & wl -i eth1 assoclist ; }'
|
_WL_CMD = 'for dev in `nvram get wl_ifnames`; do wl -i $dev assoclist; done'
|
||||||
_WL_REGEX = re.compile(
|
_WL_REGEX = re.compile(
|
||||||
r'\w+\s' +
|
r'\w+\s' +
|
||||||
r'(?P<mac>(([0-9A-F]{2}[:-]){5}([0-9A-F]{2})))')
|
r'(?P<mac>(([0-9A-F]{2}[:-]){5}([0-9A-F]{2})))')
|
||||||
|
|
||||||
_ARP_CMD = 'arp -n'
|
|
||||||
_ARP_REGEX = re.compile(
|
|
||||||
r'.+\s' +
|
|
||||||
r'\((?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})\)\s' +
|
|
||||||
r'.+\s' +
|
|
||||||
r'(?P<mac>(([0-9a-f]{2}[:-]){5}([0-9a-f]{2})))' +
|
|
||||||
r'\s' +
|
|
||||||
r'.*')
|
|
||||||
|
|
||||||
_IP_NEIGH_CMD = 'ip neigh'
|
_IP_NEIGH_CMD = 'ip neigh'
|
||||||
_IP_NEIGH_REGEX = re.compile(
|
_IP_NEIGH_REGEX = re.compile(
|
||||||
r'(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3}|'
|
r'(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3}|'
|
||||||
@ -84,15 +75,6 @@ _IP_NEIGH_REGEX = re.compile(
|
|||||||
r'\s?(router)?'
|
r'\s?(router)?'
|
||||||
r'(?P<status>(\w+))')
|
r'(?P<status>(\w+))')
|
||||||
|
|
||||||
_NVRAM_CMD = 'nvram get client_info_tmp'
|
|
||||||
_NVRAM_REGEX = re.compile(
|
|
||||||
r'.*>.*>' +
|
|
||||||
r'(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})' +
|
|
||||||
r'>' +
|
|
||||||
r'(?P<mac>(([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})))' +
|
|
||||||
r'>' +
|
|
||||||
r'.*')
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def get_scanner(hass, config):
|
def get_scanner(hass, config):
|
||||||
@ -102,7 +84,7 @@ def get_scanner(hass, config):
|
|||||||
return scanner if scanner.success_init else None
|
return scanner if scanner.success_init else None
|
||||||
|
|
||||||
|
|
||||||
AsusWrtResult = namedtuple('AsusWrtResult', 'neighbors leases arp nvram')
|
AsusWrtResult = namedtuple('AsusWrtResult', 'neighbors leases')
|
||||||
|
|
||||||
|
|
||||||
class AsusWrtDeviceScanner(DeviceScanner):
|
class AsusWrtDeviceScanner(DeviceScanner):
|
||||||
@ -173,7 +155,7 @@ class AsusWrtDeviceScanner(DeviceScanner):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
_LOGGER.info('Checking ARP')
|
_LOGGER.info('Checking Devices')
|
||||||
data = self.get_asuswrt_data()
|
data = self.get_asuswrt_data()
|
||||||
if not data:
|
if not data:
|
||||||
return False
|
return False
|
||||||
@ -182,7 +164,7 @@ class AsusWrtDeviceScanner(DeviceScanner):
|
|||||||
client['status'] == 'REACHABLE' or
|
client['status'] == 'REACHABLE' or
|
||||||
client['status'] == 'DELAY' or
|
client['status'] == 'DELAY' or
|
||||||
client['status'] == 'STALE' or
|
client['status'] == 'STALE' or
|
||||||
client['status'] == 'IN_NVRAM']
|
client['status'] == 'IN_ASSOCLIST']
|
||||||
self.last_results = active_clients
|
self.last_results = active_clients
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -204,41 +186,12 @@ class AsusWrtDeviceScanner(DeviceScanner):
|
|||||||
|
|
||||||
host = ''
|
host = ''
|
||||||
|
|
||||||
# match mac addresses to IP addresses in ARP table
|
devices[match.group('mac').upper()] = {
|
||||||
for arp in result.arp:
|
'host': host,
|
||||||
if match.group('mac').lower() in \
|
'status': 'IN_ASSOCLIST',
|
||||||
arp.decode('utf-8').lower():
|
'ip': '',
|
||||||
arp_match = _ARP_REGEX.search(
|
'mac': match.group('mac').upper(),
|
||||||
arp.decode('utf-8').lower())
|
}
|
||||||
if not arp_match:
|
|
||||||
_LOGGER.warning("Could not parse arp row: %s", arp)
|
|
||||||
continue
|
|
||||||
|
|
||||||
devices[arp_match.group('ip')] = {
|
|
||||||
'host': host,
|
|
||||||
'status': '',
|
|
||||||
'ip': arp_match.group('ip'),
|
|
||||||
'mac': match.group('mac').upper(),
|
|
||||||
}
|
|
||||||
|
|
||||||
# match mac addresses to IP addresses in NVRAM table
|
|
||||||
for nvr in result.nvram:
|
|
||||||
if match.group('mac').upper() in nvr.decode('utf-8'):
|
|
||||||
nvram_match = _NVRAM_REGEX.search(nvr.decode('utf-8'))
|
|
||||||
if not nvram_match:
|
|
||||||
_LOGGER.warning("Could not parse nvr row: %s", nvr)
|
|
||||||
continue
|
|
||||||
|
|
||||||
# skip current check if already in ARP table
|
|
||||||
if nvram_match.group('ip') in devices.keys():
|
|
||||||
continue
|
|
||||||
|
|
||||||
devices[nvram_match.group('ip')] = {
|
|
||||||
'host': host,
|
|
||||||
'status': 'IN_NVRAM',
|
|
||||||
'ip': nvram_match.group('ip'),
|
|
||||||
'mac': match.group('mac').upper(),
|
|
||||||
}
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
for lease in result.leases:
|
for lease in result.leases:
|
||||||
@ -256,20 +209,23 @@ class AsusWrtDeviceScanner(DeviceScanner):
|
|||||||
if host == '*':
|
if host == '*':
|
||||||
host = ''
|
host = ''
|
||||||
|
|
||||||
devices[match.group('ip')] = {
|
devices[match.group('mac')] = {
|
||||||
'host': host,
|
'host': host,
|
||||||
'status': '',
|
'status': '',
|
||||||
'ip': match.group('ip'),
|
'ip': match.group('ip'),
|
||||||
'mac': match.group('mac').upper(),
|
'mac': match.group('mac').upper(),
|
||||||
}
|
}
|
||||||
|
|
||||||
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",
|
||||||
continue
|
neighbor)
|
||||||
if match.group('ip') in devices:
|
continue
|
||||||
devices[match.group('ip')]['status'] = match.group('status')
|
if match.group('mac') in devices:
|
||||||
|
devices[match.group('mac')]['status'] = (
|
||||||
|
match.group('status'))
|
||||||
|
|
||||||
return devices
|
return devices
|
||||||
|
|
||||||
|
|
||||||
@ -317,27 +273,19 @@ class SshConnection(_Connection):
|
|||||||
try:
|
try:
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
self.connect()
|
self.connect()
|
||||||
self._ssh.sendline(_IP_NEIGH_CMD)
|
|
||||||
self._ssh.prompt()
|
|
||||||
neighbors = self._ssh.before.split(b'\n')[1:-1]
|
|
||||||
if self._ap:
|
if self._ap:
|
||||||
self._ssh.sendline(_ARP_CMD)
|
neighbors = ['']
|
||||||
self._ssh.prompt()
|
|
||||||
arp_result = self._ssh.before.split(b'\n')[1:-1]
|
|
||||||
self._ssh.sendline(_WL_CMD)
|
self._ssh.sendline(_WL_CMD)
|
||||||
self._ssh.prompt()
|
self._ssh.prompt()
|
||||||
leases_result = self._ssh.before.split(b'\n')[1:-1]
|
leases_result = self._ssh.before.split(b'\n')[1:-1]
|
||||||
self._ssh.sendline(_NVRAM_CMD)
|
|
||||||
self._ssh.prompt()
|
|
||||||
nvram_result = self._ssh.before.split(b'\n')[1].split(b'<')[1:]
|
|
||||||
else:
|
else:
|
||||||
arp_result = ['']
|
self._ssh.sendline(_IP_NEIGH_CMD)
|
||||||
nvram_result = ['']
|
self._ssh.prompt()
|
||||||
|
neighbors = self._ssh.before.split(b'\n')[1:-1]
|
||||||
self._ssh.sendline(_LEASES_CMD)
|
self._ssh.sendline(_LEASES_CMD)
|
||||||
self._ssh.prompt()
|
self._ssh.prompt()
|
||||||
leases_result = self._ssh.before.split(b'\n')[1:-1]
|
leases_result = self._ssh.before.split(b'\n')[1:-1]
|
||||||
return AsusWrtResult(neighbors, leases_result, arp_result,
|
return AsusWrtResult(neighbors, leases_result)
|
||||||
nvram_result)
|
|
||||||
except exceptions.EOF as err:
|
except exceptions.EOF as err:
|
||||||
_LOGGER.error("Connection refused. SSH enabled?")
|
_LOGGER.error("Connection refused. SSH enabled?")
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
@ -407,23 +355,14 @@ class TelnetConnection(_Connection):
|
|||||||
neighbors = (self._telnet.read_until(self._prompt_string).
|
neighbors = (self._telnet.read_until(self._prompt_string).
|
||||||
split(b'\n')[1:-1])
|
split(b'\n')[1:-1])
|
||||||
if self._ap:
|
if self._ap:
|
||||||
self._telnet.write('{}\n'.format(_ARP_CMD).encode('ascii'))
|
|
||||||
arp_result = (self._telnet.read_until(self._prompt_string).
|
|
||||||
split(b'\n')[1:-1])
|
|
||||||
self._telnet.write('{}\n'.format(_WL_CMD).encode('ascii'))
|
self._telnet.write('{}\n'.format(_WL_CMD).encode('ascii'))
|
||||||
leases_result = (self._telnet.read_until(self._prompt_string).
|
leases_result = (self._telnet.read_until(self._prompt_string).
|
||||||
split(b'\n')[1:-1])
|
split(b'\n')[1:-1])
|
||||||
self._telnet.write('{}\n'.format(_NVRAM_CMD).encode('ascii'))
|
|
||||||
nvram_result = (self._telnet.read_until(self._prompt_string).
|
|
||||||
split(b'\n')[1].split(b'<')[1:])
|
|
||||||
else:
|
else:
|
||||||
arp_result = ['']
|
|
||||||
nvram_result = ['']
|
|
||||||
self._telnet.write('{}\n'.format(_LEASES_CMD).encode('ascii'))
|
self._telnet.write('{}\n'.format(_LEASES_CMD).encode('ascii'))
|
||||||
leases_result = (self._telnet.read_until(self._prompt_string).
|
leases_result = (self._telnet.read_until(self._prompt_string).
|
||||||
split(b'\n')[1:-1])
|
split(b'\n')[1:-1])
|
||||||
return AsusWrtResult(neighbors, leases_result, arp_result,
|
return AsusWrtResult(neighbors, leases_result)
|
||||||
nvram_result)
|
|
||||||
except EOFError:
|
except EOFError:
|
||||||
_LOGGER.error("Unexpected response from router")
|
_LOGGER.error("Unexpected response from router")
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user