summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJiří Klimeš <jklimes@redhat.com>2012-09-03 11:04:52 +0200
committerJiří Klimeš <jklimes@redhat.com>2012-09-03 11:57:49 +0200
commitef4b0f1d77868aeb9cc46379e75923a831e227b4 (patch)
tree329c9828b63e693a7a0d5f3fc4d56a6653b18ed6 /examples
parent02478a807d03b3c6fc614b1080185682f7829bc7 (diff)
examples: add a shell example listing active Wi-Fi networks on Wi-Fi devices
Diffstat (limited to 'examples')
-rw-r--r--examples/shell/Makefile.am3
-rwxr-xr-xexamples/shell/active-wifi.sh88
2 files changed, 90 insertions, 1 deletions
diff --git a/examples/shell/Makefile.am b/examples/shell/Makefile.am
index ff35998a7e..e4b6f6c795 100644
--- a/examples/shell/Makefile.am
+++ b/examples/shell/Makefile.am
@@ -2,4 +2,5 @@ EXTRA_DIST = \
nm-logging.sh \
get-hostname.sh \
list-devices.sh \
- disconnect-device.sh
+ disconnect-device.sh \
+ active-wifi.sh
diff --git a/examples/shell/active-wifi.sh b/examples/shell/active-wifi.sh
new file mode 100755
index 0000000000..1c9466b7a6
--- /dev/null
+++ b/examples/shell/active-wifi.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+# vim: ft=sh ts=2 sts=2 sw=2 et ai
+# -*- Mode: sh; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+#
+# 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) 2012 Red Hat, Inc.
+#
+
+#
+# This example prints SSIDs of connected Wi-Fi networks.
+# GetDevices() D-Bus call returns all devices. We go through them, find Wi-Fi
+# ones and ask for ActiveAccessPoint property. If the access point path is not
+# empty ("/"), we can read its properties, like SSID.
+#
+
+NM_SERVICE_NAME="org.freedesktop.NetworkManager"
+NM_OBJECT_PATH="/org/freedesktop/NetworkManager"
+DEVICE_IFACE="org.freedesktop.NetworkManager.Device"
+DEVICE_WIFI_IFACE="org.freedesktop.NetworkManager.Device.Wireless"
+ACCESS_POINT_IFACE="org.freedesktop.NetworkManager.AccessPoint"
+NM_GET_DEVICES="org.freedesktop.NetworkManager.GetDevices"
+DBUS_PROPERTIES_GET="org.freedesktop.DBus.Properties.Get"
+
+
+get_devices()
+{
+ dbus-send --system --print-reply --dest=$NM_SERVICE_NAME $NM_OBJECT_PATH $NM_GET_DEVICES | \
+ grep "object path" | cut -d '"' -f2
+}
+
+get_property()
+{
+ # first arg: object path
+ # second arg: interface
+ # third arg: property name
+ # returns: property value
+
+ dbus-send --system --print-reply --dest=$NM_SERVICE_NAME "$1" $DBUS_PROPERTIES_GET string:"$2" string:"$3" | \
+ grep "variant" | awk -F '"' '{ if (NF == 1) {FS=" "; n=split($0,a); print a[n];} else print $(NF-1); }'
+}
+
+is_wifi_device()
+{
+ DEV_TYPE=`get_property $device $DEVICE_IFACE "DeviceType"`
+
+ if [ $DEV_TYPE -eq 2 ]; then
+ return 0
+ else
+ return 1
+ fi
+}
+
+show_active_ssids()
+{
+ for device in `get_devices`
+ do
+ INTERFACE=`get_property $device $DEVICE_IFACE "Interface"`
+
+ if `is_wifi_device`; then
+ ACTIVE_AP=`get_property $device $DEVICE_WIFI_IFACE "ActiveAccessPoint"`
+ if [ "$ACTIVE_AP" != "/" ]; then
+ SSID=`get_property $ACTIVE_AP $ACCESS_POINT_IFACE "Ssid"`
+ BSSID=`get_property $ACTIVE_AP $ACCESS_POINT_IFACE "HwAddress"`
+ echo "Device '$INTERFACE' is connected to '$SSID' (BSSID=$BSSID)"
+ else
+ echo "No active AP on device '$INTERFACE'"
+ fi
+ fi
+ done
+}
+
+# --- main program ---
+# print currently connected SSID on all Wi-Fi devices
+show_active_ssids
+