summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-08-30 11:31:00 +0200
committerChristian Poessinger <christian@poessinger.com>2019-08-30 11:32:10 +0200
commitdff5f3f38f60b9da8e791593998e6f614da6e5ec (patch)
treeab9aeb184b85da32721c0d9696370855ada92503 /python
parentfc0c87c83089dcd84a1b2dc93e4d27542e11bb54 (diff)
downloadvyos-1x-dff5f3f38f60b9da8e791593998e6f614da6e5ec.tar.gz
vyos-1x-dff5f3f38f60b9da8e791593998e6f614da6e5ec.zip
Python/ifconfig: rework changing of interface MAC address
Diffstat (limited to 'python')
-rw-r--r--python/vyos/interfaceconfig.py71
1 files changed, 49 insertions, 22 deletions
diff --git a/python/vyos/interfaceconfig.py b/python/vyos/interfaceconfig.py
index edd8ef038..c83b0d55c 100644
--- a/python/vyos/interfaceconfig.py
+++ b/python/vyos/interfaceconfig.py
@@ -46,6 +46,11 @@ class Interface:
self._ifname = str(ifname)
+ def _cmd(self, command):
+ process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
+ proc_stdout = process.communicate()[0].strip()
+ pass
+
@property
def mtu(self):
"""
@@ -83,31 +88,53 @@ class Interface:
@property
- def macaddr(self):
+ def mac(self):
"""
- get/set interface mac address
+ Get/set interface mac address
+
+ Example:
+
+ from vyos.interfaceconfig import Interface
+ mac = Interface('ens192').mac
"""
- try:
- ret = subprocess.check_output(
- ['ip -j -4 link show dev ' + self._ifname], stderr=subprocess.STDOUT, shell=True).decode()
- j = json.loads(ret)
- return j[0]['address']
- except subprocess.CalledProcessError as e:
- if self._debug():
- self._debug(e)
- return None
+ address = ''
+ with open('/sys/class/net/{0}/address'.format(self._ifname), 'r') as f:
+ address = f.read().rstrip('\n')
+ return address
+
+
+ @mac.setter
+ def mac(self, mac=None):
+ """
+ Get/set interface mac address
+
+ Example:
+
+ from vyos.interfaceconfig import Interface
+ Interface('ens192').mac = '00:90:43:fe:fe:1b'
+ """
+ # a mac address consits out of 6 octets
+ octets = len(mac.split(':'))
+ if octets != 6:
+ raise ValueError('wrong number of MAC octets: {} '.format(octets))
+
+ # validate against the first mac address byte if it's a multicast address
+ if int(mac.split(':')[0]) & 1:
+ raise ValueError('{} is a multicast MAC address'.format(mac))
+
+ # overall mac address is not allowed to be 00:00:00:00:00:00
+ if sum(int(i, 16) for i in mac.split(':')) == 0:
+ raise ValueError('00:00:00:00:00:00 is not a valid MAC address')
+
+ # check for VRRP mac address
+ if mac.split(':')[0] == '0' and addr.split(':')[1] == '0' and mac.split(':')[2] == '94' and mac.split(':')[3] == '0' and mac.split(':')[4] == '1':
+ raise ValueError('{} is a VRRP MAC address'.format(mac))
+
+ # Assemble command executed on system. Unfortunately there is no way
+ # of altering the MAC address via sysfs
+ cmd = 'ip link set dev "{}" address "{}"'.format(self._ifname, mac)
+ self._cmd(cmd)
- @macaddr.setter
- def macaddr(self, mac=None):
- if not re.search('^[a-f0-9:]{17}$', str(mac)):
- raise ValueError("mac address invalid")
- self._macaddr = str(mac)
- try:
- ret = subprocess.check_output(
- ['ip link set address ' + mac + ' ' + self._ifname], shell=True).decode()
- except subprocess.CalledProcessError as e:
- if self._debug():
- self._debug(e)
@property
def ifalias(self):