d4c1522
From 85272df6eb89682a3d13022281085b97527955e1 Mon Sep 17 00:00:00 2001
d4c1522
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes@redhat.com>
d4c1522
Date: Wed, 22 Jan 2014 15:40:22 +0100
d4c1522
Subject: [PATCH] examples: update get_ips.py python example for DNS
d4c1522
 information
d4c1522
MIME-Version: 1.0
d4c1522
Content-Type: text/plain; charset=UTF-8
d4c1522
Content-Transfer-Encoding: 8bit
d4c1522
d4c1522
d4c1522
Signed-off-by: Jiří Klimeš <jklimes@redhat.com>
d4c1522
---
d4c1522
 examples/python/gi/get_ips.py | 44 +++++++++++++++++++++++++++++++++++++++----
d4c1522
 1 file changed, 40 insertions(+), 4 deletions(-)
d4c1522
d4c1522
diff --git a/examples/python/gi/get_ips.py b/examples/python/gi/get_ips.py
d4c1522
index b1e59e2..6903b6d 100755
d4c1522
--- a/examples/python/gi/get_ips.py
d4c1522
+++ b/examples/python/gi/get_ips.py
d4c1522
@@ -24,8 +24,8 @@ import sys, socket, struct
d4c1522
 from gi.repository import GLib, NetworkManager, NMClient
d4c1522
 
d4c1522
 #
d4c1522
-#  This example shows how to get get addresses and routes from NMIP4Config and NMIP6Config
d4c1522
-#  (got out of NMDevice)
d4c1522
+#  This example shows how to get addresses, routes and DNS information
d4c1522
+#  from NMIP4Config and NMIP6Config (got out of NMDevice)
d4c1522
 #
d4c1522
 
d4c1522
 def show_addresses(self, family):
d4c1522
@@ -59,7 +59,6 @@ def show_addresses(self, family):
d4c1522
                               socket.inet_ntop(family, gateway_struct))
d4c1522
 
d4c1522
 
d4c1522
-
d4c1522
 def show_routes(self, family):
d4c1522
     if (family == socket.AF_INET):
d4c1522
        ip_cfg = self.get_ip4_config()
d4c1522
@@ -70,7 +69,7 @@ def show_routes(self, family):
d4c1522
         print("None")
d4c1522
         return
d4c1522
 
d4c1522
-    nm_routes = ip_cfg.get_routes() 
d4c1522
+    nm_routes = ip_cfg.get_routes()
d4c1522
     if len(nm_routes) == 0:
d4c1522
         print("None")
d4c1522
         return
d4c1522
@@ -93,6 +92,33 @@ def show_routes(self, family):
d4c1522
                                   metric)
d4c1522
 
d4c1522
 
d4c1522
+def show_dns(self, family):
d4c1522
+    if (family == socket.AF_INET):
d4c1522
+       ip_cfg = self.get_ip4_config()
d4c1522
+    else:
d4c1522
+       ip_cfg = self.get_ip6_config()
d4c1522
+
d4c1522
+    if ip_cfg is None:
d4c1522
+        print("None")
d4c1522
+        return
d4c1522
+
d4c1522
+    if (family == socket.AF_INET):
d4c1522
+        print ("Domains: %s") % (ip_cfg.get_domains())
d4c1522
+        print ("Searches: %s") % (ip_cfg.get_searches())
d4c1522
+        print("Nameservers:")
d4c1522
+        nameservers = ip_cfg.get_nameservers()
d4c1522
+        for dns in nameservers:
d4c1522
+            print socket.inet_ntop(family, struct.pack("=I", dns))
d4c1522
+    else:
d4c1522
+        print ("Domains: %s") % (ip_cfg.get_domains())
d4c1522
+        print ("Searches: %s") % (ip_cfg.get_searches())
d4c1522
+        print("Nameservers:")
d4c1522
+        num = ip_cfg.get_num_nameservers()
d4c1522
+        for i in range(0,num):
d4c1522
+           dns = ip_cfg.get_nameserver(i)
d4c1522
+           print socket.inet_ntop(family, dns)
d4c1522
+
d4c1522
+
d4c1522
 if __name__ == "__main__":
d4c1522
     if len(sys.argv) != 2:
d4c1522
         sys.exit('Usage: %s <interface>' % sys.argv[0])
d4c1522
@@ -125,3 +151,13 @@ if __name__ == "__main__":
d4c1522
     show_routes(dev, socket.AF_INET6)
d4c1522
     print
d4c1522
 
d4c1522
+    print "IPv4 DNS:"
d4c1522
+    print("------------")
d4c1522
+    show_dns(dev, socket.AF_INET)
d4c1522
+    print
d4c1522
+
d4c1522
+    print "IPv6 DNS:"
d4c1522
+    print("------------")
d4c1522
+    show_dns(dev, socket.AF_INET6)
d4c1522
+    print
d4c1522
+
d4c1522
-- 
d4c1522
1.7.11.7
d4c1522