首页 > 代码库 > Pexpect--example--hive.py解读
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 134time.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解读
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。