码迷,mamicode.com
首页 > 其他好文 > 详细

Pexpect--example--hive.py解读

时间:2015-01-09 16:55:31      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Pexpect--example--hive.py解读

标签:

原文地址:http://www.cnblogs.com/forilen/p/4213492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!