2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Aruba Access Points."""
|
2015-08-31 09:36:12 +00:00
|
|
|
import logging
|
|
|
|
import re
|
2016-02-02 21:49:11 +00:00
|
|
|
|
2019-10-12 19:52:04 +00:00
|
|
|
import pexpect
|
2016-09-02 04:28:46 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
from homeassistant.components.device_tracker import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
DeviceScanner,
|
|
|
|
)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
2019-10-12 19:52:04 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-08-31 09:36:12 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-08-31 09:36:12 +00:00
|
|
|
|
|
|
|
_DEVICES_REGEX = re.compile(
|
2019-07-31 19:25:30 +00:00
|
|
|
r"(?P<name>([^\s]+)?)\s+"
|
|
|
|
+ r"(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})\s+"
|
|
|
|
+ r"(?P<mac>([0-9a-f]{2}[:-]){5}([0-9a-f]{2}))\s+"
|
|
|
|
)
|
2015-08-31 09:36:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-09-02 04:28:46 +00:00
|
|
|
|
2015-08-31 09:36:12 +00:00
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Validate the configuration and return a Aruba scanner."""
|
2015-08-31 09:36:12 +00:00
|
|
|
scanner = ArubaDeviceScanner(config[DOMAIN])
|
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class ArubaDeviceScanner(DeviceScanner):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""This class queries a Aruba Access Point for connected devices."""
|
2016-03-07 20:18:53 +00:00
|
|
|
|
2015-08-31 09:36:12 +00:00
|
|
|
def __init__(self, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Initialize the scanner."""
|
2015-08-31 09:36:12 +00:00
|
|
|
self.host = config[CONF_HOST]
|
|
|
|
self.username = config[CONF_USERNAME]
|
|
|
|
self.password = config[CONF_PASSWORD]
|
|
|
|
|
|
|
|
self.last_results = {}
|
|
|
|
|
2016-03-07 20:18:53 +00:00
|
|
|
# Test the router is accessible.
|
2015-08-31 09:36:12 +00:00
|
|
|
data = self.get_aruba_data()
|
|
|
|
self.success_init = data is not None
|
|
|
|
|
|
|
|
def scan_devices(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
2015-08-31 09:36:12 +00:00
|
|
|
self._update_info()
|
2019-07-31 19:25:30 +00:00
|
|
|
return [client["mac"] for client in self.last_results]
|
2015-08-31 09:36:12 +00:00
|
|
|
|
|
|
|
def get_device_name(self, device):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Return the name of the given device or None if we don't know."""
|
2015-08-31 09:36:12 +00:00
|
|
|
if not self.last_results:
|
|
|
|
return None
|
|
|
|
for client in self.last_results:
|
2019-07-31 19:25:30 +00:00
|
|
|
if client["mac"] == device:
|
|
|
|
return client["name"]
|
2015-08-31 09:36:12 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _update_info(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Ensure the information from the Aruba Access Point is up to date.
|
|
|
|
|
|
|
|
Return boolean if scanning successful.
|
2015-09-07 17:19:11 +00:00
|
|
|
"""
|
2015-08-31 09:36:12 +00:00
|
|
|
if not self.success_init:
|
|
|
|
return False
|
|
|
|
|
2017-07-24 14:45:02 +00:00
|
|
|
data = self.get_aruba_data()
|
|
|
|
if not data:
|
|
|
|
return False
|
2015-08-31 09:36:12 +00:00
|
|
|
|
2017-07-24 14:45:02 +00:00
|
|
|
self.last_results = data.values()
|
|
|
|
return True
|
2015-08-31 09:36:12 +00:00
|
|
|
|
|
|
|
def get_aruba_data(self):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""Retrieve data from Aruba Access Point and return parsed result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-02-23 21:38:05 +00:00
|
|
|
connect = f"ssh {self.username}@{self.host}"
|
|
|
|
ssh = pexpect.spawn(connect)
|
2019-07-31 19:25:30 +00:00
|
|
|
query = ssh.expect(
|
|
|
|
[
|
|
|
|
"password:",
|
|
|
|
pexpect.TIMEOUT,
|
|
|
|
pexpect.EOF,
|
|
|
|
"continue connecting (yes/no)?",
|
|
|
|
"Host key verification failed.",
|
|
|
|
"Connection refused",
|
|
|
|
"Connection timed out",
|
|
|
|
],
|
|
|
|
timeout=120,
|
|
|
|
)
|
2016-02-02 22:40:04 +00:00
|
|
|
if query == 1:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Timeout")
|
2015-08-31 09:36:12 +00:00
|
|
|
return
|
2018-07-23 08:16:05 +00:00
|
|
|
if query == 2:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unexpected response from router")
|
2015-08-31 09:36:12 +00:00
|
|
|
return
|
2018-07-23 08:16:05 +00:00
|
|
|
if query == 3:
|
2019-07-31 19:25:30 +00:00
|
|
|
ssh.sendline("yes")
|
|
|
|
ssh.expect("password:")
|
2016-02-02 21:49:11 +00:00
|
|
|
elif query == 4:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Host key changed")
|
2016-02-02 21:49:11 +00:00
|
|
|
return
|
|
|
|
elif query == 5:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Connection refused by server")
|
2016-02-02 21:49:11 +00:00
|
|
|
return
|
|
|
|
elif query == 6:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Connection timed out")
|
2016-02-02 21:49:11 +00:00
|
|
|
return
|
2016-02-02 22:40:04 +00:00
|
|
|
ssh.sendline(self.password)
|
2019-07-31 19:25:30 +00:00
|
|
|
ssh.expect("#")
|
|
|
|
ssh.sendline("show clients")
|
|
|
|
ssh.expect("#")
|
|
|
|
devices_result = ssh.before.split(b"\r\n")
|
|
|
|
ssh.sendline("exit")
|
2016-02-02 22:40:04 +00:00
|
|
|
|
2015-08-31 09:36:12 +00:00
|
|
|
devices = {}
|
|
|
|
for device in devices_result:
|
2019-07-31 19:25:30 +00:00
|
|
|
match = _DEVICES_REGEX.search(device.decode("utf-8"))
|
2015-08-31 09:36:12 +00:00
|
|
|
if match:
|
2019-07-31 19:25:30 +00:00
|
|
|
devices[match.group("ip")] = {
|
|
|
|
"ip": match.group("ip"),
|
|
|
|
"mac": match.group("mac").upper(),
|
|
|
|
"name": match.group("name"),
|
2016-02-02 22:40:04 +00:00
|
|
|
}
|
2015-08-31 09:36:12 +00:00
|
|
|
return devices
|