通过xshell或者crt登录堡垒机然后再登录到主机,每次输入密码太麻烦。所以写了这个自动化工具


主要代码:

#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
import xlrd
import os
import shutil
 
sheet_name=[u'TP应用列表',u'TP服务器列表',u'CC应用列表',u'CC服务器列表',u'FP应用列表',u'FP服务器列表',u'EC应用列表',u'EC服务器列表',u'PA应用列表',u'PA服务器列表',u'外包应用列表',u'外包服务器列表']
filedir=os.path.dirname(os.path.abspath(__file__))
if len(sys.argv)!=2:
    
    raise AssertionError('需要参数:配置路径')
#ininame=sys.argv[1]+".ini"
confpath=sys.argv[1].rstrip("/Sessions")
if not os.path.exists(confpath+"/vbs/"):
    
    os.makedirs(confpath+"/vbs/")


#ininamepath=os.path.join(filedir,"ininame")
#ininames=os.listdir(ininamepath)
#inidir=os.path.join(filedir,"ini")
vbsdir=os.path.join(confpath,"vbs")
path=os.path.join(filedir,"howbuy.xls")
    
class DataToSql:
    
    def __init__(self,path):
        
        self.data = xlrd.open_workbook(path)
        
    def dataToSql(self,num,string):
        sysdata=self.data.sheet_by_name(sheet_name[num])
        sysrows = sysdata.nrows
        if os.path.exists(confpath+"/Sessions/"+string):
            shutil.rmtree(confpath+"/Sessions/"+string)        
        for sysrow in range(1,sysrows):
            sysrowvalue = sysdata.row_values(sysrow)
            ip=sysrowvalue[1].strip()
            ipvbs=os.path.join(vbsdir,ip+".vbs")
            appname=string+"/"+sysrowvalue[0].strip()
            #ipvbs1=os.path.join(vbsdir,appname+'/'+ip+".vbs")
            #ipvbs2=os.path.join(vbsdir,appname)
            #appnamepath=os.path.join(inidir,appname)

                
            if not os.path.exists(confpath+"/Sessions/"+appname):
                
                os.makedirs(confpath+"/Sessions/"+appname)
            
            file_object_read = open(confpath+"/Sessions/10.50.50.245.xsh", 'r')     
            stringsave=""  
            stringread=file_object_read.readline()  
            while stringread:  
                stringread=stringread.replace('ScriptPath=', 'ScriptPath='+confpath+'\\vbs\\'+ip+'.vbs')  
                stringsave=stringsave+stringread  
                stringread=file_object_read.readline()  
            file_object_save = open(confpath+"/Sessions/"+appname+"/"+ip+".xsh", 'w')  
            file_object_save.write(stringsave)  
            file_object_read.close()  
            file_object_save.close()
   
            if not os.path.exists(ipvbs):
                
                
                file_object_read = open(filedir+"/"+"xshellvbs.txt", 'r')
                stringsave=""
                stringread=file_object_read.readline()
                while stringread:
                    stringread=stringread.replace('tihuan','/i:'+ip)
                    stringsave=stringsave+stringread
                    stringread=file_object_read.readline() 
                file_object_save = open(ipvbs, 'w')
                file_object_save.write(stringsave)
                file_object_read.close()
                file_object_save.close()
            

            

data=DataToSql(path)

try:
    data.dataToSql(1,"TP")
except:
    print "tp no data"         

try:
    data.dataToSql(3,"CC")
except:
    print "cc no data"  

try:
    data.dataToSql(5,"FP")
except:
    print "fp no data"        
try:
    data.dataToSql(7,"EC")
except:
    print "ec no data"       
try:
    data.dataToSql(9,"PA")
except:
    print "pa no data"       
try:
    data.dataToSql(11,"WB")
except:
    print "wb no data"

实现图片:双击实现登录

实现xshell以及crt自动化登录_第1张图片