+ netlink.link_set_updown(ifaceobj.name, "up")
+ except Exception, e:
+ self.log_error('%s: %s' %(ifaceobj.name, str(e)), ifaceobj)
+
+ def _kill_ssh_connections(self, ifacename):
+ try:
+ runningaddrsdict = self.ipcmd.addr_get(ifacename)
+ if not runningaddrsdict:
+ return
+ iplist = [i.split('/', 1)[0] for i in runningaddrsdict.keys()]
+ if not iplist:
+ return
+ proc=[]
+ #Example output:
+ #ESTAB 0 0 10.0.1.84:ssh 10.0.1.228:45186
+ #users:(("sshd",pid=2528,fd=3))
+ cmdl = ['/bin/ss', '-t', '-p']
+ for line in utils.exec_commandl(cmdl).splitlines():
+ citems = line.split()
+ addr = None
+ if '%' in citems[3]:
+ addr = citems[3].split('%')[0]
+ elif ':ssh' in citems[3]:
+ addr = citems[3].split(':')[0]
+ if not addr:
+ continue
+ if addr in iplist:
+ if len(citems) == 6:
+ proc.append(citems[5].split(',')[1].split('=')[1])
+
+ if not proc:
+ return
+ pid = None
+ # outpt of '/usr/bin/pstree -Aps <pid>':
+ # '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)))
+
+ if ifupdownflags.flags.DRYRUN:
+ return
+ for id in proc:
+ if id != pid:
+ try:
+ os.kill(int(id), signal.SIGINT)
+ except OSError as e:
+ continue
+
+ # Kill current SSH client
+ if pid in proc:
+ try:
+ forkret = os.fork()
+ except OSError, e:
+ self.logger.info("fork error : %s [%d]" % (e.strerror, e.errno))
+ if (forkret == 0): # The first child.
+ try:
+ os.setsid()
+ self.logger.info("%s: ifreload continuing in the background" %ifacename)
+ except OSError, (err_no, err_message):
+ self.logger.info("os.setsid failed: errno=%d: %s" % (err_no, err_message))
+ self.logger.info("pid=%d pgid=%d" % (os.getpid(), os.getpgid(0)))
+ try:
+ self.logger.info("%s: killing our session: %s"
+ %(ifacename, str(proc)))
+ os.kill(int(pid), signal.SIGINT)
+ return
+ except OSError as e:
+ return