|
9 | 9 | # https://networkmanager.dev/docs/api/latest/ref-dbus-devices.html |
10 | 10 | import sdbus |
11 | 11 | from sdbus_block.networkmanager import ( |
| 12 | + ConnectionType, |
| 13 | + NetworkConnectionSettings, |
12 | 14 | NetworkManager, |
| 15 | + NetworkManagerSettings, |
13 | 16 | NetworkDeviceGeneric, |
14 | 17 | IPv4Config, |
15 | 18 | DeviceType, |
16 | 19 | NetworkDeviceWireless, |
17 | 20 | WiFiOperationMode, |
18 | 21 | AccessPoint, |
19 | 22 | ) |
| 23 | +from typing import Any, Dict, List, Optional, Tuple |
| 24 | +NetworkManagerAddressData = List[Dict[str, Tuple[str, Any]]] |
| 25 | + |
| 26 | + |
| 27 | +def get_most_recent_connection_id(ifname, dev_type) -> Optional[str]: |
| 28 | + """Return the most-recently used connection_id for this device |
| 29 | +
|
| 30 | + Besides getting the currently active connection, this will succeed |
| 31 | + in getting the most recent connection when a device is not connected |
| 32 | + at the moment this function is executed. |
| 33 | +
|
| 34 | + It uses getattr(ConnectionType, dev_type) to get the connection_type |
| 35 | + used for connection_profiles for this DeviceType. |
| 36 | +
|
| 37 | + With a slight modification, this could return the most recent connections |
| 38 | + of the given device, ordered by the time of the last use of them. |
| 39 | + """ |
| 40 | + settings_service = NetworkManagerSettings() |
| 41 | + connection_paths: List[str] = settings_service.connections |
| 42 | + conns = {} |
| 43 | + for connection_path in connection_paths: |
| 44 | + connection_manager = NetworkConnectionSettings(connection_path) |
| 45 | + connection = connection_manager.connection_profile().connection |
| 46 | + # Filter connection profiles matching the connection type for the device: |
| 47 | + if connection.connection_type != getattr(ConnectionType, dev_type): |
| 48 | + continue |
| 49 | + # If the interface_name of a connection profiles is set, it must match: |
| 50 | + if connection.interface_name and connection.interface_name != ifname: |
| 51 | + continue |
| 52 | + # If connection.timestamp is not set, it was never active. Set it to 0: |
| 53 | + if not connection.timestamp: |
| 54 | + connection.timestamp = 0 |
| 55 | + # Record the connection_ids of the matches, and timestamp is the key: |
| 56 | + conns[connection.timestamp] = connection.connection_id |
| 57 | + if not len(conns): |
| 58 | + return None |
| 59 | + # Returns the connection_id of the highest timestamp which was found: |
| 60 | + return conns.get(max(conns.keys())) |
20 | 61 |
|
21 | 62 |
|
22 | 63 | def list_networkdevice_details_blocking() -> None: |
23 | | - nm = NetworkManager() |
24 | | - devices_paths = nm.get_devices() |
25 | 64 |
|
26 | | - for device_path in devices_paths: |
| 65 | + for device_path in NetworkManager().get_devices(): |
27 | 66 | generic_device = NetworkDeviceGeneric(device_path) |
28 | | - device_ip4_conf_path = generic_device.ip4_config |
| 67 | + device_ip4_conf_path: str = generic_device.ip4_config |
29 | 68 | if device_ip4_conf_path == "/": |
30 | 69 | continue |
| 70 | + if not generic_device.managed: |
| 71 | + continue |
| 72 | + dev_type = DeviceType(generic_device.device_type).name |
| 73 | + if dev_type == DeviceType.BRIDGE.name: |
| 74 | + continue |
31 | 75 |
|
| 76 | + dev_name = generic_device.interface |
32 | 77 | ip4_conf = IPv4Config(device_ip4_conf_path) |
| 78 | + gateway: str = ip4_conf.gateway |
| 79 | + |
| 80 | + print("Type: ", dev_type.title()) |
| 81 | + print("Name: ", dev_name) |
33 | 82 |
|
34 | | - print("Device: ", generic_device.interface) |
35 | | - if ip4_conf.gateway: |
36 | | - print("Gateway:", ip4_conf.gateway) |
| 83 | + if gateway: |
| 84 | + print("Gateway:", gateway) |
37 | 85 |
|
38 | | - for ip4addr in ip4_conf.address_data: |
39 | | - print(f'Address: {ip4addr["address"][1]}/{ip4addr["prefix"][1]}') |
| 86 | + address_data: NetworkManagerAddressData = ip4_conf.address_data |
| 87 | + for inetaddr in address_data: |
| 88 | + print(f'Address: {inetaddr["address"][1]}/{inetaddr["prefix"][1]}') |
40 | 89 |
|
41 | | - for dns in ip4_conf.nameserver_data: |
| 90 | + nameservers: NetworkManagerAddressData = ip4_conf.nameserver_data |
| 91 | + for dns in nameservers: |
42 | 92 | print("DNS: ", dns["address"][1]) |
43 | 93 |
|
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")) |
| 94 | + if dev_type == DeviceType.WIFI.name: |
| 95 | + wifi = NetworkDeviceWireless(device_path) |
| 96 | + print("Wifi: ", WiFiOperationMode(wifi.mode).name.title()) |
| 97 | + ap = AccessPoint(wifi.active_access_point) |
| 98 | + ssid: bytes = ap.ssid |
| 99 | + if ssid: |
| 100 | + print("SSID: ", ssid.decode("utf-8", "ignore")) |
50 | 101 | if ap.strength: |
51 | 102 | print("Signal: ", ap.strength) |
| 103 | + connection_id = get_most_recent_connection_id(dev_name, dev_type) |
| 104 | + if connection_id: |
| 105 | + print("Profile:", connection_id) |
52 | 106 |
|
53 | 107 | print("") |
54 | 108 |
|
|
0 commit comments