首页 > 技术文章 > Pexpect--example--hive.py解读

forilen 2015-01-09 15:35 原文

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

 

推荐阅读