Skip to content
Snippets Groups Projects
Commit 70bce24b authored by Paulus Schoutsen's avatar Paulus Schoutsen
Browse files

Merge pull request #73 from balloob/nmap-lock

Improve NMAP lock issues
parents ec557f8d 4484baa8
No related branches found
No related tags found
No related merge requests found
...@@ -157,7 +157,8 @@ class DeviceTracker(object): ...@@ -157,7 +157,8 @@ class DeviceTracker(object):
def update_devices(self, now): def update_devices(self, now):
""" Update device states based on the found devices. """ """ Update device states based on the found devices. """
self.lock.acquire() if not self.lock.acquire(False):
return
found_devices = set(dev.upper() for dev in found_devices = set(dev.upper() for dev in
self.device_scanner.scan_devices()) self.device_scanner.scan_devices())
......
""" Supports scanning using nmap. """ """ Supports scanning using nmap. """
import logging import logging
from datetime import timedelta, datetime from datetime import timedelta, datetime
import threading
from collections import namedtuple from collections import namedtuple
import subprocess import subprocess
import re import re
...@@ -54,7 +53,6 @@ class NmapDeviceScanner(object): ...@@ -54,7 +53,6 @@ class NmapDeviceScanner(object):
def __init__(self, config): def __init__(self, config):
self.last_results = [] self.last_results = []
self.lock = threading.Lock()
self.hosts = config[CONF_HOSTS] self.hosts = config[CONF_HOSTS]
minutes = convert(config.get(CONF_HOME_INTERVAL), int, 0) minutes = convert(config.get(CONF_HOME_INTERVAL), int, 0)
self.home_interval = timedelta(minutes=minutes) self.home_interval = timedelta(minutes=minutes)
...@@ -116,28 +114,27 @@ class NmapDeviceScanner(object): ...@@ -116,28 +114,27 @@ class NmapDeviceScanner(object):
if not self.success_init: if not self.success_init:
return False return False
with self.lock: _LOGGER.info("Scanning")
_LOGGER.info("Scanning")
options = "-F --host-timeout 5"
options = "-F" exclude_targets = set()
exclude_targets = set() if self.home_interval:
if self.home_interval: now = datetime.now()
now = datetime.now() for host in self.last_results:
for host in self.last_results: if host.last_update + self.home_interval > now:
if host.last_update + self.home_interval > now: exclude_targets.add(host)
exclude_targets.add(host) if len(exclude_targets) > 0:
if len(exclude_targets) > 0: target_list = [t.ip for t in exclude_targets]
target_list = [t.ip for t in exclude_targets] options += " --exclude {}".format(",".join(target_list))
options += " --exclude {}".format(",".join(target_list))
nmap = NmapProcess(targets=self.hosts, options=options)
nmap = NmapProcess(targets=self.hosts, options=options)
nmap.run()
nmap.run()
if nmap.rc == 0:
if nmap.rc == 0: if self._parse_results(nmap.stdout):
if self._parse_results(nmap.stdout): self.last_results.extend(exclude_targets)
self.last_results.extend(exclude_targets) else:
else: self.last_results = []
self.last_results = [] _LOGGER.error(nmap.stderr)
_LOGGER.error(nmap.stderr) return False
return False
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment