aboutsummaryrefslogtreecommitdiffstats
path: root/autoscan/autoscan.py
diff options
context:
space:
mode:
Diffstat (limited to 'autoscan/autoscan.py')
-rw-r--r--autoscan/autoscan.py105
1 files changed, 89 insertions, 16 deletions
diff --git a/autoscan/autoscan.py b/autoscan/autoscan.py
index 0c9a06b..f614db3 100644
--- a/autoscan/autoscan.py
+++ b/autoscan/autoscan.py
@@ -1,30 +1,103 @@
#!/bin/env python
-# autoscan - keep logs of visited networks
-# XXX IN PROGRESS 20130721 laurent
+# autoscan - automatic fingerprint of visited networks
+# XXX IN PROGRESS 20130721 laurent
# 2013, Laurent Ghigonis <laurent@gouloum.fr>
-# Each time internet connectivity become available after a cut-off,
-# keep a log of the following
+# Usage: autoscan.py [interfaces]
+# by default, monitor all network interfaces
+
+# Each time network connectivity become available after a cut-off,
+# run some tests and store results in a file db
# * ifconfig
# * if WIFI, iwconfig
# * route -n
# * traceroute
# * local net IP scan
+# * public IP (curl ifconfig.me)
+# * ping 8.8.8.8
+import sys
+import time
import subprocess
-TARGET = "8.8.8.8"
-LOG = "autoscan.log"
+class Mon_iface(object):
+ PUBIP = "8.8.8.8"
+
+ def __init__(self, iface, logpath):
+ self.iface = iface
+ self.logpath = logpath
+ self.date = None # set by _do_tests()
+
+ def run(self):
+ self._do_tests()
+ while True:
+ self._wait_down()
+ self._wait_up()
+ self._do_tests()
+
+ def _wait_up(self):
+ while True:
+ out, err, code = self._exec(
+ ['ifconfig', self.iface])
+ up = re.search(r'UP', out)
+ if up:
+ break
+ time.sleep(1)
+
+ def _wait_down(self):
+ while True:
+ out, err, code = self._exec(
+ ['ifconfig', self.iface])
+ up = re.search(r'UP', out)
+ if not up:
+ break
+ time.sleep(1)
+
+ def _do_tests(self):
+ self.date = time.strftime("%Y%m%d_%H%M%S", time.gmtime())
+ self._test_ifconfig()
+ self._test_iwconfig()
+ self._test_route()
+ self._test_scan()
+ self._test_pubip_get()
+ self._test_pubip_ping()
+ self._test_pubip_traceroute()
+
+ def _test_ifconfig(self):
+ out, err, code = self._exec(
+ ['ifconfig', self.iface])
+ self._store("ifconfig/out", out)
+ up = re.search(r'UP', out)
+ if up: self._store("ifconfig/up", "")
+ ip4 = re.search(r'inet (\S+)', out)
+ if ip4: self._store("ifconfig/ip4", ip4.group(1))
+ ip6 = re.search(r'inet6 (\S+)', out)
+ if ip6: self._store("ifconfig/ip6", ip6.group(1))
+
+ def _test_pubip_ping(self):
+ out, err, code = self._exec(
+ ['ping', '-W', '3', '-c', '1', self.PUBIP])
+ self._store("pubip_ping/code", code)
+ self._store("pubip_ping/out", out)
+
+ def _exec(self, cmd):
+ p = subprocess.Popen(cmd,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ out, err = p.communicate()
+ return out, err, p.returncode
+
+ def _store(self, suffix, val):
+ name = "%s/%s_%s/%s" % (self.logpath,
+ self.date, self.iface, suffix)
+ f = open(name, "w+")
+ f.write(txt)
+ f.close()
+
+
+logpath = "."
-log = open(LOG, "a")
+# XXX netifaces
+# XXX thread per interface
+Mon_iface("eth0", logpath)
-res = -1
-while True:
- res = subprocess.call(['ping', '-W', '1', '-c', '1', TARGET])
- if res == 0:
- print "ping to", address, "OK"
- elif res == 2:
- print "no response from", address
- else:
- print "ping to", address, "failed!"