3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 # Copyright (C) 2015 Red Hat, Inc.
21 # This example configures a Bond from ethernet devices and activates it
23 # NetworkManager D-Bus API:
24 # https://developer.gnome.org/NetworkManager/1.0/spec.html
27 import dbus, sys, uuid
28 from dbus.mainloop.glib import DBusGMainLoop
31 DBusGMainLoop(set_as_default=True)
33 def add_connection(con):
34 bus = dbus.SystemBus()
35 proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
36 settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
37 return settings.AddConnection(con)
39 def create_bond(bond_name):
40 bond_opts = dbus.Dictionary({'mode': '4'})
41 s_bond = dbus.Dictionary({'options': bond_opts})
42 s_con = dbus.Dictionary({
44 'uuid': str(uuid.uuid4()),
46 'interface-name': bond_name,
48 'autoconnect-slaves': 1})
49 s_ip4 = dbus.Dictionary({'method': 'auto'})
50 s_ip6 = dbus.Dictionary({'method': 'ignore'})
52 con = dbus.Dictionary({
57 print "Creating bond connection: %s" % bond_name
58 return add_connection(con)
60 def create_slave(device, master):
61 slave_name = 'bond-' + master + '-slave-' + device
62 s_wired = dbus.Dictionary({'duplex': 'full'})
63 s_con = dbus.Dictionary({
64 'type': '802-3-ethernet',
65 'uuid': str(uuid.uuid4()),
67 'interface-name': device,
70 'slave-type': 'bond'})
72 con = dbus.Dictionary({
73 '802-3-ethernet': s_wired,
75 print "Creating slave connection: %s" % slave_name
79 print "Usage: %s <bond_name> <ifname1> ..." % sys.argv[0]
86 # Create bond master and slave connections
87 bond_name = sys.argv[1]
88 bond_path = create_bond(bond_name)
89 for ifname in sys.argv[2:]:
90 create_slave(ifname, bond_name)
93 bus = dbus.SystemBus()
94 proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
95 manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
96 ac = manager.ActivateConnection(bond_path, "/", "/")
97 print "Activating bond: %s (%s)" % (bond_name, ac)
99 # Monitor the active bond connection
100 loop = gobject.MainLoop()
101 def properties_changed(props):
103 if props['State'] == 2:
104 print "Succesfully connected"
106 if props['State'] == 3 or props['State'] == 4:
107 print "Bond activation failed"
110 obj = bus.get_object("org.freedesktop.NetworkManager", ac)
111 iface = dbus.Interface(obj, "org.freedesktop.NetworkManager.Connection.Active")
112 iface.connect_to_signal("PropertiesChanged", properties_changed)