From be27627277c483874f95c7fbd3281b585bd48eff Mon Sep 17 00:00:00 2001 From: Nikhil Date: Wed, 25 May 2016 16:16:22 -0700 Subject: [PATCH] addons: vrf: Fix to kill current ssh on 'sudo ifreload -a' to enable mgmt VRF Ticket: CM-11080 Reviewed By: roopa, dsa, dave olson, daniel, julien Testing Done: yes, with mgmt VRF configured This patch parses '/usr/bin/pstree -Aps ' output to find the pid of current ssh session, and send 'sudo ifreload -a' to background before killing itself. Signed-off-by: Nikhil --- addons/vrf.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/addons/vrf.py b/addons/vrf.py index 5b4d52e..9da6cf5 100644 --- a/addons/vrf.py +++ b/addons/vrf.py @@ -19,6 +19,7 @@ from ifupdownaddons.modulebase import moduleBase from ifupdownaddons.bondutil import bondutil from ifupdownaddons.iproute2 import iproute2 from ifupdownaddons.dhclient import dhclient +from ifupdownaddons.utilsbase import * class vrfPrivFlags: PROCESSED = 0x1 @@ -632,8 +633,18 @@ class vrf(moduleBase): if not proc: return - cmdl = ['/bin/ps', '--no-headers', '-fp', str(os.getppid())] - pid = utils.exec_commandl(cmdl).split()[2] + pid = None + # outpt of '/usr/bin/pstree -Aps ': + # 'systemd(1)---sshd(990)---sshd(16112)---sshd(16126)---bash(16127)---sudo(16756)---ifreload(16761)---pstree(16842)\n' + # get the above output to following format + # ['systemd(1)', 'sshd(990)', 'sshd(16112)', 'sshd(16126)', 'bash(16127)', 'sudo(16756)', 'ifreload(16761)', 'pstree(16850)'] + pstree = list(reversed(utils.exec_command('/usr/bin/pstree -Aps %s' %os.getpid()).strip().split('---'))) + for index, process in enumerate(pstree): + # check the parent of SSH process to make sure + # we don't kill SSH server or systemd process + if 'sshd' in process and 'sshd' in pstree[index + 1]: + pid = filter(lambda x: x.isdigit(), process) + break self.logger.info("%s: killing active ssh sessions: %s" %(ifacename, str(proc)))