diff options
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/ifconfig/interface.py | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index 718fe93db..18256cf99 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -93,6 +93,9 @@ class Interface(Control): } _sysfs_get = { + 'mac': { + 'location': '/sys/class/net/{ifname}/address', + }, 'mtu': { 'location': '/sys/class/net/{ifname}/mtu', }, @@ -287,6 +290,17 @@ class Interface(Control): """ return self.set_interface('mtu', mtu) + def get_mac(self): + """ + Get current interface MAC (Media Access Contrl) address used. + + Example: + >>> from vyos.ifconfig import Interface + >>> Interface('eth0').get_mac() + '00:50:ab:cd:ef:00' + """ + self.get_interface('mac') + def set_mac(self, mac): """ Set interface MAC (Media Access Contrl) address to given value. @@ -295,6 +309,16 @@ class Interface(Control): >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_mac('00:50:ab:cd:ef:01') """ + + # If MAC is unchanged, bail out early + if mac == self.get_mac(): + return None + + # MAC address can only be changed if interface is in 'down' state + prev_state = self.get_state() + if prev_state == 'up': + self.set_state('down') + self.set_interface('mac', mac) def set_vrf(self, vrf=''): @@ -426,7 +450,7 @@ class Interface(Control): def get_state(self): """ - Enable (up) / Disable (down) an interface + Get interface administrative state. Function will return 'up' or 'down' Example: >>> from vyos.ifconfig import Interface @@ -436,11 +460,16 @@ class Interface(Control): cmd = 'ip -json link show dev {}'.format(self.config['ifname']) tmp = self._cmd(cmd) out = json.loads(tmp) - return out[0]['operstate'].lower() + + state = 'down' + if 'UP' in out[0]['flags']: + state = 'up' + + return state def set_state(self, state): """ - Enable (up) / Disable (down) an interface + Set interface administrative state to be 'up' or 'down' Example: >>> from vyos.ifconfig import Interface |