1 """Support for Cisco IOS Routers."""
3 from __future__
import annotations
8 from pexpect
import pxssh
9 import voluptuous
as vol
12 DOMAIN
as DEVICE_TRACKER_DOMAIN,
13 PLATFORM_SCHEMA
as DEVICE_TRACKER_PLATFORM_SCHEMA,
21 _LOGGER = logging.getLogger(__name__)
23 PLATFORM_SCHEMA = vol.All(
24 DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
26 vol.Required(CONF_HOST): cv.string,
27 vol.Required(CONF_USERNAME): cv.string,
28 vol.Optional(CONF_PASSWORD, default=
""): cv.string,
29 vol.Optional(CONF_PORT): cv.port,
35 def get_scanner(hass: HomeAssistant, config: ConfigType) -> CiscoDeviceScanner |
None:
36 """Validate the configuration and return a Cisco scanner."""
39 return scanner
if scanner.success_init
else None
43 """Class which queries a wireless router running Cisco IOS firmware."""
46 """Initialize the scanner."""
47 self.
hosthost = config[CONF_HOST]
49 self.
portport = config.get(CONF_PORT)
57 """Get the firmware doesn't save the name of the wireless device."""
61 """Scan for new devices and return a list with found device IDs."""
67 """Ensure the information from the Cisco router is up to date.
69 Returns boolean if scanning successful.
75 lines_result = string_result.splitlines()
81 lines_result = lines_result[2:]
83 for line
in lines_result:
97 last_results.append(mac)
105 """Open connection to the router and get arp entries."""
108 cisco_ssh = pxssh.pxssh()
114 auto_prompt_reset=
False,
118 initial_line = cisco_ssh.before.decode(
"utf-8").splitlines()
119 router_hostname = initial_line[len(initial_line) - 1]
120 router_hostname +=
"#"
122 regex_expression = f
"(?i)^{router_hostname}".encode()
123 cisco_ssh.PROMPT = re.compile(regex_expression, re.MULTILINE)
125 cisco_ssh.sendline(
"terminal length 0")
128 cisco_ssh.sendline(
"show ip arp")
131 devices_result = cisco_ssh.before
133 return devices_result.decode(
"utf-8")
134 except pxssh.ExceptionPxssh
as px_e:
135 _LOGGER.error(
"Failed to login via pxssh: %s", px_e)
141 """Parse a Cisco formatted HW address to normal MAC.
149 Takes in cisco_hwaddr: HWAddr String from Cisco ARP table
150 Returns a regular standard MAC address
152 cisco_hardware_addr = cisco_hardware_addr.replace(
".",
"")
154 cisco_hardware_addr[x : x + 2]
for x
in range(0, len(cisco_hardware_addr), 2)
157 return ":".join(blocks).upper()
str|None async_get_device_name(self, str device)
def __init__(self, config)
def _parse_cisco_mac_address(cisco_hardware_addr)
CiscoDeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)