Skip to content

Commit 46a7d54

Browse files
authored
Merge pull request #3 from bernhardkaindl/examples/list-connections
Example for listing connection profiles
2 parents 8caf47c + 43f7f28 commit 46a7d54

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
4+
# Example which lists the details of NetworkManager's connection profiles.
5+
# This is the asyncio variant of this example using sdbus_async.networkmanager.
6+
# The blocking variant of this example is examples/block/list-connections.py
7+
#
8+
# Configuration settings are described at
9+
# https://networkmanager.dev/docs/api/latest/ref-settings.html
10+
#
11+
# Example output:
12+
# | name: Wired connection 1
13+
# | uuid: b2caabdc-98bb-3f88-8d28-d10369d6ded9
14+
# | type: 802-3-ethernet
15+
# | interface-name: enx001e101f0000
16+
# | ipv4: method: manual
17+
# | ipaddr: 192.168.178.34/24
18+
# | route-metric: 200
19+
# | ipv6: method: disabled
20+
import asyncio
21+
import sdbus
22+
from sdbus_async.networkmanager import (
23+
NetworkManagerSettings,
24+
NetworkConnectionSettings,
25+
)
26+
from typing import List
27+
28+
29+
async def list_connection_profiles_async() -> None:
30+
networkmanager_settings = NetworkManagerSettings()
31+
connections_paths: List[str] = await networkmanager_settings.connections
32+
for connection_path in connections_paths:
33+
connection_settings = NetworkConnectionSettings(connection_path)
34+
settings = await connection_settings.get_settings()
35+
connection = settings["connection"]
36+
37+
# Skip connection profiles for bridges and wireless networks
38+
if connection["type"][1] in ("bridge", "802-11-wireless"):
39+
continue
40+
41+
print("-------------------------------------------------------------")
42+
print("name:", connection["id"][1])
43+
print("uuid:", connection["uuid"][1])
44+
print("type:", connection["type"][1])
45+
if "interface-name" in connection:
46+
print(" interface-name:", connection["interface-name"][1])
47+
48+
if "ipv4" in settings:
49+
ipv4 = settings["ipv4"]
50+
print("ipv4: method:", ipv4["method"][1])
51+
if "address-data" in ipv4:
52+
for a in ipv4["address-data"][1]:
53+
print(f' ipaddr: {a["address"][1]}/{a["prefix"][1]}')
54+
if "route-metric" in ipv4:
55+
print(f' route-metric: {ipv4["route-metric"][1]}')
56+
57+
if "ipv6" in settings:
58+
print("ipv6: method:", settings["ipv6"]["method"][1])
59+
60+
61+
if __name__ == "__main__":
62+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
63+
asyncio.run(list_connection_profiles_async())

examples/block/list-connections.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
4+
# Example which lists the details of NetworkManager's connection profiles.
5+
#
6+
# Configuration settings are described at
7+
# https://networkmanager.dev/docs/api/latest/ref-settings.html
8+
#
9+
# Example output:
10+
# | name: Wired connection 1
11+
# | uuid: b2caabdc-98bb-3f88-8d28-d10369d6ded9
12+
# | type: 802-3-ethernet
13+
# | interface-name: enx001e101f0000
14+
# | ipv4: method: manual
15+
# | ipaddr: 192.168.178.34/24
16+
# | route-metric: 200
17+
# | ipv6: method: disabled
18+
import sdbus
19+
from sdbus_block.networkmanager import (
20+
NetworkManagerSettings,
21+
NetworkConnectionSettings,
22+
)
23+
24+
25+
def list_connection_profiles_blocking() -> None:
26+
for connection_path in NetworkManagerSettings().connections:
27+
connection_settings = NetworkConnectionSettings(connection_path)
28+
settings = connection_settings.get_settings()
29+
connection = settings["connection"]
30+
31+
# Skip connection profiles for bridges and wireless networks
32+
if connection["type"][1] in ("bridge", "802-11-wireless"):
33+
continue
34+
35+
print("-------------------------------------------------------------")
36+
print("name:", connection["id"][1])
37+
print("uuid:", connection["uuid"][1])
38+
print("type:", connection["type"][1])
39+
if "interface-name" in connection:
40+
print(" interface-name:", connection["interface-name"][1])
41+
42+
if "ipv4" in settings:
43+
ipv4 = settings["ipv4"]
44+
print("ipv4: method:", ipv4["method"][1])
45+
if "address-data" in ipv4:
46+
for a in ipv4["address-data"][1]:
47+
print(f' ipaddr: {a["address"][1]}/{a["prefix"][1]}')
48+
if "route-metric" in ipv4:
49+
print(f' route-metric: {ipv4["route-metric"][1]}')
50+
51+
if "ipv6" in settings:
52+
print("ipv6: method:", settings["ipv6"]["method"][1])
53+
54+
55+
if __name__ == "__main__":
56+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
57+
list_connection_profiles_blocking()

0 commit comments

Comments
 (0)