parent
b9ad22595f
commit
87b46a6224
1 changed files with 131 additions and 7 deletions
138
cdist/scan.py
138
cdist/scan.py
|
@ -19,24 +19,148 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
from scapy.all import *
|
#
|
||||||
from scapy.data import ETHER_TYPES
|
# Interface to be implemented:
|
||||||
|
# - cdist scan --mode {scan, trigger, install, config}, --mode can be repeated
|
||||||
|
# scan: scan / listen for icmp6 replies
|
||||||
|
# trigger: send trigger to multicast
|
||||||
|
# config: configure newly detected hosts
|
||||||
|
# install: install newly detected hosts
|
||||||
|
#
|
||||||
|
# Scanner logic
|
||||||
|
# - save results to configdir:
|
||||||
|
# basedir = ~/.cdist/scan/<ipv6-address>
|
||||||
|
# last_seen = ~/.cdist/scan/<ipv6-address>/last_seen -- record unix time or similar
|
||||||
|
# last_configured = ~/.cdist/scan/<ipv6-address>/last_configured -- record unix time or similar
|
||||||
|
# last_installed = ~/.cdist/scan/<ipv6-address>/last_configured -- record unix time or similar
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# cdist scan --list
|
||||||
|
# Show all known hosts including last seen flag
|
||||||
|
#
|
||||||
|
# Logic for reconfiguration:
|
||||||
|
#
|
||||||
|
# - record when configured last time
|
||||||
|
# - introduce a parameter --reconfigure-after that takes time argument
|
||||||
|
# - reconfigure if a) host alive and b) reconfigure-after time passed
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
from multiprocessing import Process
|
||||||
|
import os
|
||||||
|
|
||||||
|
# FIXME: fail gracefully if non existent - i.e. "scapy required for scanner - please install python3-scapy"
|
||||||
|
from scapy.all import *
|
||||||
|
|
||||||
|
# Datetime overwrites scapy.all.datetime - needs to be imported AFTER
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
class Trigger(object):
|
||||||
|
"""
|
||||||
|
Trigger an ICMPv6EchoReply from all hosts that are alive
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, interfaces=None, verbose=False):
|
||||||
|
self.interfaces = interfaces
|
||||||
|
self.verbose = verbose
|
||||||
|
|
||||||
|
# Wait 5 seconds before triggering again - FIXME: add parameter
|
||||||
|
self.sleeptime = 5
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.processes = []
|
||||||
|
for interface in self.interfaces:
|
||||||
|
p = Process(target=self.run_interface, args=(interface,))
|
||||||
|
self.processes.append(p)
|
||||||
|
p.start()
|
||||||
|
|
||||||
|
def join(self):
|
||||||
|
for process in self.processes:
|
||||||
|
process.join()
|
||||||
|
|
||||||
|
def run_interface(self, interface):
|
||||||
|
while True:
|
||||||
|
self.trigger(interface)
|
||||||
|
time.sleep(self.sleeptime)
|
||||||
|
|
||||||
|
def trigger(self, interface):
|
||||||
|
packet = IPv6(dst=f"ff02::1%{interface}") / ICMPv6EchoRequest()
|
||||||
|
send(packet, verbose=self.verbose)
|
||||||
|
|
||||||
class Scanner(object):
|
class Scanner(object):
|
||||||
def recv_msg_cpu(self, pkg):
|
"""
|
||||||
# print(pkg.__repr__())
|
Scan for replies of hosts, maintain the up-to-date database
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, interfaces=None, outdir=None):
|
||||||
|
self.interfaces = interfaces
|
||||||
|
|
||||||
|
if outdir:
|
||||||
|
self.outdir = outdir
|
||||||
|
else:
|
||||||
|
self.outdir = "."
|
||||||
|
|
||||||
|
def handle_pkg(self, pkg):
|
||||||
if ICMPv6EchoReply in pkg:
|
if ICMPv6EchoReply in pkg:
|
||||||
host = pkg['IPv6'].src
|
host = pkg['IPv6'].src
|
||||||
print(f"Host {host} is alive")
|
print(f"Host {host} is alive")
|
||||||
|
|
||||||
|
dir = os.path.join(self.outdir, host)
|
||||||
|
fname = os.path.join(dir, "last_seen")
|
||||||
|
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
|
||||||
|
os.makedirs(dir, exist_ok=True)
|
||||||
|
|
||||||
|
# FIXME: maybe adjust the format so we can easily parse again
|
||||||
|
with open(fname, "w") as fd:
|
||||||
|
fd.write(f"{now}\n")
|
||||||
|
|
||||||
|
|
||||||
def scan(self):
|
def scan(self):
|
||||||
sniff(iface="wlan0",
|
sniff(iface=self.interfaces,
|
||||||
filter="icmp6",
|
filter="icmp6",
|
||||||
prn=self.recv_msg_cpu)
|
prn=self.handle_pkg)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
s = Scanner()
|
t = Trigger(interfaces=["wlan0"])
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
# Scanner can listen on many interfaces at the same time
|
||||||
|
s = Scanner(interfaces=["wlan0"])
|
||||||
s.scan()
|
s.scan()
|
||||||
|
|
||||||
|
# Join back the trigger processes
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
# Test in my lan shows:
|
||||||
|
# [18:48] bridge:cdist% ls -1d fe80::*
|
||||||
|
# fe80::142d:f0a5:725b:1103
|
||||||
|
# fe80::20d:b9ff:fe49:ac11
|
||||||
|
# fe80::20d:b9ff:fe4c:547d
|
||||||
|
# fe80::219:d2ff:feb2:2e12
|
||||||
|
# fe80::21b:fcff:feee:f446
|
||||||
|
# fe80::21b:fcff:feee:f45c
|
||||||
|
# fe80::21b:fcff:feee:f4b1
|
||||||
|
# fe80::21b:fcff:feee:f4ba
|
||||||
|
# fe80::21b:fcff:feee:f4bc
|
||||||
|
# fe80::21b:fcff:feee:f4c1
|
||||||
|
# fe80::21d:72ff:fe86:46b
|
||||||
|
# fe80::42b0:34ff:fe6f:f6f0
|
||||||
|
# fe80::42b0:34ff:fe6f:f863
|
||||||
|
# fe80::42b0:34ff:fe6f:f9b2
|
||||||
|
# fe80::4a5d:60ff:fea1:e55f
|
||||||
|
# fe80::77a3:5e3f:82cc:f2e5
|
||||||
|
# fe80::9e93:4eff:fe6c:c1f4
|
||||||
|
# fe80::ba69:f4ff:fec5:6041
|
||||||
|
# fe80::ba69:f4ff:fec5:8db7
|
||||||
|
# fe80::bad8:12ff:fe65:313d
|
||||||
|
# fe80::bad8:12ff:fe65:d9b1
|
||||||
|
# fe80::ce2d:e0ff:fed4:2611
|
||||||
|
# fe80::ce32:e5ff:fe79:7ea7
|
||||||
|
# fe80::d66d:6dff:fe33:e00
|
||||||
|
# fe80::e2ff:f7ff:fe00:20e6
|
||||||
|
# fe80::f29f:c2ff:fe7c:275e
|
||||||
|
|
Loading…
Reference in a new issue