summaryrefslogtreecommitdiff
path: root/ip-link-add-menu.py
diff options
context:
space:
mode:
Diffstat (limited to 'ip-link-add-menu.py')
-rwxr-xr-xip-link-add-menu.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/ip-link-add-menu.py b/ip-link-add-menu.py
index 84f9172..c52b436 100755
--- a/ip-link-add-menu.py
+++ b/ip-link-add-menu.py
@@ -1,5 +1,9 @@
#!/usr/bin/env python3
+from dataclasses import dataclass
+from enum import Enum
+from typing import Optional
+
import dialog
from pyroute2 import IPRoute, IPDB
@@ -51,6 +55,30 @@ class MACVLAN():
mode=self.mode
).commit()
+
+class TuntapMode(Enum):
+ TAP = "tap"
+ TUN = "tun"
+
+
+@dataclass
+class TunTap:
+ name: str
+ mode: TuntapMode
+ uid: Optional[int] = None
+ gid: Optional[int] = None
+
+ def create(self) -> None:
+ global ipdb
+ ipdb.create(
+ kind="tuntap",
+ ifname=self.name,
+ mode=self.mode.value,
+ uid=self.uid,
+ gid=self.gid,
+ ).commit()
+
+
def create_bridge():
choices = [(str(i), '', False) for i in ipdb.by_name.keys()]
@@ -119,6 +147,25 @@ def create_macvlan():
macvlan.create()
+
+def create_tuntap(mode: TuntapMode) -> None:
+ code, name = d.inputbox(f"Please name your {mode.value.upper()} interface.")
+ if code in (d.ESC, d.CANCEL):
+ return
+
+ code, input_ = d.inputbox(f"UID of the interface (leave empty for default)")
+ if code in (d.ESC, d.CANCEL):
+ return
+ uid = int(input_) if input_ else None
+ code, input_ = d.inputbox(f"GID of the interface (leave empty for default)")
+ if code in (d.ESC, d.CANCEL):
+ return
+ gid = int(input_) if input_ else None
+
+ interface = TunTap(name=name, uid=uid, gid=gid, mode=mode)
+ interface.create()
+
+
def main():
d.set_background_title('ip-link add menu')
@@ -126,6 +173,8 @@ def main():
('bridge', 'Create a Bridge'),
('vlan', 'Create a VLAN'),
('macvlan', 'Create a MACVLAN'),
+ ('tun', 'Create a TUN interface'),
+ ('tap', 'Create a TAP interface'),
)
code, tag = d.menu('Select the type of interface you want to create.', choices=choices)
@@ -139,6 +188,10 @@ def main():
create_vlan()
elif tag == 'macvlan':
create_macvlan()
+ elif tag == 'tun':
+ create_tuntap(TuntapMode.TUN)
+ elif tag == 'tap':
+ create_tuntap(TuntapMode.TAP)
if __name__ == '__main__':
main()