diff options
Diffstat (limited to 'examples/shell')
| -rw-r--r-- | examples/shell/Makefile.am | 5 | ||||
| -rw-r--r-- | examples/shell/Makefile.in | 62 | ||||
| -rwxr-xr-x | examples/shell/active-wifi.sh | 88 | ||||
| -rwxr-xr-x | examples/shell/disconnect-device.sh | 77 | ||||
| -rwxr-xr-x | examples/shell/list-devices.sh | 105 |
5 files changed, 312 insertions, 25 deletions
diff --git a/examples/shell/Makefile.am b/examples/shell/Makefile.am index e4083d9a..e4b6f6c7 100644 --- a/examples/shell/Makefile.am +++ b/examples/shell/Makefile.am @@ -1,3 +1,6 @@ EXTRA_DIST = \ nm-logging.sh \ - get-hostname.sh + get-hostname.sh \ + list-devices.sh \ + disconnect-device.sh \ + active-wifi.sh diff --git a/examples/shell/Makefile.in b/examples/shell/Makefile.in index 03a59ea6..2f566949 100644 --- a/examples/shell/Makefile.in +++ b/examples/shell/Makefile.in @@ -54,16 +54,18 @@ subdir = examples/shell DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/compiler_warnings.m4 \ - $(top_srcdir)/m4/gettext.m4 $(top_srcdir)/m4/gtk-doc.m4 \ - $(top_srcdir)/m4/iconv.m4 $(top_srcdir)/m4/intlmacosx.m4 \ - $(top_srcdir)/m4/intltool.m4 $(top_srcdir)/m4/introspection.m4 \ - $(top_srcdir)/m4/lib-ld.m4 $(top_srcdir)/m4/lib-link.m4 \ - $(top_srcdir)/m4/lib-prefix.m4 $(top_srcdir)/m4/libnl-check.m4 \ - $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \ - $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ - $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/nls.m4 \ - $(top_srcdir)/m4/po.m4 $(top_srcdir)/m4/progtest.m4 \ - $(top_srcdir)/m4/vapigen.m4 $(top_srcdir)/configure.ac + $(top_srcdir)/m4/gettext.m4 \ + $(top_srcdir)/m4/gnome-code-coverage.m4 \ + $(top_srcdir)/m4/gtk-doc.m4 $(top_srcdir)/m4/iconv.m4 \ + $(top_srcdir)/m4/intlmacosx.m4 $(top_srcdir)/m4/intltool.m4 \ + $(top_srcdir)/m4/introspection.m4 $(top_srcdir)/m4/lib-ld.m4 \ + $(top_srcdir)/m4/lib-link.m4 $(top_srcdir)/m4/lib-prefix.m4 \ + $(top_srcdir)/m4/libnl-check.m4 $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/m4/nls.m4 $(top_srcdir)/m4/po.m4 \ + $(top_srcdir)/m4/progtest.m4 $(top_srcdir)/m4/vapigen.m4 \ + $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) mkinstalldirs = $(install_sh) -d @@ -96,6 +98,10 @@ AWK = @AWK@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ +CKDB_PATH = @CKDB_PATH@ +CODE_COVERAGE_CFLAGS = @CODE_COVERAGE_CFLAGS@ +CODE_COVERAGE_ENABLED = @CODE_COVERAGE_ENABLED@ +CODE_COVERAGE_LDFLAGS = @CODE_COVERAGE_LDFLAGS@ CPP = @CPP@ CPPFLAGS = @CPPFLAGS@ CXX = @CXX@ @@ -110,9 +116,7 @@ DBUS_SYS_DIR = @DBUS_SYS_DIR@ DEFS = @DEFS@ DEPDIR = @DEPDIR@ DHCLIENT_PATH = @DHCLIENT_PATH@ -DHCLIENT_VERSION = @DHCLIENT_VERSION@ DHCPCD_PATH = @DHCPCD_PATH@ -DISABLE_DEPRECATED = @DISABLE_DEPRECATED@ DLLTOOL = @DLLTOOL@ DSYMUTIL = @DSYMUTIL@ DUMPBIN = @DUMPBIN@ @@ -122,17 +126,14 @@ ECHO_T = @ECHO_T@ EGREP = @EGREP@ EXEEXT = @EXEEXT@ FGREP = @FGREP@ +GENHTML = @GENHTML@ GETTEXT_MACRO_VERSION = @GETTEXT_MACRO_VERSION@ GETTEXT_PACKAGE = @GETTEXT_PACKAGE@ -GIO_CFLAGS = @GIO_CFLAGS@ -GIO_LIBS = @GIO_LIBS@ GLIB_CFLAGS = @GLIB_CFLAGS@ GLIB_GENMARSHAL = @GLIB_GENMARSHAL@ GLIB_LIBS = @GLIB_LIBS@ GLIB_MAKEFILE = @GLIB_MAKEFILE@ GLIB_MKENUMS = @GLIB_MKENUMS@ -GMODULE_CFLAGS = @GMODULE_CFLAGS@ -GMODULE_LIBS = @GMODULE_LIBS@ GMSGFMT = @GMSGFMT@ GMSGFMT_015 = @GMSGFMT_015@ GNUTLS_CFLAGS = @GNUTLS_CFLAGS@ @@ -173,6 +174,7 @@ IPTABLES_PATH = @IPTABLES_PATH@ IWMX_SDK_CFLAGS = @IWMX_SDK_CFLAGS@ IWMX_SDK_LIBS = @IWMX_SDK_LIBS@ KERNEL_FIRMWARE_DIR = @KERNEL_FIRMWARE_DIR@ +LCOV = @LCOV@ LD = @LD@ LDFLAGS = @LDFLAGS@ LIBDL = @LIBDL@ @@ -189,11 +191,7 @@ LIBNL2_LIBS = @LIBNL2_LIBS@ LIBNL3_CFLAGS = @LIBNL3_CFLAGS@ LIBNL3_LIBS = @LIBNL3_LIBS@ LIBNL_CFLAGS = @LIBNL_CFLAGS@ -LIBNL_GENL3_CFLAGS = @LIBNL_GENL3_CFLAGS@ -LIBNL_GENL3_LIBS = @LIBNL_GENL3_LIBS@ LIBNL_LIBS = @LIBNL_LIBS@ -LIBNL_ROUTE3_CFLAGS = @LIBNL_ROUTE3_CFLAGS@ -LIBNL_ROUTE3_LIBS = @LIBNL_ROUTE3_LIBS@ LIBOBJS = @LIBOBJS@ LIBS = @LIBS@ LIBSOUP_CFLAGS = @LIBSOUP_CFLAGS@ @@ -208,6 +206,8 @@ MAINT = @MAINT@ MAKEINFO = @MAKEINFO@ MANIFEST_TOOL = @MANIFEST_TOOL@ MKDIR_P = @MKDIR_P@ +MM_GLIB_CFLAGS = @MM_GLIB_CFLAGS@ +MM_GLIB_LIBS = @MM_GLIB_LIBS@ MOC = @MOC@ MSGFMT = @MSGFMT@ MSGFMT_015 = @MSGFMT_015@ @@ -217,6 +217,7 @@ NMEDIT = @NMEDIT@ NM_MAJOR_VERSION = @NM_MAJOR_VERSION@ NM_MICRO_VERSION = @NM_MICRO_VERSION@ NM_MINOR_VERSION = @NM_MINOR_VERSION@ +NM_MODIFY_SYSTEM_POLICY = @NM_MODIFY_SYSTEM_POLICY@ NM_VERSION = @NM_VERSION@ NSS_CFLAGS = @NSS_CFLAGS@ NSS_LIBS = @NSS_LIBS@ @@ -243,13 +244,14 @@ PPPD_PLUGIN_DIR = @PPPD_PLUGIN_DIR@ QT_CFLAGS = @QT_CFLAGS@ QT_LIBS = @QT_LIBS@ RANLIB = @RANLIB@ -RESOLVCONF_PATH = @RESOLVCONF_PATH@ SED = @SED@ SET_MAKE = @SET_MAKE@ SHELL = @SHELL@ STRIP = @STRIP@ -SYSTEMD_CFLAGS = @SYSTEMD_CFLAGS@ -SYSTEMD_LIBS = @SYSTEMD_LIBS@ +SYSTEMD_INHIBIT_CFLAGS = @SYSTEMD_INHIBIT_CFLAGS@ +SYSTEMD_INHIBIT_LIBS = @SYSTEMD_INHIBIT_LIBS@ +SYSTEMD_LOGIN_CFLAGS = @SYSTEMD_LOGIN_CFLAGS@ +SYSTEMD_LOGIN_LIBS = @SYSTEMD_LOGIN_LIBS@ SYSTEM_CA_PATH = @SYSTEM_CA_PATH@ UDEV_BASE_DIR = @UDEV_BASE_DIR@ USE_NLS = @USE_NLS@ @@ -304,6 +306,11 @@ localedir = @localedir@ localstatedir = @localstatedir@ mandir = @mandir@ mkdir_p = @mkdir_p@ +nmbinary = @nmbinary@ +nmconfdir = @nmconfdir@ +nmdatadir = @nmdatadir@ +nmrundir = @nmrundir@ +nmstatedir = @nmstatedir@ oldincludedir = @oldincludedir@ pdfdir = @pdfdir@ prefix = @prefix@ @@ -318,9 +325,16 @@ target_alias = @target_alias@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ +with_dhclient = @with_dhclient@ +with_dhcpcd = @with_dhcpcd@ +with_netconfig = @with_netconfig@ +with_resolvconf = @with_resolvconf@ EXTRA_DIST = \ nm-logging.sh \ - get-hostname.sh + get-hostname.sh \ + list-devices.sh \ + disconnect-device.sh \ + active-wifi.sh all: all-am diff --git a/examples/shell/active-wifi.sh b/examples/shell/active-wifi.sh new file mode 100755 index 00000000..1c9466b7 --- /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 + diff --git a/examples/shell/disconnect-device.sh b/examples/shell/disconnect-device.sh new file mode 100755 index 00000000..045d64d7 --- /dev/null +++ b/examples/shell/disconnect-device.sh @@ -0,0 +1,77 @@ +#!/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 takes a device interface name as a parameter and tells +# NetworkManager to disconnect that device (using Disconnect()), closing +# down any network connection it may have + +NM_SERVICE_NAME="org.freedesktop.NetworkManager" +NM_OBJECT_PATH="/org/freedesktop/NetworkManager" +DEVICE_IFACE="org.freedesktop.NetworkManager.Device" +DEVICE_DISCONNECT=${DEVICE_IFACE}".Disconnect" +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_device_property() +{ + # first arg: device object path + # second arg: property name + # returns: property value + + dbus-send --system --print-reply --dest=$NM_SERVICE_NAME "$1" $DBUS_PROPERTIES_GET string:$DEVICE_IFACE string:"$2" | \ + grep "variant" | awk '{print $3}' | sed 's/"//g' +} + +disconnect_device () +{ + # first arg: device interface name + + for device in `get_devices` + do + DEV_INTERFACE=`get_device_property "$device" "Interface"` + + if [ "$DEV_INTERFACE" = "$1" ]; then + echo "Device with interface '$1' found -> disconnecting." + dbus-send --system --print-reply --dest=$NM_SERVICE_NAME $device $DEVICE_DISCONNECT + return 0 + fi + done + + echo "Device with interface '$1' not found." + return 1 +} + + +# --- main program --- +if [ ! -n "$1" ]; then + echo "Usage: `basename $0` <interface name>" + exit 2 +fi + +# disconnect device +disconnect_device $1 + diff --git a/examples/shell/list-devices.sh b/examples/shell/list-devices.sh new file mode 100755 index 00000000..4cf9c599 --- /dev/null +++ b/examples/shell/list-devices.sh @@ -0,0 +1,105 @@ +#!/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) 2011 - 2012 Red Hat, Inc. +# + +# +# This example lists basic information about network interfaces known to NM. +# It finds the devices via GetDevices() D-Bus call and then gets properties of +# each device. +# + +NM_SERVICE_NAME="org.freedesktop.NetworkManager" +NM_OBJECT_PATH="/org/freedesktop/NetworkManager" +DEVICE_IFACE="org.freedesktop.NetworkManager.Device" +NM_GET_DEVICES="org.freedesktop.NetworkManager.GetDevices" +DBUS_PROPERTIES_GET="org.freedesktop.DBus.Properties.Get" + +devtype_to_name() +{ + case $1 in + 1) echo "Ethernet" ;; + 2) echo "Wi-Fi" ;; + 5) echo "Bluetooth" ;; + 6) echo "OLPC" ;; + 7) echo "WiMAX" ;; + 8) echo "Modem" ;; + 9) echo "InfiniBand" ;; + 10) echo "Bond" ;; + 11) echo "VLAN" ;; + 12) echo "ADSL" ;; + *) echo "Unknown" ;; + esac +} + +state_to_name() +{ + case $1 in + 10) echo "Unmanaged" ;; + 20) echo "Unavailable" ;; + 30) echo "Disconnected" ;; + 40) echo "Prepare" ;; + 50) echo "Config" ;; + 60) echo "Need Auth" ;; + 70) echo "IP Config" ;; + 80) echo "IP Check" ;; + 90) echo "Secondaries" ;; + 100) echo "Activated" ;; + 110) echo "Deactivating" ;; + 120) echo "Failed" ;; + *) echo "Unknown" ;; + esac +} + +get_devices() +{ + dbus-send --system --print-reply --dest=$NM_SERVICE_NAME $NM_OBJECT_PATH $NM_GET_DEVICES | \ + grep "object path" | cut -d '"' -f2 +} + +get_device_property() +{ + # first arg: device object path + # second arg: property name + # returns: property value + + dbus-send --system --print-reply --dest=$NM_SERVICE_NAME "$1" $DBUS_PROPERTIES_GET string:$DEVICE_IFACE string:"$2" | \ + grep "variant" | awk '{print $3}' | sed 's/"//g' +} + +list_devices_details() +{ + for device in `get_devices` + do + DEV_INTERFACE=`get_device_property "$device" "Interface"` + DEV_TYPE=`get_device_property "$device" "DeviceType"` + DEV_DRIVER=`get_device_property "$device" "Driver"` + DEV_STATE=`get_device_property "$device" "State"` + + echo "============================" + echo "Interface: $DEV_INTERFACE" + echo "Type: `devtype_to_name $DEV_TYPE`" + echo "Driver: $DEV_DRIVER" + echo "State: `state_to_name $DEV_STATE`" + done +} + +# print devices details +list_devices_details + |