1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#!/usr/bin/env python3
from dataclasses import dataclass
from enum import Enum
from typing import Optional
import dialog
from pyroute2 import IPRoute, IPDB
d = dialog.Dialog()
ipdb = IPDB()
class Bridge():
def __init__(self, name, interfaces=[]):
self.name = name
self.interfaces = interfaces
def create(self):
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 = int(vid)
def create(self):
global ipdb
underlying = ipdb.interfaces[self.master]
ipdb.create(
kind='vlan',
ifname=self.name,
link=underlying,
vlan_id=self.vid
).commit()
class MACVLAN():
def __init__(self, name, master, mode):
self.name = name
self.master = master
self.mode = mode
def create(self):
global ipdb
underlying = ipdb.interfaces[self.master]
ipdb.create(
kind='macvlan',
ifname=self.name,
link=underlying,
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()]
code, interfaces = d.checklist('Select the interfaces to add to the bridge.', choices=choices)
if code == d.ESC or code == d.CANCEL:
return
code, name = d.inputbox('Please name your new bridge.')
if code == d.ESC or code == d.CANCEL:
return
bridge = Bridge(name, interfaces)
bridge.create()
def create_vlan():
choices = [(str(i), '') for i in ipdb.by_name.keys()]
code, master = d.menu('Select the master interface to use.', choices=choices)
if code == d.ESC or code == d.CANCEL:
return
code, vid = d.inputbox('Please give the VLAN ID.')
if code == d.ESC or code == d.CANCEL:
return
default_newname = master + '.' + vid
code, name = d.inputbox('Please name your new VLAN interface.', init=default_newname)
if code == d.ESC or code == d.CANCEL:
return
vlan = VLAN(name, master, vid)
vlan.create()
def create_macvlan():
choices = [(str(i), '') for i in ipdb.by_name.keys()]
code, master = d.menu('Select the master interface to use.', choices=choices)
if code == d.ESC or code == d.CANCEL:
return
code, name = d.inputbox('Please name your new MACVLAN interface.')
if code == d.ESC or code == d.CANCEL:
return
choices = [
('private', 'Private'),
('vepa', 'VEPA'),
('bridge', 'Bridge'),
('passthru', 'Passthrough'),
('source', 'Source'),
]
code, mode = d.menu('Select the mode of MACVLAN interface.', choices=choices)
macvlan = MACVLAN(name, master, mode)
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')
choices = (
('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)
if code == d.ESC or code == d.CANCEL:
return
if tag == 'bridge':
create_bridge()
elif tag == 'vlan':
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()
|