host.py 文件源码

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

项目:INSALATA 作者: tumi8 项目源码 文件源码
def waitForBoot(logger, host):
    """
    Wait for a host to be booted in a sense that ssh is ready (using nmap)

    :param logger: A logger used for logging possible errors.
    :type logger: seealso:: :class:`logging:Logger`

    :param host: Host-instance to wait for.
    :type host: Host
    """
    #find the correct name
    name = host.getID() if host.getNameApplied() else host.getTemplate().getID()

    #wait for the machine to be ssh-ready
    nm = nmap.PortScanner()

    logger.info("Boot VM {0}, waiting for SSH".format(name))

    isOffline = sshClosed = True 
    while isOffline or sshClosed:
        time.sleep(2)
        #scanres = nm.scan(name , '22', '')
        try:
            remoteServerIP = socket.gethostbyname(name)
        except Exception as e:
            logger.debug("Unable to resolve hostname '{}'".format(name))
            continue

        isOffline = False

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((remoteServerIP, 22))
        sock.close()
        if not result == 0: 
            continue
        else:
            sshClosed = False

    logger.info("Host '{0}' is reachable via SSH.".format(name))

        #scanres = {}
        #logger.info("SCANRES: " + scanres)
        #isOffline = scanres['nmap']['scanstats']['uphosts'] == '0'
        #if len(list(scanres['scan'].keys())) > 0:
        #    sshClosed = scanres['scan'][list(scanres['scan'].keys())[0]]['tcp'][22]['state'] == 'closed'
        #else:
        #    logger.info("VM {0} not up yet, keep waiting.".format(name))

#Clean shutdown of the vm
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号