Skip to content

Commit cb4b780

Browse files
authored
Merge pull request #1 from bernhardkaindl/examples/async/devicegeneric
Thank you again. I will look in to other merge requests in upcoming days.
2 parents bba41fb + 1fa24a7 commit cb4b780

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
4+
# Example to list the network devices including type, state, internet
5+
# connectivitycheck state and the identifier of the active connection.
6+
#
7+
# NetworkDeviceGeneric/org.freedesktop.NetworkManager.Device is described at
8+
# https://networkmanager.dev/docs/api/latest/ref-dbus-devices.html
9+
#
10+
# The output resembles the output of the NM CLI command "nmcli device":
11+
#
12+
# Interface Type State Internet Connection
13+
# lo Generic Unmanaged Unknown
14+
# wlp0s20f3 Wifi Activated Full Wolke7 [primary connection]
15+
# docker0 Bridge Activated None docker0
16+
# enx0c3796090408 Ethernet Activated Full enx0c3796090408
17+
# p2p-dev-wlp0s20f3 Wifi_P2P Disconnected None
18+
19+
import argparse
20+
import asyncio
21+
import sdbus
22+
from sdbus_async.networkmanager import (
23+
NetworkManager,
24+
NetworkDeviceGeneric,
25+
DeviceState,
26+
DeviceType,
27+
DeviceCapabilities as Capabilities,
28+
ActiveConnection,
29+
ConnectivityState,
30+
)
31+
from enum import Enum
32+
33+
34+
def title(enum: Enum) -> str:
35+
"""Get the name of an enum: 1st character is uppercase, rest lowercase"""
36+
return enum.name.title()
37+
38+
39+
async def list_active_hardware_networkdevice_states(only_hw: bool) -> None:
40+
"""Print the list of activated network devices similar to nmcli device"""
41+
nm = NetworkManager()
42+
devices_paths = await nm.get_devices()
43+
44+
print("Interface Type State Internet Connection")
45+
for device_path in devices_paths:
46+
generic = NetworkDeviceGeneric(device_path)
47+
48+
# Demonstrates an enum to match devices using capabilities:
49+
if only_hw and await generic.capabilities & Capabilities.IS_SOFTWARE:
50+
continue
51+
52+
# Create the strings for the columns using the names of the enums:
53+
dev: str = await generic.interface
54+
type = title(DeviceType(await generic.device_type))
55+
state = title(DeviceState(await generic.state))
56+
connectivity = title(ConnectivityState(await generic.ip4_connectivity))
57+
58+
if await generic.active_connection == "/": # No active connection
59+
id = ""
60+
else:
61+
# ActiveConnection() gets propertites from active connection path:
62+
active_conn = ActiveConnection(await generic.active_connection)
63+
id: str = await active_conn.id
64+
if await active_conn.default:
65+
id += " [primary connection]"
66+
67+
print(f"{dev:<17} {type:<8} {state:<12} {connectivity:<8} {id:<14}")
68+
69+
70+
if __name__ == "__main__":
71+
p = argparse.ArgumentParser()
72+
p.add_argument("--hw", action="store_true", dest="only_hw", help="Only HW")
73+
args = p.parse_args()
74+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
75+
asyncio.run(list_active_hardware_networkdevice_states(args.only_hw))

examples/block/device-state.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
4+
# Example to list the network devices including type, state, internet
5+
# connectivitycheck state and the identifier of the active connection.
6+
#
7+
# NetworkDeviceGeneric/org.freedesktop.NetworkManager.Device is described at
8+
# https://networkmanager.dev/docs/api/latest/ref-dbus-devices.html
9+
#
10+
# The output resembles the output of the NM CLI command "nmcli device":
11+
#
12+
# Interface Type State Internet Connection
13+
# lo Generic Unmanaged Unknown
14+
# wlp0s20f3 Wifi Activated Full Wolke7 [primary connection]
15+
# docker0 Bridge Activated None docker0
16+
# enx0c3796090408 Ethernet Activated Full enx0c3796090408
17+
# p2p-dev-wlp0s20f3 Wifi_P2P Disconnected None
18+
19+
import argparse
20+
import sdbus
21+
from sdbus_block.networkmanager import (
22+
NetworkManager,
23+
NetworkDeviceGeneric,
24+
DeviceState,
25+
DeviceType,
26+
DeviceCapabilities as Capabilities,
27+
ActiveConnection,
28+
ConnectivityState,
29+
)
30+
from enum import Enum
31+
32+
33+
def title(enum: Enum) -> str:
34+
"""Get the name of an enum: 1st character is uppercase, rest lowercase"""
35+
return enum.name.title()
36+
37+
38+
def list_active_hardware_networkdevice_states(only_hw: bool) -> None:
39+
"""Print the list of activated network devices similar to nmcli device"""
40+
nm = NetworkManager()
41+
devices_paths = nm.get_devices()
42+
43+
print("Interface Type State Internet Connection")
44+
for device_path in devices_paths:
45+
generic_dev = NetworkDeviceGeneric(device_path)
46+
47+
# Demonstrates an enum to match devices using capabilities:
48+
if only_hw and generic_dev.capabilities & Capabilities.IS_SOFTWARE:
49+
continue
50+
51+
# Create the strings for the columns using the names of the enums:
52+
dev = generic_dev.interface
53+
type = title(DeviceType(generic_dev.device_type))
54+
state = title(DeviceState(generic_dev.state))
55+
connectivity = title(ConnectivityState(generic_dev.ip4_connectivity))
56+
57+
if generic_dev.active_connection == "/": # No active connection
58+
id = ""
59+
else:
60+
# ActiveConnection() gets propertites from active connection path:
61+
active_connection = ActiveConnection(generic_dev.active_connection)
62+
id = active_connection.id
63+
if active_connection.default:
64+
id += " [primary connection]"
65+
66+
print(f"{dev:<17} {type:<8} {state:<12} {connectivity:<8} {id:<14}")
67+
68+
69+
if __name__ == "__main__":
70+
p = argparse.ArgumentParser()
71+
p.add_argument("--hw", action="store_true", dest="only_hw", help="Only HW")
72+
args = p.parse_args()
73+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
74+
list_active_hardware_networkdevice_states(args.only_hw)

0 commit comments

Comments
 (0)