2015-05-11 16:06:12 +00:00
|
|
|
"""
|
2016-03-07 17:12:06 +00:00
|
|
|
Support for scanning a network with nmap.
|
2015-05-11 16:06:12 +00:00
|
|
|
|
2015-10-13 18:55:45 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/device_tracker.nmap_scanner/
|
2015-05-11 16:06:12 +00:00
|
|
|
"""
|
2014-12-15 05:29:36 +00:00
|
|
|
import logging
|
|
|
|
import re
|
2016-02-19 05:27:50 +00:00
|
|
|
import subprocess
|
|
|
|
from collections import namedtuple
|
|
|
|
from datetime import timedelta
|
2014-12-15 05:29:36 +00:00
|
|
|
|
2016-09-18 06:44:15 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-05-15 04:07:15 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-09-18 06:44:15 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN, PLATFORM_SCHEMA
|
2014-12-15 05:29:36 +00:00
|
|
|
from homeassistant.const import CONF_HOSTS
|
2016-09-18 06:44:15 +00:00
|
|
|
from homeassistant.util import Throttle
|
2014-12-15 05:29:36 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
REQUIREMENTS = ['python-nmap==0.6.1']
|
2014-12-15 05:29:36 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
CONF_EXCLUDE = 'exclude'
|
2016-07-31 20:47:46 +00:00
|
|
|
# Interval in minutes to exclude devices from a scan while they are home
|
2016-09-03 23:41:38 +00:00
|
|
|
CONF_HOME_INTERVAL = 'home_interval'
|
2015-03-07 20:52:54 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
|
|
|
|
|
2015-08-09 04:22:34 +00:00
|
|
|
|
2016-09-18 06:44:15 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2016-10-20 05:15:00 +00:00
|
|
|
vol.Required(CONF_HOSTS): cv.ensure_list,
|
2016-09-18 06:44:15 +00:00
|
|
|
vol.Required(CONF_HOME_INTERVAL, default=0): cv.positive_int,
|
|
|
|
vol.Optional(CONF_EXCLUDE, default=[]):
|
|
|
|
vol.All(cv.ensure_list, vol.Length(min=1))
|
|
|
|
})
|
|
|
|
|
2014-12-15 05:29:36 +00:00
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Validate the configuration and return a Nmap scanner."""
|
2014-12-15 05:29:36 +00:00
|
|
|
scanner = NmapDeviceScanner(config[DOMAIN])
|
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
2016-09-18 06:44:15 +00:00
|
|
|
Device = namedtuple('Device', ['mac', 'name', 'ip', 'last_update'])
|
2014-12-15 05:29:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _arp(ip_address):
|
2016-03-07 17:12:06 +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()
|
2014-12-19 23:42:34 +00:00
|
|
|
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)
|
2016-09-18 06:44:15 +00:00
|
|
|
_LOGGER.info('No MAC address found for %s', ip_address)
|
2015-08-31 07:29:41 +00:00
|
|
|
return None
|
2014-12-15 05:29:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NmapDeviceScanner(object):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""This class scans for devices using nmap."""
|
2016-03-07 20:18:53 +00:00
|
|
|
|
2016-09-18 06:44:15 +00:00
|
|
|
exclude = []
|
|
|
|
|
2014-12-15 05:29:36 +00:00
|
|
|
def __init__(self, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Initialize the scanner."""
|
2014-12-15 05:29:36 +00:00
|
|
|
self.last_results = []
|
|
|
|
|
|
|
|
self.hosts = config[CONF_HOSTS]
|
2016-09-03 23:41:38 +00:00
|
|
|
self.exclude = config.get(CONF_EXCLUDE, [])
|
2016-09-18 06:44:15 +00:00
|
|
|
minutes = config[CONF_HOME_INTERVAL]
|
2015-03-07 20:52:54 +00:00
|
|
|
self.home_interval = timedelta(minutes=minutes)
|
2014-12-15 05:29:36 +00:00
|
|
|
|
2015-08-31 07:29:41 +00:00
|
|
|
self.success_init = self._update_info()
|
2016-10-30 08:58:34 +00:00
|
|
|
_LOGGER.info("nmap scanner initialized")
|
2014-12-15 05:29:36 +00:00
|
|
|
|
|
|
|
def scan_devices(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Scan for new devices and return a list with 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):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Return the name of the given device or None if we don't know."""
|
2014-12-15 05:29:36 +00:00
|
|
|
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):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Scan the network for devices.
|
|
|
|
|
2015-09-07 17:19:11 +00:00
|
|
|
Returns boolean if scanning successful.
|
|
|
|
"""
|
2016-10-30 08:58:34 +00:00
|
|
|
_LOGGER.info("Scanning...")
|
2015-03-26 05:50:51 +00:00
|
|
|
|
2015-08-31 07:29:41 +00:00
|
|
|
from nmap import PortScanner, PortScannerError
|
|
|
|
scanner = PortScanner()
|
|
|
|
|
2016-09-18 06:44:15 +00:00
|
|
|
options = '-F --host-timeout 5s '
|
2015-10-09 04:45:51 +00:00
|
|
|
|
2015-03-26 05:50: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:
|
2016-09-03 23:41:38 +00:00
|
|
|
exclude_hosts = self.exclude + [device.ip for device
|
|
|
|
in last_results]
|
|
|
|
else:
|
|
|
|
exclude_hosts = self.exclude
|
2015-10-09 04:45:51 +00:00
|
|
|
else:
|
|
|
|
last_results = []
|
2016-09-03 23:41:38 +00:00
|
|
|
exclude_hosts = self.exclude
|
|
|
|
if exclude_hosts:
|
2016-09-18 06:44:15 +00:00
|
|
|
options += ' --exclude {}'.format(','.join(exclude_hosts))
|
2015-03-26 05:50:51 +00:00
|
|
|
|
2015-08-31 07:29:41 +00:00
|
|
|
try:
|
2016-10-20 05:15:00 +00:00
|
|
|
result = scanner.scan(hosts=' '.join(self.hosts),
|
|
|
|
arguments=options)
|
2015-08-31 07:29:41 +00:00
|
|
|
except PortScannerError:
|
2015-03-26 05:50:51 +00:00
|
|
|
return False
|
2015-08-31 07:29:41 +00:00
|
|
|
|
|
|
|
now = dt_util.now()
|
2015-08-31 07:44:59 +00:00
|
|
|
for ipv4, info in result['scan'].items():
|
2015-08-31 07:29:41 +00:00
|
|
|
if info['status']['state'] != 'up':
|
|
|
|
continue
|
2015-10-09 04:01:38 +00:00
|
|
|
name = info['hostnames'][0]['name'] if info['hostnames'] else ipv4
|
2015-08-31 07:29:41 +00:00
|
|
|
# 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)
|
2015-08-31 07:29:41 +00:00
|
|
|
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
|
2015-08-31 07:29:41 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
_LOGGER.info("nmap scan successful")
|
2015-08-31 07:29:41 +00:00
|
|
|
return True
|