Skip to content

Commit

Permalink
dev: add timeout (default 45s) to nmap in case of unusual networking
Browse files Browse the repository at this point in the history
resolves #54
  • Loading branch information
jesteria committed Jun 11, 2024
1 parent e965123 commit 3b84df5
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/netrics/measurement/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import netifaces
from descriptors import classonlymethod
from schema import Optional
from schema import Optional, Or

from netrics import task

Expand All @@ -16,6 +16,13 @@

PARAMS = task.schema.extend('connected_devices_arp', {
Optional('iface', default='eth0'): task.schema.Text,

# timeout: seconds after which nmap is killed
# (0, None, False, etc. to disable timeout)
Optional('timeout', default=45): Or(task.schema.GTZero(),
task.schema.falsey,
error='timeout: seconds greater than zero or '
'falsey to disable'),
})


Expand All @@ -32,6 +39,10 @@ def main(nmap, arp, params):
local network. The network interface to query may be configured
(`iface`).
Should nmap not return within `timeout` seconds, an error
is returned. (This may be disabled by setting a "falsey" timeout
value.)
Devices are recorded by MAC address, their most recent timestamp of
detection persisted to task state.
Expand Down Expand Up @@ -73,11 +84,22 @@ def main(nmap, arp, params):
'-sn', # no port scan
subnet,
),
timeout=(params.timeout or None),
# note: we don't actually want output -- unless there's an error
capture_output=True,
check=True,
text=True,
)
except subprocess.TimeoutExpired as exc:
# nmap took longer than it should / than is allowed
task.log.critical(
cmd=exc.cmd,
elapsed=exc.timeout,
stdout=exc.stdout,
stderr=exc.stderr,
status='timeout',
)
return task.status.timeout
except subprocess.CalledProcessError as exc:
# nmap shouldn't really error this way: this is serious
task.log.critical(
Expand Down

0 comments on commit 3b84df5

Please sign in to comment.