core/homeassistant/components/device_tracker/nmap_tracker.py

135 lines
4.2 KiB
Python
Raw Normal View History

2015-05-11 16:06:12 +00:00
"""
homeassistant.components.device_tracker.nmap
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Device tracker platform that supports scanning a network with nmap.
2015-10-13 18:55:45 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.nmap_scanner.html
2015-05-11 16:06:12 +00:00
"""
2014-12-15 05:29:36 +00:00
import logging
from datetime import timedelta
2014-12-15 05:29:36 +00:00
from collections import namedtuple
import subprocess
import re
import homeassistant.util.dt as dt_util
2014-12-15 05:29:36 +00:00
from homeassistant.const import CONF_HOSTS
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle, convert
2014-12-15 05:29:36 +00:00
from homeassistant.components.device_tracker import DOMAIN
# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
_LOGGER = logging.getLogger(__name__)
# interval in minutes to exclude devices from a scan while they are home
CONF_HOME_INTERVAL = "home_interval"
2015-09-14 06:35:12 +00:00
REQUIREMENTS = ['python-nmap==0.4.3']
2015-08-09 04:22:34 +00:00
2014-12-15 05:29:36 +00:00
def get_scanner(hass, config):
""" Validates config and returns a Nmap scanner. """
if not validate_config(config, {DOMAIN: [CONF_HOSTS]},
_LOGGER):
return None
scanner = NmapDeviceScanner(config[DOMAIN])
return scanner if scanner.success_init else None
Device = namedtuple("Device", ["mac", "name", "ip", "last_update"])
2014-12-15 05:29:36 +00:00
def _arp(ip_address):
2015-05-11 16:06:12 +00:00
""" Get the MAC address for a given IP. """
2014-12-15 05:29:36 +00:00
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))
2014-12-15 05:29:36 +00:00
if match:
return match.group(0)
_LOGGER.info("No MAC address found for %s", ip_address)
return None
2014-12-15 05:29:36 +00:00
class NmapDeviceScanner(object):
""" This class scans for devices using nmap. """
2014-12-15 05:29:36 +00:00
def __init__(self, config):
self.last_results = []
self.hosts = config[CONF_HOSTS]
minutes = convert(config.get(CONF_HOME_INTERVAL), int, 0)
self.home_interval = timedelta(minutes=minutes)
2014-12-15 05:29:36 +00:00
self.success_init = self._update_info()
2014-12-15 05:29:36 +00:00
_LOGGER.info("nmap scanner initialized")
def scan_devices(self):
"""
Scans for new devices and return a list containing found device ids.
"""
2014-12-15 05:29:36 +00:00
self._update_info()
return [device.mac for device in self.last_results]
def get_device_name(self, mac):
""" Returns the name of the given device or None if we don't know. """
filter_named = [device.name for device in self.last_results
if device.mac == mac]
if filter_named:
return filter_named[0]
else:
return None
@Throttle(MIN_TIME_BETWEEN_SCANS)
def _update_info(self):
"""
Scans the network for devices.
Returns boolean if scanning successful.
"""
_LOGGER.info("Scanning")
from nmap import PortScanner, PortScannerError
scanner = PortScanner()
2015-09-01 15:50:45 +00:00
options = "-F --host-timeout 5"
2015-10-09 04:45:51 +00:00
if self.home_interval:
2015-10-09 04:45:51 +00:00
boundary = dt_util.now() - self.home_interval
last_results = [device for device in self.last_results
if device.last_update > boundary]
if last_results:
# Pylint is confused here.
# pylint: disable=no-member
options += " --exclude {}".format(",".join(device.ip for device
in last_results))
else:
last_results = []
try:
result = scanner.scan(hosts=self.hosts, arguments=options)
except PortScannerError:
return False
now = dt_util.now()
2015-08-31 07:44:59 +00:00
for ipv4, info in result['scan'].items():
if info['status']['state'] != 'up':
continue
2015-10-09 04:01:38 +00:00
name = info['hostnames'][0]['name'] if info['hostnames'] else ipv4
# Mac address only returned if nmap ran as root
2015-09-01 05:01:45 +00:00
mac = info['addresses'].get('mac') or _arp(ipv4)
if mac is None:
continue
2015-10-09 04:45:51 +00:00
last_results.append(Device(mac.upper(), name, ipv4, now))
self.last_results = last_results
_LOGGER.info("nmap scan successful")
return True