braviatv, nmap_tracker: use getmac for getting MAC addresses (#24628)
* braviatv, nmap_tracker: use getmac for getting MAC addresses Refs https://github.com/home-assistant/home-assistant/pull/24601 * Move getmac imports to top level
This commit is contained in:
1 parent
ecfbfb4527
commit
f1cbb2a0b3
5 files changed
+22
-34
No files matched your search
@@ -1,10 +1,9 @@
|
||||
"""Support for scanning a network with nmap."""
|
||||
import logging
|
||||
import re
|
||||
import subprocess
|
||||
from collections import namedtuple
|
||||
from datetime import timedelta
|
||||
|
||||
from getmac import get_mac_address
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
@@ -40,18 +39,6 @@ def get_scanner(hass, config):
|
||||
Device = namedtuple('Device', ['mac', 'name', 'ip', 'last_update'])
|
||||
|
||||
|
||||
def _arp(ip_address):
|
||||
"""Get the MAC address for a given IP."""
|
||||
cmd = ['arp', '-n', ip_address]
|
||||
arp = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
||||
out, _ = arp.communicate()
|
||||
match = re.search(r'(([0-9A-Fa-f]{1,2}\:){5}[0-9A-Fa-f]{1,2})', str(out))
|
||||
if match:
|
||||
return ':'.join([i.zfill(2) for i in match.group(0).split(':')])
|
||||
_LOGGER.info('No MAC address found for %s', ip_address)
|
||||
return None
|
||||
|
||||
|
||||
class NmapDeviceScanner(DeviceScanner):
|
||||
"""This class scans for devices using nmap."""
|
||||
|
||||
@@ -132,8 +119,9 @@ class NmapDeviceScanner(DeviceScanner):
|
||||
continue
|
||||
name = info['hostnames'][0]['name'] if info['hostnames'] else ipv4
|
||||
# Mac address only returned if nmap ran as root
|
||||
mac = info['addresses'].get('mac') or _arp(ipv4)
|
||||
mac = info['addresses'].get('mac') or get_mac_address(ip=ipv4)
|
||||
if mac is None:
|
||||
_LOGGER.info('No MAC address found for %s', ipv4)
|
||||
continue
|
||||
last_results.append(Device(mac.upper(), name, ipv4, now))
|
||||
|
||||
|
||||
Reference in new issue
Block a user