From 3238a9cc621975c9417885b3f8552585dce594be Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 30 Aug 2019 11:41:48 +0200 Subject: Python/ifconfig: rework interface alias assignment --- python/vyos/interfaceconfig.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'python') diff --git a/python/vyos/interfaceconfig.py b/python/vyos/interfaceconfig.py index c83b0d55c..c10f76e52 100644 --- a/python/vyos/interfaceconfig.py +++ b/python/vyos/interfaceconfig.py @@ -138,16 +138,43 @@ class Interface: @property def ifalias(self): - return open('/sys/class/net/{0}/ifalias'.format(self._ifname), 'r').read() + """ + Get/set interface alias name + + Example: + + from vyos.interfaceconfig import Interface + alias = Interface('ens192').ifalias + """ + + alias = '' + with open('/sys/class/net/{0}/ifalias'.format(self._ifname), 'r') as f: + alias = f.read().rstrip('\n') + return alias + @ifalias.setter def ifalias(self, ifalias=None): + """ + Get/set interface alias name + + Example: + + from vyos.interfaceconfig import Interface + Interface('ens192').ifalias = 'VyOS upstream interface' + + to clear interface alias e.g. delete it use: + + Interface('ens192').ifalias = '' + """ + + # clear interface alias if not ifalias: - self._ifalias = self._ifname - else: - self._ifalias = str(ifalias) - open('/sys/class/net/{0}/ifalias'.format( - self._ifname), 'w').write(self._ifalias) + ifalias = '\0' + + with open('/sys/class/net/{0}/ifalias'.format(self._ifname), 'w') as f: + f.write(str(ifalias)) + @property def linkstate(self): -- cgit v1.2.3