~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to checkbox-old/scripts/network_info

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-29 07:50:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2153.
  • Revision ID: zygmunt.krynicki@canonical.com-20130529075030-ngwz245hs2u3y6us
checkbox: move current checkbox code into checkbox-old

This patch cleans up the top-level directory of the project into dedicated
sub-project directories. One for checkbox-old (the current checkbox and all the
associated stuff), one for plainbox and another for checkbox-ng.

There are some associated changes, such as updating the 'source' mode of
checkbox provider in plainbox, and fixing paths in various test scripts that we
have.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
 
 
3
import os
 
4
import sys
 
5
import socket
 
6
import fcntl
 
7
import struct
 
8
 
 
9
SYS_PATH = '/sys/class/net'
 
10
 
 
11
 
 
12
def _read_file(file):
 
13
    source = open(file, 'r')
 
14
    content = source.read()
 
15
    source.close()
 
16
    return content
 
17
 
 
18
 
 
19
def get_connected(interface):
 
20
    STATUS = ('No', 'Yes')
 
21
    carrier_file = os.path.join(SYS_PATH, interface, 'carrier')
 
22
 
 
23
    carrier = 0
 
24
    try:
 
25
        carrier = int(_read_file(carrier_file))
 
26
    except IOError:
 
27
        pass
 
28
 
 
29
    return STATUS[carrier]
 
30
 
 
31
 
 
32
def get_ip_address(interface):
 
33
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
34
    return socket.inet_ntoa(fcntl.ioctl(
 
35
        s.fileno(),
 
36
        0x8915,  # SIOCGIFADDR
 
37
        struct.pack('256s', interface[:15].encode())
 
38
    )[20:24])
 
39
 
 
40
 
 
41
def get_mac_address(interface):
 
42
    address_file = os.path.join(SYS_PATH, interface, 'address')
 
43
 
 
44
    address = ''
 
45
    try:
 
46
        address = _read_file(address_file)
 
47
    except IOError:
 
48
        pass
 
49
 
 
50
    return address
 
51
 
 
52
 
 
53
def main(args):
 
54
    for interface in args:
 
55
        connected = get_connected(interface)
 
56
        print("Interface: %s" % interface)
 
57
        print("Connected: %s" % connected)
 
58
        try:
 
59
            print("IP: %s" % get_ip_address(interface))
 
60
        except IOError:
 
61
            print("IP: n/a")
 
62
        print("MAC: %s\n" % get_mac_address(interface))
 
63
 
 
64
    return 0
 
65
 
 
66
if __name__ == "__main__":
 
67
    sys.exit(main(sys.argv[1:]))