利用Pexpect模拟人机互动实现自动LOG取得

#!/usr/bin/python
#-*- coding: utf-8 -*-
import pexpect
import re
import os
import sys, getopt
import threading
import string
from time import strftime,time,strptime,localtime,sleep

hostlist=''
module=''

def get_logpath(date):
    global hostlist
    global module
    logpath=''
    f = open(r'logrule') 
    lines = f.readlines()
    for line in lines:
        pattern = re.compile(r'.*host=(.*)\]')
        match = pattern.match(line)
        if match:
            hostlist=match.group(1) 
        pattern = re.compile(r'(.*%s.*)=(.*)'%module,re.I)
        match = pattern.match(line)
        if match:
            module=match.group(1)
            print 'Module:%s'%(module)
            logpath=match.group(2)
            break;
    f.close()
    
    if logpath!='':
        logdate=strptime(date,'%Y%m%d')
        logpath = logpath.replace("YYYY",'%s'%(logdate.tm_year))
        logpath = logpath.replace("MM",'%02d'%(logdate.tm_mon))
        logpath = logpath.replace("M",'%s'%(logdate.tm_mon))
        logpath = logpath.replace("DD",'%02d'%(logdate.tm_mday))
        logpath = logpath.replace("D",'%s'%(logdate.tm_mday))
    else:
        print "can't find the path of module!"
        os._exit(1)
        
    #print hostlist+logpath
    return logpath

def login_command(user, host, password):
    print 'ssh %s@%s'%(user, host)
    ssh_newkey = 'Are you sure you want to continue connecting'
    child = pexpect.spawn('ssh %s@%s'%(user, host),timeout=900,maxread=5000)
    i = child.expect([pexpect.TIMEOUT,ssh_newkey,".*password:.*"])
    if(i == 0): 
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
    elif(i == 1): 
        child.sendline("yes")
        i = child.expect ('password: ')
    child.sendline(password)
    return child

def scp_command(child, user, host, password, frompath ,tofolder):
    command='scp %s@%s:%s %s'%(user, host, frompath, tofolder)
    print command
    child.sendline(command)
    ssh_newkey = 'Are you sure you want to continue connecting'
    i=child.expect([pexpect.TIMEOUT,ssh_newkey, ".*password:.*"],timeout=30)
    if(i == 0):
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
    elif(i == 1): 
        child.sendline("yes")
    child.sendline(password)
    i = child.expect([pexpect.TIMEOUT, ".*"],timeout=300)
    if(i == 0): 
        print '--------------------------------------SCP timeout!'
        print child.before, child.after
    return child

def getlog_command(user, host, password,user2, password2, date):
    
    #登录138
    child=login_command(user, host, password)
    basefolder="/home/pset_user/pset/log_%s"%(date)
    child.sendline('mkdir -p %s'%(basefolder))
    
    #取得log
    logpath=get_logpath(date)
    hostarray=hostlist.split(';')
    icnt=1
    for ip in hostarray:
        child.sendline('mkdir -p %s/%s_%s'%(basefolder,module,icnt))
        child=scp_command(child,user2,ip,password2,logpath,'%s/%s_%s/'%(basefolder,module,icnt))
        icnt+=1
        
    #打包
    child.sendline('cd %s'%(basefolder))
    tarname='%s_%s.tar.gz'%(module,strftime('%Y%m%d%H%M%S',localtime(time())))
    print 'tar czvf %s %s_?'%(tarname,module)
    child.sendline('tar czvf %s %s_?'%(tarname,module))
    child.sendline('exit')
    child.expect(pexpect.EOF)
    #print child.before
    
    #登录221
    child=login_command('#', '#', '#')
    
    #scp日志到221
    toBaseFolder='/home/cms/pio_test'
    child.sendline('cd %s'%(toBaseFolder))
    #today=strftime('%Y%m%d',localtime(time()))
    child.sendline('mkdir -p %s'%(date))

    #进度监视
    prothread=pthread(user, host, password,'%s/%s'%(basefolder,tarname),'%s/%s/%s'%(toBaseFolder,date,tarname))
    prothread.start()
    child=scp_command(child,user,host,password,'%s/%s'%(basefolder,tarname),'%s/'%(date))
    prothread.join()
    child.sendline('exit')
    child.expect(pexpect.EOF)
    #print child.before
    
    #登录138
    child=login_command(user, host, password)
    child.sendline('rm -rf %s'%(basefolder))
    #child.sendline('rm -f %s/%s'%(basefolder,tarname))
    
    child.sendline('exit')
    return child



def ssh_command (user, host, password, command):
    ssh_newkey = 'Are you sure you want to continue connecting'
    child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
    i = child.expect([pexpect.TIMEOUT, ssh_newkey, '.*password:.*'])
    if i == 0:
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
        return None
    if i == 1:
        child.sendline ('yes')
        i = child.expect([pexpect.TIMEOUT, 'password: '])
        if i == 0:
            print 'ERROR!'
            print 'SSH could not login. Here is what SSH said:'
            print child.before, child.after
            return None
    # 输入密码.
    child.sendline(password)
    child.expect(pexpect.EOF)
    size= child.before.strip().split()[0]
    return size

class pthread(threading.Thread):
    def __init__(self,user, host, password,command,command2):
        self.user = user
        self.user = user
        self.host = host
        self.password = password
        self.command = command
        self.command2 = command2
        threading.Thread.__init__(self)
        
    def run (self):
        size=0
        totalsize=ssh_command(self.user, self.host, self.password,"du -s %s"%(self.command))
        pattern = re.compile(r'[0-9]+')
        match = pattern.match(totalsize)
        if match:
            while totalsize!=size:
               sleep(5)
               size=ssh_command('cms', '#', '#', "du -s %s"%(self.command2))
               sleep(5)
               print 'downloading(%d%s)…… size:%sk/total:%sk'%(string.atoi(size)*100/string.atoi(totalsize),'%',size,totalsize)
            else:
               print "get the module'logruleg finished!"

def main ():
    global module
    host = "#"
    user = "#"
    password = "#"
    user2 = "#"
    password2 = "#"
    modulelist = ''
    # 获得用户指定模块名及指定日期
    opts, args = getopt.getopt(sys.argv[1:], "hm:d:",["help"])
    for op, value in opts:
        if op == "-m":
            modulelist = value
        elif op == "-d":
            date = value
        elif op in ("-h","--help"):
            sys.exit()
    #module = raw_input('Module: ')
    #date = raw_input('Date(YYYYMMDD): ')
    pattern = re.compile(r'^\d{8}$')
    match = pattern.match(date)
    if match:
        #调用scp命令取得log并打包传送
        for module in modulelist.split(','):
            child = getlog_command (user, host, password,user2, password2, date)
            child.expect(pexpect.EOF)
            print child.before
    else:
        print 'invalid date format!'

if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        print str(e)
        #traceback.print_exc()
        os._exit(1)

你可能感兴趣的:(pexpect)