ssh_connections.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:sd2 作者: gae123 项目源码 文件源码
def poll(self):
        for host in six.viewvalues(self.hosts_to_ssh):
            if host['ssh']['proc']:
                host['ssh']['proc'].poll()
                while (True):
                    try:
                        line = host['ssh']['proc'].stderr.readline()
                    except IOError:
                        break
                    if line:
                        logging.info("SSH {}: {}".format(
                            host['name'], line))
                    else:
                        break
            # logging.info("%s %s",host, host['sshproc'].returncode if host['sshproc'] else '???')
            if host['ssh']['proc'] is None or host['ssh']['proc'].returncode != None:
                if host['ssh']['proc'] and host['ssh']['proc'].returncode != None:
                    log = logging.info if host['ssh']['proc'].returncode == 0 else logging.error
                    log("SSH:RC %s=%s",
                                 host['sshcmd'],
                                 host['ssh']['proc'].returncode)
                    host['ssh']['proc'] = None
                # Only try to start once every 30 seconds
                if (host['ssh'].get('last_connection') and
                            (datetime.datetime.now() - host[
                                'ssh']['last_connection']).seconds < 30):
                    continue
                logging.info("SSH:START %s", host['sshcmd'])
                try:
                    host['ssh']['proc'] = subprocess.Popen(
                        host['sshcmd'],
                        shell=True,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        close_fds=ON_POSIX)
                    host['ssh']['last_connection'] = datetime.datetime.now()
                except Exception as ex:
                    logging.warning("SSH:START:EXC: %s", ex)
                fl = fcntl.fcntl(host['ssh']['proc'].stderr, fcntl.F_GETFL)
                fcntl.fcntl(host['ssh']['proc'].stderr, fcntl.F_SETFL,
                            fl | os.O_NONBLOCK)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号