zoukankan      html  css  js  c++  java
  • Pexpect--example--hive.py解读

    python version 2.6.6 ; pexpect 2.3

    login方法解读:

    def login (args, cli_username=None, cli_password=None):
    
        # I have to keep a separate list of host names because Python dicts are not ordered.
        # I want to keep the same order as in the args list.
        host_names = []
        hive_connect_info = {}
        hive = {}
        # build up the list of connection information (hostname, username, password, port)
        for host_connect_string in args:
            hcd = parse_host_connect_string (host_connect_string)
            hostname = hcd['hostname']
            port     = hcd['port']
            if port == '':
    #             port=22
                port = None
            if len(hcd['username']) > 0: 
                username = hcd['username']
            elif cli_username is not None:
                username = cli_username
            else:
                username = raw_input('%s username: ' % hostname)
            if len(hcd['password']) > 0:
                password = hcd['password']
            elif cli_password is not None:
                password = cli_password
            else:
                password = getpass.getpass('%s password: ' % hostname)
            host_names.append(hostname)
            hive_connect_info[hostname] = (hostname, username, password, port)
            print hive_connect_info
            ''' 
            return result like this 
            {'192.168.100.245': ('192.168.100.245', 'root', 'pwdxxx', None), '192.168.100.246': ('192.168.100.246', 'root', 'pwdxxx', None)}
            '''
        # build up the list of hive connections using the connection information.
        for hostname in host_names:
            print 'connecting to', hostname
            try:
                fout = file("log_"+hostname, "w") 
                'fout means fileout'
                hive[hostname] = pxssh.pxssh()
                print "hive[hostname]:",hive[hostname]
                hive[hostname].login(*hive_connect_info[hostname])
                'the exception happened on the last line'
                print hive[hostname].before
                hive[hostname].logfile = fout
                print '- OK'
            except Exception, e:
                print '- ERROR',
                print str(e)
                print 'Skipping', hostname
                hive[hostname] = None
        return host_names, hive

    上面代码 hive[hostname].login(*hive_connect_info[hostname]) 这行会出一个bug,不过还是很好修的,参考 http://stackoverflow.com/questions/21055943/pxssh-connecting-to-an-ssh-proxy-timeout-exceeded-in-read-nonblocking 可以找到解决办法:

    修改 /usr/lib/python2.6/site-packages/pxssh.py 在第134行插入下面:

    self.sendline() #Line 134
    time.sleep(0.5) #Line 135

    其下面几行正好是

    self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
  • 相关阅读:
    IDEA快速搭建 SpringCloud 注册中心与
    -bash: nginx: 未找到命令 (command not found) 解决方案
    【转载】02-PowerDesigner的下载及安装
    redis.conf配置文件配置项解析
    Linux 重启防火墙失败
    hduoj 3459 Rubik 2×2×2
    sdutoj 2605 A^X mod P
    hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
    hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
    hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
  • 原文地址:https://www.cnblogs.com/forilen/p/4213492.html
Copyright © 2011-2022 走看看