~ubuntu-branches/ubuntu/precise/checkbox/precise

« back to all changes in this revision

Viewing changes to scripts/internet_test

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 16:46:15 UTC
  • Revision ID: james.westby@ubuntu.com-20090120164615-7iz6nmlef41h4vx2
Tags: 0.4
* Setup bzr-builddeb in native mode.
* Removed LGPL notice from the copyright file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import os
 
4
import re
 
5
import sys
 
6
 
 
7
import commands
 
8
import logging
 
9
import socket
 
10
import struct
 
11
 
 
12
from optparse import OptionParser
 
13
 
 
14
 
 
15
class Route(object):
 
16
    """Gets routing information from the system.
 
17
    """
 
18
 
 
19
    # auxiliary functions
 
20
    def _hex_to_dec(self, string):
 
21
        """Returns the integer value of a hexadecimal string s
 
22
        """
 
23
        return int(string, 16)
 
24
 
 
25
    def _num_to_dotted_quad(self, number):
 
26
        """Convert long int to dotted quad string
 
27
        """
 
28
        return socket.inet_ntoa(struct.pack("<L", number))
 
29
 
 
30
    def _get_default_gateway_from_proc(self):
 
31
        """"Returns the current default gateway, reading that from /proc
 
32
        """
 
33
        logging.debug("Reading default gateway information from /proc")
 
34
        try:
 
35
            file = open("/proc/net/route")
 
36
            route = file.read()
 
37
        except:
 
38
            logging.error("Failed to read def gateway from /proc")
 
39
            return None
 
40
        else:
 
41
            h = re.compile("\n(?P<interface>\w+)\s+00000000\s+(?P<def_gateway>[\w]+)\s+")
 
42
            w = h.search(route)
 
43
            if w:
 
44
                if w.group("def_gateway"):
 
45
                    return self._num_to_dotted_quad(self._hex_to_dec(w.group("def_gateway")))
 
46
                else:
 
47
                    logging.error("Could not find def gateway info in /proc")
 
48
                    return None
 
49
            else:
 
50
                logging.error("Could not find def gateway info in /proc")
 
51
                return None
 
52
 
 
53
    def _get_default_gateway_from_bin_route(self):
 
54
        """Get default gateway from /sbin/route -n
 
55
        Called by get_default_gateway and is only used if could not get that from /proc
 
56
        """
 
57
        logging.debug("Reading default gateway information from route binary")
 
58
        routebin = commands.getstatusoutput("export LANGUAGE=C; /usr/bin/env route -n")
 
59
 
 
60
        if routebin[0] != 0:
 
61
            logging.error("Error while trying to run route")
 
62
            return None
 
63
 
 
64
        h = re.compile("\n0.0.0.0\s+(?P<def_gateway>[\w.]+)\s+")
 
65
        w = h.search(routebin[1])
 
66
        if w:
 
67
            def_gateway = w.group("def_gateway")
 
68
 
 
69
        if def_gateway:
 
70
            return def_gateway
 
71
 
 
72
        logging.error("Could not find default gateway by running route")
 
73
        return None
 
74
 
 
75
    def get_hostname(self):
 
76
        return socket.gethostname()
 
77
 
 
78
    def get_default_gateway(self):
 
79
        t1 = self._get_default_gateway_from_proc()
 
80
        if not t1:
 
81
            t1 = self._get_default_gateway_from_bin_route()
 
82
 
 
83
        return t1
 
84
 
 
85
def ping(host, count, deadline):
 
86
    command = "ping -q -c %s -w %s %s" % (count, deadline, host)
 
87
    reg = re.compile(r"(\d) received")
 
88
    packets_received = 0
 
89
 
 
90
    output = os.popen(command)
 
91
    for line in output.readlines():
 
92
        received = re.findall(reg, line)
 
93
        if received:
 
94
            packets_received = int(received[0])
 
95
 
 
96
    return packets_received
 
97
 
 
98
def main(args):
 
99
    default_count = 2
 
100
    default_delay = 4
 
101
 
 
102
    usage = "%prog [HOST]"
 
103
    parser = OptionParser(usage=usage)
 
104
    parser.add_option("-c", "--count",
 
105
        default=default_count,
 
106
        type="int",
 
107
        help="Number of packets to send.")
 
108
    parser.add_option("-d", "--deadline",
 
109
        default=default_delay,
 
110
        type="int",
 
111
        help="Timeouts in seconds.")
 
112
    (options, args) = parser.parse_args(args)
 
113
 
 
114
    if args:
 
115
        host = args.pop(0)
 
116
    else:
 
117
        route = Route()
 
118
        host = route.get_default_gateway()
 
119
 
 
120
    received_packets = ping(host, options.count, options.deadline)
 
121
 
 
122
    if received_packets == 0:
 
123
        print "No Internet connection"
 
124
    elif received_packets != options.count:
 
125
        print "Connection established lost a packet"
 
126
    else:
 
127
        print "Internet connection fully established"
 
128
 
 
129
    return 0
 
130
 
 
131
if __name__ == "__main__":
 
132
    sys.exit(main(sys.argv[1:]))