summaryrefslogtreecommitdiff
path: root/examples/python/gi/get_ips.py
blob: 9f1853f17ea5608d9fbae0dfac05088c62361e55 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
#
# vim: ft=python ts=4 sts=4 sw=4 et ai
# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2014 Red Hat, Inc.
#

import sys, socket, struct
from gi.repository import GLib, NM

#
#  This example shows how to get addresses, routes and DNS information
#  from NMIP4Config and NMIP6Config (got out of NMDevice)
#

def show_addresses(self, family):
    if (family == socket.AF_INET):
       ip_cfg = self.get_ip4_config()
    else:
       ip_cfg = self.get_ip6_config()

    if ip_cfg is None:
        print("None")
        return

    nm_addresses = ip_cfg.get_addresses()
    if len(nm_addresses) == 0:
        print("None")
        return

    for nm_address in nm_addresses:
        addr = nm_address.get_address()
        prefix = nm_address.get_prefix()
        gateway = nm_address.get_gateway()

        if (family == socket.AF_INET):
            addr_struct = struct.pack("=I", addr)
            gateway_struct = struct.pack("=I", gateway)
        else:
            addr_struct = addr
            gateway_struct = gateway
        print("%s/%d  %s") % (socket.inet_ntop(family, addr_struct),
                              prefix,
                              socket.inet_ntop(family, gateway_struct))


def show_routes(self, family):
    if (family == socket.AF_INET):
       ip_cfg = self.get_ip4_config()
    else:
       ip_cfg = self.get_ip6_config()

    if ip_cfg is None:
        print("None")
        return

    nm_routes = ip_cfg.get_routes()
    if len(nm_routes) == 0:
        print("None")
        return

    for nm_route in nm_routes:
        dest = nm_route.get_dest()
        prefix = nm_route.get_prefix()
        next_hop = nm_route.get_next_hop()
        metric = nm_route.get_metric()

        if (family == socket.AF_INET):
            dest_struct = struct.pack("=I", dest)
            next_hop_struct = struct.pack("=I", next_hop)
        else:
            dest_struct = dest
            next_hop_struct = next_hop
        print("%s/%d  %s  %d") % (socket.inet_ntop(family, dest_struct),
                                  prefix,
                                  socket.inet_ntop(family, next_hop_struct),
                                  metric)


def show_dns(self, family):
    if (family == socket.AF_INET):
       ip_cfg = self.get_ip4_config()
    else:
       ip_cfg = self.get_ip6_config()

    if ip_cfg is None:
        print("None")
        return

    if (family == socket.AF_INET):
        print ("Domains: %s") % (ip_cfg.get_domains())
        print ("Searches: %s") % (ip_cfg.get_searches())
        print("Nameservers:")
        nameservers = ip_cfg.get_nameservers()
        for dns in nameservers:
            print socket.inet_ntop(family, struct.pack("=I", dns))
    else:
        print ("Domains: %s") % (ip_cfg.get_domains())
        print ("Searches: %s") % (ip_cfg.get_searches())
        print("Nameservers:")
        num = ip_cfg.get_num_nameservers()
        for i in range(0,num):
           dns = ip_cfg.get_nameserver(i)
           print socket.inet_ntop(family, dns)


if __name__ == "__main__":
    if len(sys.argv) != 2:
        sys.exit('Usage: %s <interface>' % sys.argv[0])
    dev_iface = sys.argv[1]

    c = NM.Client.new()
    dev = c.get_device_by_iface(dev_iface)
    if dev is None:
        sys.exit('Device \'%s\' not found' % dev_iface)
    print "Device: %s - %s" % (dev_iface, dev.get_device_type().value_name)
    print "---------------------------------------"

    print("IPv4 addresses:")
    print("---------------")
    show_addresses(dev, socket.AF_INET)
    print

    print("IPv4 routes:")
    print("------------")
    show_routes(dev, socket.AF_INET)
    print

    print "IPv6 addresses:"
    print("---------------")
    show_addresses(dev, socket.AF_INET6)
    print

    print "IPv6 routes:"
    print("------------")
    show_routes(dev, socket.AF_INET6)
    print

    print "IPv4 DNS:"
    print("------------")
    show_dns(dev, socket.AF_INET)
    print

    print "IPv6 DNS:"
    print("------------")
    show_dns(dev, socket.AF_INET6)
    print