Skip to content

Instantly share code, notes, and snippets.

@daniloegea
Created February 8, 2023 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniloegea/d3bb95dbac2bf37e2f1a7ad046a9f0e8 to your computer and use it in GitHub Desktop.
Save daniloegea/d3bb95dbac2bf37e2f1a7ad046a9f0e8 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import os
import socket
import gi
gi.require_version("NM", "1.0")
from gi.repository import NM, GLib, Gio
nmclient = NM.Client.new()
def _add_connection(connection):
main_loop = GLib.MainLoop()
def add_cb(client, result, data):
nmclient.add_connection_finish(result)
main_loop.quit()
nmclient.add_connection_async(connection, False, None, add_cb, None)
main_loop.run()
def _commit_and_save_connection(connection):
main_loop = GLib.MainLoop()
def commit_cb(client, result, data):
connection.commit_changes_finish(result)
main_loop.quit()
connection.commit_changes_async(True, None, commit_cb, None)
main_loop.run()
def test_create_an_interface_and_change_it():
"""Add a tap interface and change it after create adding IP addresses to it.
XXX: the netplan yaml file will be deleted when we try to change the connetion"""
# Creating a tap1234 device to be a bridge member
tap = NM.SimpleConnection.new()
tap_conn_settings = NM.SettingConnection.new()
tap_conn_settings.set_property(NM.SETTING_CONNECTION_ID, "tap1234")
tap_conn_settings.set_property(NM.SETTING_CONNECTION_INTERFACE_NAME, "tap1234")
tap_conn_settings.set_property(NM.SETTING_CONNECTION_TYPE, "tun")
tap_settings = NM.SettingTun.new()
tap_settings.set_property(NM.SETTING_TUN_MODE, NM.SettingTunMode.TAP)
tap.add_setting(tap_conn_settings)
tap.add_setting(tap_settings)
_add_connection(tap)
tap_connection = nmclient.get_connection_by_id("tap1234")
filename = f"/etc/netplan/90-NM-{tap_connection.get_uuid()}.yaml"
print("tap1234 should exit now")
try:
os.stat(filename)
print(f"File /etc/netplan/{filename} was created")
except FileNotFoundError:
print(f"File {filename} should exist, but doesn't")
print("press any key to continue")
input()
ipv4_settings = tap_connection.get_setting_ip4_config()
ipv4_settings.set_property(NM.SETTING_IP_CONFIG_METHOD, "manual")
ip4_addr1 = NM.IPAddress.new(socket.AF_INET, "10.20.30.40", 24)
ip4_addr2 = NM.IPAddress.new(socket.AF_INET, "10.20.30.41", 24)
ipv4_settings.add_address(ip4_addr1)
ipv4_settings.add_address(ip4_addr2)
ipv6_settings = tap_connection.get_setting_ip6_config()
ipv6_settings.set_property(NM.SETTING_IP_CONFIG_METHOD, "manual")
ip6_addr1 = NM.IPAddress.new(socket.AF_INET6, "dead:beef::1", 64)
ip6_addr2 = NM.IPAddress.new(socket.AF_INET6, "dead:beef::2", 64)
ipv6_settings.add_address(ip6_addr1)
ipv6_settings.add_address(ip6_addr2)
_commit_and_save_connection(tap_connection)
print("The connection was changed")
try:
os.stat(filename)
print(f"The file /etc/netplan/{filename} should exist")
except FileNotFoundError:
print(f"File {filename} should exist, but doesn't")
new_client = NM.Client.new()
tap_connection = new_client.get_connection_by_id("tap1234")
if tap_connection:
print(f"Connection {tap_connection.get_uuid()} still exists")
else:
print("Connection doesn't exist anymore")
if __name__ == '__main__':
test_create_an_interface_and_change_it()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment