[scanner] pycodestyle compliance
This commit is contained in:
parent
2232435c22
commit
75c71f69c1
2 changed files with 48 additions and 26 deletions
|
@ -25,13 +25,15 @@ from datetime import datetime
|
||||||
|
|
||||||
log = logging.getLogger("scan")
|
log = logging.getLogger("scan")
|
||||||
|
|
||||||
|
|
||||||
def run(scan, args):
|
def run(scan, args):
|
||||||
# We run each component in a separate process since they
|
# We run each component in a separate process since they
|
||||||
# must not block on each other.
|
# must not block on each other.
|
||||||
processes = []
|
processes = []
|
||||||
|
|
||||||
if 'trigger' in args.mode:
|
if 'trigger' in args.mode:
|
||||||
t = scan.Trigger(interfaces=args.interfaces, sleeptime=args.trigger_delay)
|
t = scan.Trigger(interfaces=args.interfaces,
|
||||||
|
sleeptime=args.trigger_delay)
|
||||||
t.start()
|
t.start()
|
||||||
processes.append(t)
|
processes.append(t)
|
||||||
log.debug("Trigger started")
|
log.debug("Trigger started")
|
||||||
|
@ -48,6 +50,7 @@ def run(scan, args):
|
||||||
for process in processes:
|
for process in processes:
|
||||||
process.join()
|
process.join()
|
||||||
|
|
||||||
|
|
||||||
def list(scan, args):
|
def list(scan, args):
|
||||||
s = scan.Scanner(interfaces=args.interfaces, name_mapper=args.name_mapper)
|
s = scan.Scanner(interfaces=args.interfaces, name_mapper=args.name_mapper)
|
||||||
hosts = s.list()
|
hosts = s.list()
|
||||||
|
@ -66,10 +69,16 @@ def list(scan, args):
|
||||||
print('=' * (name_max_size + 3 + ipv6_max_size + 2 * (3 + date_max_size)))
|
print('=' * (name_max_size + 3 + ipv6_max_size + 2 * (3 + date_max_size)))
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
last_seen = host.last_seen()
|
last_seen = host.last_seen()
|
||||||
last_seen = last_seen.strftime(scan.datetime_format) if last_seen else '-'
|
if last_seen:
|
||||||
|
last_seen = last_seen.strftime(scan.datetime_format)
|
||||||
|
else:
|
||||||
|
last_seen = '-'
|
||||||
|
|
||||||
last_configured = host.last_configured()
|
last_configured = host.last_configured()
|
||||||
last_configured = last_configured.strftime(scan.datetime_format) if last_configured else '-'
|
if last_configured:
|
||||||
|
last_configured = last_configured.strftime(scan.datetime_format)
|
||||||
|
else:
|
||||||
|
'-'
|
||||||
|
|
||||||
print("{} | {} | {} | {}".format(
|
print("{} | {} | {} | {}".format(
|
||||||
host.name(default='-').ljust(name_max_size),
|
host.name(default='-').ljust(name_max_size),
|
||||||
|
@ -77,6 +86,7 @@ def list(scan, args):
|
||||||
last_seen.ljust(date_max_size),
|
last_seen.ljust(date_max_size),
|
||||||
last_configured.ljust(date_max_size)))
|
last_configured.ljust(date_max_size)))
|
||||||
|
|
||||||
|
|
||||||
# CLI processing is defined outside of the main scan class to handle
|
# CLI processing is defined outside of the main scan class to handle
|
||||||
# non-available optional scapy dependency (instead of crashing mid-flight).
|
# non-available optional scapy dependency (instead of crashing mid-flight).
|
||||||
def commandline(args):
|
def commandline(args):
|
||||||
|
@ -94,7 +104,7 @@ def commandline(args):
|
||||||
# By default scan and trigger, but do not call any action.
|
# By default scan and trigger, but do not call any action.
|
||||||
args.mode = ['scan', 'trigger', ]
|
args.mode = ['scan', 'trigger', ]
|
||||||
|
|
||||||
if 'config' in args.mode and args.name_mapper == None:
|
if 'config' in args.mode and args.name_mapper is None:
|
||||||
print('--name-mapper must be specified for scanner config mode.',
|
print('--name-mapper must be specified for scanner config mode.',
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
|
@ -33,6 +33,7 @@ logging.basicConfig(level=logging.DEBUG)
|
||||||
log = logging.getLogger("scan")
|
log = logging.getLogger("scan")
|
||||||
datetime_format = '%Y-%m-%d %H:%M:%S'
|
datetime_format = '%Y-%m-%d %H:%M:%S'
|
||||||
|
|
||||||
|
|
||||||
class Host(object):
|
class Host(object):
|
||||||
def __init__(self, addr, outdir, name_mapper=None):
|
def __init__(self, addr, outdir, name_mapper=None):
|
||||||
self.addr = addr
|
self.addr = addr
|
||||||
|
@ -55,7 +56,7 @@ class Host(object):
|
||||||
fd.write(f"{value}")
|
fd.write(f"{value}")
|
||||||
|
|
||||||
def name(self, default=None):
|
def name(self, default=None):
|
||||||
if self.name_mapper == None:
|
if self.name_mapper is None:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
fpath = os.path.join(os.getcwd(), self.name_mapper)
|
fpath = os.path.join(os.getcwd(), self.name_mapper)
|
||||||
|
@ -101,12 +102,13 @@ class Host(object):
|
||||||
log.debug("Executing: %s", cmd)
|
log.debug("Executing: %s", cmd)
|
||||||
completed_process = subprocess.run(cmd, stdout=fd, stderr=fd)
|
completed_process = subprocess.run(cmd, stdout=fd, stderr=fd)
|
||||||
if completed_process.returncode != 0:
|
if completed_process.returncode != 0:
|
||||||
log.error("%s return with non-zero code %i - see %s for details.",
|
log.error("%s return with non-zero code %i - see %s for \
|
||||||
cmd, completed_process.returncode, fname)
|
details.", cmd, completed_process.returncode, fname)
|
||||||
|
|
||||||
now = datetime.datetime.now().strftime(datetime_format)
|
now = datetime.datetime.now().strftime(datetime_format)
|
||||||
self.__set('last_configured', now)
|
self.__set('last_configured', now)
|
||||||
|
|
||||||
|
|
||||||
class Trigger(object):
|
class Trigger(object):
|
||||||
"""
|
"""
|
||||||
Trigger an ICMPv6EchoReply from all hosts that are alive
|
Trigger an ICMPv6EchoReply from all hosts that are alive
|
||||||
|
@ -140,7 +142,9 @@ class Trigger(object):
|
||||||
def trigger(self, interface):
|
def trigger(self, interface):
|
||||||
try:
|
try:
|
||||||
log.debug("Sending ICMPv6EchoRequest on %s", interface)
|
log.debug("Sending ICMPv6EchoRequest on %s", interface)
|
||||||
packet = IPv6(dst="ff02::1%{}".format(interface)) / ICMPv6EchoRequest()
|
packet = IPv6(
|
||||||
|
dst="ff02::1%{}".format(interface)
|
||||||
|
) / ICMPv6EchoRequest()
|
||||||
send(packet, verbose=self.verbose)
|
send(packet, verbose=self.verbose)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error("Could not send ICMPv6EchoRequest: %s", e)
|
log.error("Could not send ICMPv6EchoRequest: %s", e)
|
||||||
|
@ -151,7 +155,8 @@ class Scanner(object):
|
||||||
Scan for replies of hosts, maintain the up-to-date database
|
Scan for replies of hosts, maintain the up-to-date database
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, interfaces, autoconfigure=False, outdir=None, name_mapper=None):
|
def __init__(self, interfaces, autoconfigure=False, outdir=None,
|
||||||
|
name_mapper=None):
|
||||||
self.interfaces = interfaces
|
self.interfaces = interfaces
|
||||||
self.autoconfigure = autoconfigure
|
self.autoconfigure = autoconfigure
|
||||||
self.name_mapper = name_mapper
|
self.name_mapper = name_mapper
|
||||||
|
@ -169,14 +174,17 @@ class Scanner(object):
|
||||||
if ICMPv6EchoReply in pkg:
|
if ICMPv6EchoReply in pkg:
|
||||||
host = Host(pkg['IPv6'].src, self.outdir, self.name_mapper)
|
host = Host(pkg['IPv6'].src, self.outdir, self.name_mapper)
|
||||||
if host.name():
|
if host.name():
|
||||||
log.verbose("Host %s (%s) is alive", host.name(), host.address())
|
log.verbose("Host %s (%s) is alive", host.name(),
|
||||||
|
host.address())
|
||||||
else:
|
else:
|
||||||
log.verbose("Host %s is alive", host.address())
|
log.verbose("Host %s is alive", host.address())
|
||||||
|
|
||||||
host.seen()
|
host.seen()
|
||||||
|
|
||||||
# Configure if needed.
|
# Configure if needed.
|
||||||
if self.autoconfigure and \
|
if self.autoconfigure and \
|
||||||
host.last_configured(default=datetime.datetime.min) + self.config_delay < datetime.datetime.now():
|
host.last_configured(default=datetime.datetime.min) + \
|
||||||
|
self.config_delay < datetime.datetime.now():
|
||||||
self.config(host)
|
self.config(host)
|
||||||
|
|
||||||
def list(self):
|
def list(self):
|
||||||
|
@ -187,15 +195,19 @@ class Scanner(object):
|
||||||
return hosts
|
return hosts
|
||||||
|
|
||||||
def config(self, host):
|
def config(self, host):
|
||||||
if host.name() == None:
|
if host.name() is None:
|
||||||
log.debug("config - could not resolve name for %s, aborting.", host.address())
|
log.debug("config - could not resolve name for %s, aborting.",
|
||||||
|
host.address())
|
||||||
return
|
return
|
||||||
|
|
||||||
previous_config_process = self.running_configs.get(host.name())
|
previous_config_process = self.running_configs.get(host.name())
|
||||||
if previous_config_process != None and previous_config_process.is_alive():
|
if previous_config_process is not None and \
|
||||||
log.debug("config - is already running for %s, aborting.", host.name())
|
previous_config_process.is_alive():
|
||||||
|
log.debug("config - is already running for %s, aborting.",
|
||||||
|
host.name())
|
||||||
|
|
||||||
log.info("config - running against host %s (%s).", host.name(), host.address())
|
log.info("config - running against host %s (%s).", host.name(),
|
||||||
|
host.address())
|
||||||
p = Process(target=host.configure())
|
p = Process(target=host.configure())
|
||||||
p.start()
|
p.start()
|
||||||
self.running_configs[host.name()] = p
|
self.running_configs[host.name()] = p
|
||||||
|
|
Loading…
Reference in a new issue