Skip to content

Commit 963d17a

Browse files
author
Bernhard Kaindl
committed
PR #4 based on #8: Example to create an Ethernet connection
1 parent 195ac4e commit 963d17a

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
4+
# Example to create a new Ethernet network connection profile:
5+
#
6+
# add-eth-connection.py --help
7+
# usage: add-eth-connection.py [-h] [-c CONN_ID] [-i INTERFACE_NAME] [-4 IP4]
8+
# [-g GW] [-a] [--save]
9+
#
10+
# Optional arguments have example values:
11+
#
12+
# optional arguments:
13+
# -h, --help show this help message and exit
14+
# -c CONN_ID Connection Id
15+
# -i INTERFACE_NAME ethX device
16+
# -4 IP4 IP4/prefix
17+
# -g GW gw/metric
18+
# -a autoconnect
19+
# --save Save
20+
#
21+
# Connection Profile settings are described at:
22+
# https://networkmanager.dev/docs/api/latest/ref-settings.html
23+
24+
import sdbus
25+
import functools
26+
import pprint
27+
import sys
28+
import uuid
29+
from argparse import ArgumentParser, Namespace
30+
from sdbus_block.networkmanager import NetworkManagerSettings
31+
from sdbus_block.networkmanager import NetworkManagerConnectionProperties
32+
33+
34+
def add_ethernet_connection_profile(args: Namespace) -> None:
35+
"""Add a temporary (not yet saved) network connection profile"""
36+
37+
# If we add many connections using the same id, things get messy. Check:
38+
if NetworkManagerSettings().get_connections_by_id(args.conn_id):
39+
print(f'Connections using ID "{args.conn_id}" exist, remove them:')
40+
print(f'Run: nmcli connection delete "{args.conn_id}"')
41+
return
42+
43+
ipaddr, prefix = args.ip4.split("/")
44+
profile: NetworkManagerConnectionProperties = {
45+
"connection": {
46+
"id": ("s", args.conn_id),
47+
"uuid": ("s", str(uuid.uuid4())),
48+
"type": ("s", "802-3-ethernet"),
49+
"autoconnect": ("b", args.auto),
50+
},
51+
"ipv4": {
52+
"method": ("s", "manual"),
53+
"address-data": (
54+
"aa{sv}",
55+
[
56+
{
57+
"address": ("s", ipaddr),
58+
"prefix": ("u", int(prefix)),
59+
},
60+
],
61+
),
62+
},
63+
"ipv6": {"method": ("s", "disabled")},
64+
}
65+
if args.interface_name:
66+
profile["connection"]["interface-name"] = ("s", args.interface_name)
67+
if len(sys.argv) == 1 or args.gw != "192.0.2.1/4000":
68+
default_gateway, route_metric = args.gw.split("/")
69+
profile["ipv4"]["gateway"] = ("s", default_gateway)
70+
profile["ipv4"]["route-metric"] = ("u", int(route_metric))
71+
72+
if args.save:
73+
NetworkManagerSettings().add_connection(profile)
74+
print("New connection profile created and saved, show it with:")
75+
else:
76+
NetworkManagerSettings().add_connection_unsaved(profile)
77+
print("New unsaved connection profile created, show it with:")
78+
79+
print(f'nmcli connection show "{args.conn_id}"|grep -v -e -- -e default')
80+
print("Settings used:")
81+
functools.partial(pprint.pprint, sort_dicts=False)(profile)
82+
83+
84+
if __name__ == "__main__":
85+
p = ArgumentParser(description="Optional arguments have example values:")
86+
conn_id = "MyConnectionExample"
87+
p.add_argument("-c", dest="conn_id", default=conn_id, help="Connection Id")
88+
p.add_argument("-i", dest="interface_name", default="", help="ethX device")
89+
p.add_argument("-4", dest="ip4", default="192.0.2.8/24", help="IP4/prefix")
90+
p.add_argument("-g", dest="gw", default="192.0.2.1/4000", help="gw/metric")
91+
p.add_argument("-a", dest="auto", action="store_true", help="autoconnect")
92+
p.add_argument("--save", dest="save", action="store_true", help="Save")
93+
args = p.parse_args()
94+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
95+
add_ethernet_connection_profile(args)

0 commit comments

Comments
 (0)