summaryrefslogtreecommitdiff
path: root/examples/shell/list-devices.sh
blob: 74342d417c9df1b6ec08be11e7333ca12e075eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0+
#
# 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"

# For the types see include/NetworkManager.h
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" ;;
   13) echo "Bridge" ;;
   14) echo "Generic" ;;
   15) echo "Team" ;;
   16) echo "TUN" ;;
   17) echo "IPTunnel" ;;
   18) echo "MACVLAN" ;;
   19) echo "VXLAN" ;;
   20) echo "Veth" ;;
    *) 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