summaryrefslogtreecommitdiff
path: root/ip-link-add-menu.py
diff options
context:
space:
mode:
authorDenis Thulin <dt@satcom1.com>2018-03-09 14:05:48 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2018-03-09 16:05:57 +0100
commitc10fc793720392a995378b2c1d1f941caa1cbf29 (patch)
treebf157a595f07133ba85cbc952b7d9af768245b08 /ip-link-add-menu.py
parent6d5ff31490e7122b4e0480c6faf9841ec18c8b18 (diff)
Used pyroute2 instead of iproute or `/sys/class`
Signed-off-by: Denis Thulin <denis.thulin@gmail.com>
Diffstat (limited to 'ip-link-add-menu.py')
-rwxr-xr-xip-link-add-menu.py43
1 files changed, 18 insertions, 25 deletions
diff --git a/ip-link-add-menu.py b/ip-link-add-menu.py
index 3b8f51a..a39df54 100755
--- a/ip-link-add-menu.py
+++ b/ip-link-add-menu.py
@@ -3,49 +3,42 @@
import dialog
import os
+from pyroute2 import IPRoute, IPDB
import subprocess
d = dialog.Dialog()
+ipdb = IPDB()
+
class Bridge():
def __init__(self, name, interfaces=[]):
self.name = name
self.interfaces = interfaces
def create(self):
- cmd = [
- '/sbin/ip', 'link',
- 'add', 'name', self.name,
- 'type', 'bridge',
- ]
- subprocess.run(cmd)
- for interface in self.interfaces:
- cmd = [
- '/sbin/ip', 'link',
- 'set', 'dev', interface,
- 'master', self.name
- ]
- subprocess.run(cmd)
+ global ipdb
+ with ipdb.create(kind='bridge', ifname=self.name) as master:
+ for slave in self.interfaces:
+ master.add_port(ipdb.interfaces[slave])
class VLAN():
def __init__(self, name, master, vid):
self.name = name
self.master = master
- self.vid = vid
+ self.vid = int(vid)
def create(self):
- cmd = [
- '/sbin/ip', 'link',
- 'add', 'link', self.master,
- 'name', self.name,
- 'type', 'vlan',
- 'id', self.vid,
- ]
- subprocess.run(cmd)
+ global ipdb
+ underlying = ipdb.interfaces[self.master]
+ ipdb.create(
+ kind='vlan',
+ ifname=self.name,
+ link=underlying,
+ vlan_id=self.vid
+ ).commit()
def create_bridge():
- # We need to list the interfaces from the sysfs.
- choices = [(i, '', False) for i in os.listdir('/sys/class/net')]
+ choices = [(str(i), '', False) for i in ipdb.by_name.keys()]
code, interfaces = d.checklist('Select the interfaces to add to the bridge.', choices=choices)
@@ -63,7 +56,7 @@ def create_bridge():
def create_vlan():
# We need to list the interfaces from the sysfs.
- choices = [(i, '') for i in os.listdir('/sys/class/net')]
+ choices = [(str(i), '') for i in ipdb.by_name.keys()]
code, master = d.menu('Select the master interface to use.', choices=choices)