From 3b5895db7ef2c49b3942077f900982f7713af4ff Mon Sep 17 00:00:00 2001 From: Laurent Ghigonis Date: Wed, 24 Jul 2013 08:51:59 +0200 Subject: autoscan: logging --- autoscan/autoscan.py | 76 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/autoscan/autoscan.py b/autoscan/autoscan.py index b7633ac..7a98bfc 100755 --- a/autoscan/autoscan.py +++ b/autoscan/autoscan.py @@ -12,12 +12,19 @@ import re import argparse import shutil import errno +import logging class Autoscan_iface(object): - def __init__(self, iface, logpath=".", target_pubip="8.8.8.8", verbose=0, noexplore=False): + def __init__(self, iface, outdir=".", logfile=None, loglevel=logging.INFO, target_pubip="8.8.8.8", noexplore=False): + logstream = None + if not logfile: + logstream = sys.stdout + logging.basicConfig(filename=logfile, level=loglevel, + stream=logstream, + format='%(asctime)s %(message)s', + datefmt="%Y%m%d-%H%M%S") self.iface = iface - self.logpath = logpath - self.verbose = verbose + self.outdir = outdir self.target_pubip = target_pubip self.noexplore = noexplore self.date = None # set by _do_tests() @@ -31,6 +38,7 @@ class Autoscan_iface(object): self.found_ip6 = None self.found_pubip = None self.found_dns = list() + self.found_essid = None def run_now(self): self._do_tests() @@ -44,8 +52,7 @@ class Autoscan_iface(object): self._do_tests() def _wait_up(self): - if self.verbose >= 1: - print("[>] _wait_up") + logging.info("[>] %s: _wait_up", self.iface) while True: out, err, code = self._exec( ['ifconfig', self.iface]) @@ -60,8 +67,7 @@ class Autoscan_iface(object): time.sleep(3) # XXX wait for network to be configured def _wait_down(self): - if self.verbose >= 1: - print("[>] _wait_down") + logging.info("[>] %s: _wait_down", self.iface) last_ip4 = None last_ip6 = None last_t = None @@ -94,8 +100,7 @@ class Autoscan_iface(object): time.sleep(0.5) def _do_tests(self): - if self.verbose >= 1: - print("[>] _do_tests") + logging.info("[>] %s: _do_tests", self.iface) self.date = time.strftime("%Y%m%d_%H%M%S", time.gmtime()) self._do_tests_run(self._test_pcap) self._do_tests_run(self._test_ifconfig) @@ -113,12 +118,11 @@ class Autoscan_iface(object): def _do_tests_run(self, func): try: - if self.verbose >= 1: - print "[-] %s" % func.__name__ + logging.info("[-] %s: %s" % (self.iface, func.__name__)) func() except Exception, e: - print("test %s failed: %s" % (func, e)) - traceback.print_exc() + logging.info("[!] %s: test %s failed: %s" % (self.iface, func, e)) + logging.info(traceback.format_exc()) def _test_pcap(self): if os.fork() != 0: @@ -144,15 +148,20 @@ class Autoscan_iface(object): self.found_ip6 = ip6.group(1) def _test_iwconfig(self): + self.found_essid = None out, err, code = self._exec( ['iwconfig', self.iface]) if len(out) == 0: return # not a WIFI interface self._store("iwconfig/out", out) essid = re.search(r'ESSID:(\S+)', out) - if essid: self._store("iwconfig/essid", essid.group(1)) + if essid: + essid = essid.group(1).replace("\"", "") + self.found_essid = essid + self._store("iwconfig/essid", essid) ap = re.search(r'Access Point: (\S+)', out) - if ap: self._store("iwconfig/ap", ap.group(1)) + if ap: + self._store("iwconfig/ap", ap.group(1)) def _test_route(self): out, err, code = self._exec( @@ -221,21 +230,20 @@ class Autoscan_iface(object): def _store(self, suffix, txt): name = self._storepath_get(suffix) - if self.verbose >= 2: - print("%s = %s" % (name, txt)) + logging.debug("%s = %s" % (name, txt)) f = open(name, "w+") f.write(str(txt)) f.close() os.chown(name, self.perm_uid, self.perm_gid) def _storepath_get(self, suffix=None): - path = "%s/%s_%s" % (self.logpath, self.date, self.iface) + path = "%s/%s_%s" % (self.outdir, self.date, self.iface) if suffix: path += "/" + suffix d = os.path.dirname(path) if not os.path.isdir(d): os.makedirs(d) - subprocess.check_output(['chown', '-R', '%s:%s' % (self.perm_uid, self.perm_gid), self.logpath]) # pythonic way is awefull + subprocess.check_output(['chown', '-R', '%s:%s' % (self.perm_uid, self.perm_gid), self.outdir]) # pythonic way is awefull return path def _storepath_rename(self): @@ -243,9 +251,10 @@ class Autoscan_iface(object): suffix = self.found_pubip else: suffix = self.found_ip4 + if self.found_essid: + suffix += "_" + self.found_essid newpath = self._storepath_get() + "_" + suffix - if self.verbose >= 1: - print "[*] %s" % newpath + logging.info("[*] %s: %s" % (self.iface, newpath)) os.rename(self._storepath_get(), newpath) def _util_traceroute(self, target): @@ -274,8 +283,10 @@ parser.add_argument("-x", "--noexplore", action="store_true", help="Do not run explore tests (traceroute to arbitrary local ranges + nmap scan)") parser.add_argument("-p", "--pubip", action="store", default="8.8.8.8", help="Use target IP for public IP tests") +parser.add_argument("-q", "--quiet", action="store_true", + help="Quiet logging (warning only)") parser.add_argument("-v", "--verbose", action="store_true", - help="Increase output verbosity, default=0, max=2") + help="Verbose logging") args = parser.parse_args() if args.runnow and args.monitor: @@ -284,15 +295,32 @@ if args.runnow and args.monitor: if args.runnow and args.foreground: print "Cannot specify foreground with runnow, it's implicit !" sys.exit(1) +if args.verbose and args.quiet: + print "Cannot specify both verbose and quiet !" + sys.exit(1) + if not args.runnow and not args.monitor: args.runnow = True if args.runnow: args.foreground = True +if args.foreground: + logfile = None +else: + logfile = "autoscan.log" +if args.verbose: + loglevel = logging.DEBUG +elif args.quiet: + loglevel = logging.WARN +else: + loglevel = logging.INFO for iface in args.interfaces: if os.fork() == 0: - autoscan = Autoscan_iface(iface, args.outdir, args.pubip, - args.verbose, args.noexplore) + autoscan = Autoscan_iface(iface, args.outdir, + logfile=logfile, + loglevel=loglevel, + target_pubip=args.pubip, + noexplore=args.noexplore) if args.runnow: autoscan.run_now() else: -- cgit v1.2.3-59-g8ed1b