summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2015-07-08 09:29:59 -0500
committerDan Williams <dcbw@redhat.com>2015-07-08 09:31:22 -0500
commitd1d048c93da6190ac97acb710f786479efd67e88 (patch)
tree899973ca575496f869e4ceb10d350d9663653fe2
parentce2964c021e1164081855fd9eefebf586356ebeb (diff)
downloadNetworkManager-d1d048c93da6190ac97acb710f786479efd67e88.tar.gz
examples: add python+dbus example to print active access point
-rw-r--r--examples/python/dbus/Makefile.am3
-rwxr-xr-xexamples/python/dbus/wifi-active-ap.py66
2 files changed, 68 insertions, 1 deletions
diff --git a/examples/python/dbus/Makefile.am b/examples/python/dbus/Makefile.am
index 6eab4889ac..a6aedcd159 100644
--- a/examples/python/dbus/Makefile.am
+++ b/examples/python/dbus/Makefile.am
@@ -13,4 +13,5 @@ EXTRA_DIST = \
update-ip4-method.py \
is-wwan-default.py \
wifi-hotspot.py \
- create-bond.py
+ create-bond.py \
+ wifi-active-ap.py
diff --git a/examples/python/dbus/wifi-active-ap.py b/examples/python/dbus/wifi-active-ap.py
new file mode 100755
index 0000000000..3b17c6f130
--- /dev/null
+++ b/examples/python/dbus/wifi-active-ap.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+#
+# 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) 2015 Red Hat, Inc.
+#
+
+#
+# This example starts or stops a wifi hotspot
+#
+# Configuration settings are described at
+# https://developer.gnome.org/NetworkManager/1.0/ref-settings.html
+#
+
+import dbus, sys, time
+
+bus = dbus.SystemBus()
+service_name = "org.freedesktop.NetworkManager"
+proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings")
+settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
+
+if len(sys.argv) != 2:
+ print "Usage: %s <ifname>" % sys.argv[0]
+ sys.exit(0)
+
+# Get the device object path based on interface name
+iface = sys.argv[1]
+proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager")
+nm = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
+devpath = nm.GetDeviceByIpIface(iface)
+
+# Get a proxy to the wifi device and get the active access point's object path
+proxy = bus.get_object(service_name, devpath)
+props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
+active_ap_path = props.Get("org.freedesktop.NetworkManager.Device.Wireless", "ActiveAccessPoint")
+if active_ap_path == "/":
+ print "%s is not currently associated" % sys.argv[1]
+ sys.exit(0)
+
+# Get the active access point's SSID and BSSID
+ap_proxy = bus.get_object(service_name, active_ap_path)
+ap_props = dbus.Interface(ap_proxy, "org.freedesktop.DBus.Properties")
+raw_ssid = ap_props.Get("org.freedesktop.NetworkManager.AccessPoint", "Ssid")
+bssid = ap_props.Get("org.freedesktop.NetworkManager.AccessPoint", "HwAddress")
+
+# Convert the SSID from a byte array to a string, assuming ASCII encoding
+ssid = ""
+for c in raw_ssid:
+ ssid = ssid + chr(c)
+
+print "%s is associated to '%s' (%s)" % (sys.argv[1], ssid, bssid)
+
+sys.exit(0)
+