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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
.. _vyos_api:
VyOS API Classes
================
Interface Config
----------------
This class contains the code to configure a network interfaces.
Exceptions are being send to stdout if the debug flag is set within the environment, via ``export DEBUG=1``.
It shows exception for executed commands and parses andd displays the exception output in a structured form.
Value errors are always displayed, regardless of the presence of the ``DEBUG`` variable.
Example how the ``DEBUG`` variable can be used.
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
a = Interface("wg01", "fnord")
print (a.set_link_state("up"))
.. code-block:: sh
root@vyos:/home/vyos# ./example_script.py
RTNETLINK answers: Operation not supported
export DEBUG=1
root@vyos:/home/vyos# ./example_script.py
Exception raised:
command: ['ip link add dev wg01 type fnord']
error code: 2
subprocess output: RTNETLINK answers: Operation not supported
Interface(interfacename, type=None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The interfacename is mandatory, if it is a virtual interface such as a wireguard interface or vlan, veth etc., the type can be assigned and the device is being created if it is a valid device.
existing device:
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
interface_instance = Interface("eth2")
non-existing device which needs to be created:
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
interface_instance = Interface("wg01", "wireguard")
If a device creation has been successful, the return code is ``0`` otherwise ``None``.
set_alias(alias=None)
^^^^^^^^^^^^^^^^^^^^^
An interfaces ifalias variable is empty by default, but the variable is used for example by snmp. The parameter is optional and if not set, the default behavior is to use the interfacename of the instance, if set it uses the name set in the parameter.
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
interface_instance = Interface("eth2")
interface_instance.set_alias()
.. code-block:: sh
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether 08:00:27:70:9c:a3 brd ff:ff:ff:ff:ff:ff
alias eth2
get_alias()
^^^^^^^^^^^
Reads the ifalias variable directly from the /sys/class/net/<interface>/ifalias and can be used to determine in a config a new value and what value is set in in the system.
del_alias()
^^^^^^^^^^^
Removes any content from the ifalias variable.
set_link_state(state="up")
^^^^^^^^^^^^^^^^^^^^^^^^^^
Sets an interface state either to adminitrativly up or down, regardless of the real connection status. If called without parameter, the default function is up, valid parameters are ``up`` or ``down``.
.. code-block:: sh
ip link show dev eth2
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
interface_instance = Interface("eth2")
interface_instance.set_link_state()
.. code-block:: sh
ip link show dev eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
get_link_state()
^^^^^^^^^^^^^^^^
Returns the link status of an interface which can be either ``up``, ``down`` or ``unknown``. The link status ``unknown`` is often visible on uninitialized wireguard interfaces, once traffic was successfully sent and received it will change to state ``up``. However, it is not recommended to assume that wireguard is correctly configured by the interface status, since multiple peers can be configured on a single interface and if only 1 out of 10 is working, the interface status will shown as ``up``.
remove_interface()
^^^^^^^^^^^^^^^^^^
Removes an interface from the system, given as parameter of the objects instance.
set_macaddr(mac)
^^^^^^^^^^^^^^^^
Sets the mac address on a network interface.
get_macaddr()
^^^^^^^^^^^^^
Returns the mac address of a network interface.
set_mtu(mtu=None)
^^^^^^^^^^^^^^^^^
Sets the MTU on a network interface.
get_mtu(self)
^^^^^^^^^^^^^
Returns the MTU of a network interface.
add_ipv4_addr(ipaddr=[]):
^^^^^^^^^^^^^^^^^^^^^^^^^
Adds IPv4 addresses given as parameter.
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
ips = ['10.100.100.1/24', '10.100.100.2/24', '10.100.100.3/24']
interface_instance = Interface("eth2")
interface_instance.add_ipv4_addr(ips)
.. code-block:: sh
ip -4 -br addr sh dev eth2
eth2 UP 10.100.100.1/24 10.100.100.2/24 10.100.100.3/24
del_ipv4_addr(ipaddr=[])
^^^^^^^^^^^^^^^^^^^^^^^^
Removes the IPs given in the parameter ``ipaddr``.
def get_ipv4_addr()
^^^^^^^^^^^^^^^^^^^
Returns a list of all IPv4 addresses of an interface.
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
interface_instance = Interface("eth2")
ips = interface_instance.get_ipv4_addr()
print(ips)
.. code-block:: sh
['10.100.100.1', '10.100.100.2', '10.100.100.3']
set_dhcpv4()
^^^^^^^^^^^^
Starts dhclient and sends DHCPREQUEST messages on the interface.
del_dhcpv4()
^^^^^^^^^^^^
Stops dhclient on the interface.
get_dhcpv4()
^^^^^^^^^^^^
Returns the pid of the dhclient process, if none is runing `False` is being returned and the message ``no dhcp client running on interface <interface>`` displayed on stdout.
add_ipv6_addr(ipaddr=[])
^^^^^^^^^^^^^^^^^^^^^^^^
Adds IPv6 addresses given as parameter.
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
ips = ['2001:db8:dead::1/64', '2001:db8:beaf::1/64', '2001:db8:cafe::1/64']
interface_instance = Interface("eth2")
interface_instance.add_ipv6_addr(ips)
.. code-block:: sh
ip -6 -br addr sh dev eth2
eth2 UP 2001:db8:cafe::1/64 2001:db8:beaf::1/64 2001:db8:dead::1/64 fe80::a00:27ff:fe70:9ca3/64
del_ipv6_addr(ipaddr=[])
^^^^^^^^^^^^^^^^^^^^^^^^
Removes the IPv6 addresses given via the paramater ``ipaddr``.
get_ipv6_addr()
^^^^^^^^^^^^^^^
Returns all IPv6 addresse set on the interface.
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
interface_instance = Interface("eth2")
ips = interface_instance.get_ipv6_addr()
print(ips)
.. code-block:: sh
['2001:db8:cafe::1', '2001:db8:beaf::1', '2001:db8:dead::1', 'fe80::a00:27ff:fe70:9ca3']
set_dhcpv6()
^^^^^^^^^^^^
It enables stateful IPv6 deployments on the given interface. The interface will stop to listen to route annoncements (RA's) and requests that parameter via dhcpv6.
DHCPv4 and DHCPv6 can be configured simultaniously on the interface.
.. code-block:: sh
#!/usr/bin/python3
from vyos.interfaceconfig import Interface
interface_instance = Interface("eth2")
interface_instance.set_dhcpv4()
interface_instance.set_dhcpv6()
del_dhcpv6()
^^^^^^^^^^^^
Stops dhclient and starts listen and acceptiing RA's again.
get_dhcpv6()
^^^^^^^^^^^^
Returns the pid of the running dhclient process or None if it doesn't exist.
|