Skip to content

Commit 106b096

Browse files
author
Bernhard Kaindl
committed
Example to list the active IPv4 config of netdevs
1 parent bba41fb commit 106b096

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

examples/async/netdevinfo-async.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
4+
# Example to list the active IPv4 protocol configuration of network devices
5+
# and the current status of WiFi adapters
6+
#
7+
# For IPv4 and org.freedesktop.NetworkManager.Device.Wireless see:
8+
# https://networkmanager.dev/docs/api/latest/settings-ipv4.html
9+
# https://networkmanager.dev/docs/api/latest/ref-dbus-devices.html
10+
import asyncio
11+
import sdbus
12+
from sdbus_async.networkmanager import (
13+
NetworkManager,
14+
NetworkDeviceGeneric,
15+
IPv4Config,
16+
DeviceType,
17+
NetworkDeviceWireless,
18+
WiFiOperationMode,
19+
AccessPoint,
20+
)
21+
from typing import Any, Dict, List, Tuple
22+
NetworkManagerAddressData = List[Dict[str, Tuple[str, Any]]]
23+
24+
25+
async def list_networkdevice_details_async() -> None:
26+
nm = NetworkManager()
27+
devices_paths = await nm.get_devices()
28+
29+
for device_path in devices_paths:
30+
generic_device = NetworkDeviceGeneric(device_path)
31+
device_ip4_conf_path: str = await generic_device.ip4_config
32+
if device_ip4_conf_path == "/":
33+
continue
34+
35+
ip4_conf = IPv4Config(device_ip4_conf_path)
36+
gateway: str = await ip4_conf.gateway
37+
38+
print("Device: ", await generic_device.interface)
39+
if gateway:
40+
print("Gateway:", gateway)
41+
42+
address_data: NetworkManagerAddressData = await ip4_conf.address_data
43+
for inetaddr in address_data:
44+
print(f'Address: {inetaddr["address"][1]}/{inetaddr["prefix"][1]}')
45+
46+
nameservers: NetworkManagerAddressData = await ip4_conf.nameserver_data
47+
for dns in nameservers:
48+
print("DNS: ", dns["address"][1])
49+
50+
if await generic_device.device_type == DeviceType.WIFI:
51+
wifi = NetworkDeviceWireless(device_path)
52+
print("Wifi: ", WiFiOperationMode(await wifi.mode).name.title())
53+
ap = AccessPoint(await wifi.active_access_point)
54+
ssid: bytes = await ap.ssid
55+
if ssid:
56+
print("SSID: ", ssid.decode("utf-8", "ignore"))
57+
if await ap.strength:
58+
print("Signal: ", await ap.strength)
59+
60+
print("")
61+
62+
63+
if __name__ == "__main__":
64+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
65+
asyncio.run(list_networkdevice_details_async())

examples/block/netdevinfo.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
4+
# Example to list the active IPv4 protocol configuration of network devices
5+
# and the current status of WiFi adapters
6+
#
7+
# For IPv4 and org.freedesktop.NetworkManager.Device.Wireless see:
8+
# https://networkmanager.dev/docs/api/latest/settings-ipv4.html
9+
# https://networkmanager.dev/docs/api/latest/ref-dbus-devices.html
10+
import sdbus
11+
from sdbus_block.networkmanager import (
12+
NetworkManager,
13+
NetworkDeviceGeneric,
14+
IPv4Config,
15+
DeviceType,
16+
NetworkDeviceWireless,
17+
WiFiOperationMode,
18+
AccessPoint,
19+
)
20+
21+
22+
def list_networkdevice_details_blocking() -> None:
23+
nm = NetworkManager()
24+
devices_paths = nm.get_devices()
25+
26+
for device_path in devices_paths:
27+
generic_device = NetworkDeviceGeneric(device_path)
28+
device_ip4_conf_path = generic_device.ip4_config
29+
if device_ip4_conf_path == "/":
30+
continue
31+
32+
ip4_conf = IPv4Config(device_ip4_conf_path)
33+
34+
print("Device: ", generic_device.interface)
35+
if ip4_conf.gateway:
36+
print("Gateway:", ip4_conf.gateway)
37+
38+
for ip4addr in ip4_conf.address_data:
39+
print(f'Address: {ip4addr["address"][1]}/{ip4addr["prefix"][1]}')
40+
41+
for dns in ip4_conf.nameserver_data:
42+
print("DNS: ", dns["address"][1])
43+
44+
if generic_device.device_type == DeviceType.WIFI:
45+
wifidevice = NetworkDeviceWireless(device_path)
46+
print("Wifi: ", WiFiOperationMode(wifidevice.mode).name.title())
47+
ap = AccessPoint(wifidevice.active_access_point)
48+
if ap.ssid:
49+
print("SSID: ", ap.ssid.decode("utf-8", "ignore"))
50+
if ap.strength:
51+
print("Signal: ", ap.strength)
52+
53+
print("")
54+
55+
56+
if __name__ == "__main__":
57+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
58+
list_networkdevice_details_blocking()

0 commit comments

Comments
 (0)