|
| 1 | +Examples |
| 2 | +================== |
| 3 | + |
| 4 | +Listing interfaces |
| 5 | +------------------ |
| 6 | + |
| 7 | +List interface and their IPv4 adress using |
| 8 | +blocking API: |
| 9 | + |
| 10 | +.. code-block:: python |
| 11 | +
|
| 12 | + from sdbus_block.networkmanager import ( |
| 13 | + NetworkManager, |
| 14 | + NetworkDeviceGeneric, |
| 15 | + IPv4Config, |
| 16 | + ) |
| 17 | + from sdbus import sd_bus_open_system |
| 18 | +
|
| 19 | + system_bus = sd_bus_open_system() # We need system bus |
| 20 | +
|
| 21 | + nm = NetworkManager(system_bus) |
| 22 | +
|
| 23 | + devices_paths = nm.get_devices() |
| 24 | +
|
| 25 | + for device_path in devices_paths: |
| 26 | + generic_device = NetworkDeviceGeneric(device_path, system_bus) |
| 27 | + print('Device: ', generic_device.interface) |
| 28 | + device_ip4_conf_path = generic_device.ip4_config |
| 29 | + if device_ip4_conf_path == '/': |
| 30 | + # This is how NetworkManager indicates there is no ip config |
| 31 | + # for the interface |
| 32 | + continue |
| 33 | + else: |
| 34 | + ip4_conf = IPv4Config(device_ip4_conf_path, system_bus) |
| 35 | + for address_data in ip4_conf.address_data: |
| 36 | + print(' Ip Adress:', address_data['address'][1]) |
| 37 | +
|
| 38 | +Same but using async API: |
| 39 | + |
| 40 | +.. code-block:: python |
| 41 | +
|
| 42 | + from sdbus_async.networkmanager import ( |
| 43 | + NetworkManager, |
| 44 | + NetworkDeviceGeneric, |
| 45 | + IPv4Config, |
| 46 | + ) |
| 47 | + from sdbus import sd_bus_open_system |
| 48 | + from asyncio import run as async_run |
| 49 | +
|
| 50 | + system_bus = sd_bus_open_system() # We need system bus |
| 51 | +
|
| 52 | + nm = NetworkManager(system_bus) |
| 53 | +
|
| 54 | +
|
| 55 | + async def test() -> None: |
| 56 | + devices_paths = await nm.get_devices() |
| 57 | + for device_path in devices_paths: |
| 58 | + generic_device = NetworkDeviceGeneric(device_path, system_bus) |
| 59 | + print('Device: ', await generic_device.interface) |
| 60 | + device_ip4_conf_path = await generic_device.ip4_config |
| 61 | + if device_ip4_conf_path == '/': |
| 62 | + # This is how NetworkManager indicates there is no ip config |
| 63 | + # for the interface |
| 64 | + continue |
| 65 | + else: |
| 66 | + ip4_conf = IPv4Config(device_ip4_conf_path, system_bus) |
| 67 | + for address_data in await ip4_conf.address_data: |
| 68 | + print(' Ip Adress:', address_data['address'][1]) |
| 69 | +
|
| 70 | + async_run(test()) |
0 commit comments