aboutsummaryrefslogtreecommitdiffstats
path: root/autoscan/autoscan.py
blob: f614db3a2e9e067e1e9465c693ced271f4411fd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/env python

# autoscan - automatic fingerprint of visited networks
# XXX IN PROGRESS 20130721 laurent
# 2013, Laurent Ghigonis <laurent@gouloum.fr>

# 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

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 = "."

# XXX netifaces
# XXX thread per interface
Mon_iface("eth0", logpath)