about summary refs log tree commit diff
path: root/examples/python/gi/get_ips.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/python/gi/get_ips.py')
-rwxr-xr-xexamples/python/gi/get_ips.py90
1 files changed, 43 insertions, 47 deletions
diff --git a/examples/python/gi/get_ips.py b/examples/python/gi/get_ips.py
index 6903b6de..690bd6ef 100755
--- a/examples/python/gi/get_ips.py
+++ b/examples/python/gi/get_ips.py
@@ -17,22 +17,22 @@
 # 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.
+# Copyright 2014 Red Hat, Inc.
 #
 
-import sys, socket, struct
-from gi.repository import GLib, NetworkManager, NMClient
+import sys, socket
+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):
+def show_addresses(dev, family):
     if (family == socket.AF_INET):
-       ip_cfg = self.get_ip4_config()
+       ip_cfg = dev.get_ip4_config()
     else:
-       ip_cfg = self.get_ip6_config()
+       ip_cfg = dev.get_ip6_config()
 
     if ip_cfg is None:
         print("None")
@@ -46,24 +46,29 @@ def show_addresses(self, family):
     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))
+        print("%s/%d") % (addr, prefix)
 
+def show_gateway(dev, family):
+    if (family == socket.AF_INET):
+        ip_cfg = dev.get_ip4_config()
+    else:
+        ip_cfg = dev.get_ip6_config()
+
+    if ip_cfg is None:
+        gw = "None"
+    else:
+        gw = ip_cfg.get_gateway()
+        if gw == '':
+            gw = "None"
+
+    print(gw)
 
-def show_routes(self, family):
+def show_routes(dev, family):
     if (family == socket.AF_INET):
-       ip_cfg = self.get_ip4_config()
+       ip_cfg = dev.get_ip4_config()
     else:
-       ip_cfg = self.get_ip6_config()
+       ip_cfg = dev.get_ip6_config()
 
     if ip_cfg is None:
         print("None")
@@ -80,43 +85,24 @@ def show_routes(self, family):
         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)
+        print("%s/%d  %s  %d") % (dest, prefix, next_hop, metric)
 
 
-def show_dns(self, family):
+def show_dns(dev, family):
     if (family == socket.AF_INET):
-       ip_cfg = self.get_ip4_config()
+       ip_cfg = dev.get_ip4_config()
     else:
-       ip_cfg = self.get_ip6_config()
+       ip_cfg = dev.get_ip6_config()
 
     if ip_cfg is None:
         print("None")
         return
 
+    print ("Nameservers: %s") % (ip_cfg.get_nameservers())
+    print ("Domains: %s") % (ip_cfg.get_domains())
+    print ("Searches: %s") % (ip_cfg.get_searches())
     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)
+        print ("WINS: %s") % (ip_cfg.get_wins_servers())
 
 
 if __name__ == "__main__":
@@ -124,7 +110,7 @@ if __name__ == "__main__":
         sys.exit('Usage: %s <interface>' % sys.argv[0])
     dev_iface = sys.argv[1]
 
-    c = NMClient.Client.new()
+    c = NM.Client.new(None)
     dev = c.get_device_by_iface(dev_iface)
     if dev is None:
         sys.exit('Device \'%s\' not found' % dev_iface)
@@ -136,6 +122,11 @@ if __name__ == "__main__":
     show_addresses(dev, socket.AF_INET)
     print
 
+    print("IPv4 gateway:")
+    print("-------------")
+    show_gateway(dev, socket.AF_INET)
+    print
+
     print("IPv4 routes:")
     print("------------")
     show_routes(dev, socket.AF_INET)
@@ -146,6 +137,11 @@ if __name__ == "__main__":
     show_addresses(dev, socket.AF_INET6)
     print
 
+    print("IPv6 gateway:")
+    print("-------------")
+    show_gateway(dev, socket.AF_INET6)
+    print
+
     print "IPv6 routes:"
     print("------------")
     show_routes(dev, socket.AF_INET6)