|
| 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()) |
0 commit comments