Created
August 1, 2012 21:51
-
-
Save aeris/3231048 to your computer and use it in GitHub Desktop.
Create Network Manager connection from command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dbus, struct, socket, uuid | |
def _ip(ip): | |
t = socket.inet_aton(ip) | |
return dbus.UInt32(struct.unpack("I", t)[0]) | |
def _ips(ips): | |
return dbus.Array([_ip(ip) for ip in ips], signature=dbus.Signature('u')) | |
def _ip6(ip): | |
return dbus.ByteArray(ip) | |
def _ip6s(ips): | |
return dbus.Array([_ip6(ip) for ip in ips], signature=dbus.Signature('ay')) | |
s_wired = dbus.Dictionary({ | |
'mac': dbus.ByteArray('XX:XX:XX:XX:XX:XX') | |
}) | |
s_con = dbus.Dictionary({ | |
'type': '802-3-ethernet', | |
'uuid': str(uuid.uuid4()), | |
'id': 'dhcp' | |
}) | |
s_ip4 = dbus.Dictionary({ | |
'method': 'auto', | |
'dns': _ips(['127.0.0.1']), | |
'dns-search': ['example.org'], | |
'ignore-auto-dns': True, | |
'may-fail': True | |
}) | |
s_ip6 = dbus.Dictionary({ | |
'method': 'auto', | |
'dns': _ip6s(['::1']), | |
'dns-search': ['example.org'], | |
'ignore-auto-dns': True, | |
'may-fail': True | |
}) | |
con = dbus.Dictionary({ | |
'802-3-ethernet': s_wired, | |
'connection': s_con, | |
'ipv4': s_ip4, | |
'ipv6': s_ip6 | |
}) | |
bus = dbus.SystemBus() | |
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings") | |
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings") | |
settings.AddConnection(con) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment