标签:names bat create list() containe 配置 write nic mis
在上次做了第一次尝试,现尝试优化
https://www.cnblogs.com/tingxin/p/11949317.html
首先日志文件和logging 配置文件都放在conf文件夹下
产生日志文件 newfile.py
import os,time def newfile(targetfile): path = os.path.dirname(os.getcwd()) logpath = os.path.join(path, "worklog") logfileformat = str(targetfile+time.strftime("_%Y-%m-%d_%H_%M_%S", time.localtime()) + ‘.log‘) createlogfile = os.path.join(logpath, logfileformat) createlogfile = createlogfile.replace(‘\\‘, ‘/‘) with open(createlogfile, mode="a", encoding="utf-8") as f: pass print(f"this logfile : {createlogfile} is create by programer :{targetfile}") return createlogfile
logging 的配置文件: logconf,py
import logging import sys from conf import newfile def logconf(logtarget): logger = logging.getLogger(logtarget) logger.setLevel(logging.INFO) formatter = logging.Formatter(fmt=‘%(asctime)s - %(name)s - %(levelno)s - %(filename)s - %(funcName)s - %(levelname)s - %(message)s‘,datefmt="%d-%m-%Y %H:%M:%S") # # standoutput stream_handler = logging.StreamHandler(sys.stdout) stream_handler.setLevel(level=logging.INFO) stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) logfile=newfile.newfile(logtarget) file_handler = logging.FileHandler(logfile) file_handler.setLevel(level=logging.INFO) # 设置日志级别 file_handler.setFormatter(formatter) # 添加日志格式 logger.addHandler(file_handler) # 添加处理器 return logger
以下是新的脚本: pylinuxautomulti.py
import paramiko import threading import sys,os import datetime from conf import logconf import getpass hostname=""
def hostnamelist(): """ :return: hostname list """ global result,hosttoal try: hosts = open("vmhost", "r", encoding=‘utf-8‘) hostnames = hosts.readlines() hosts.seek(0) hosttoal = len(open("vmhost", "r", encoding=‘utf-8‘).readlines()) print("There are %d target host :/n%s" % (hosttoal,hostnames)) hosts.close() return hostnames except FileNotFoundError: print(‘无法打开指定的文件!‘) except LookupError: print(‘指定了未知的编码!‘) except UnicodeDecodeError: print(‘读取文件时解码错误!‘) finally: if hosts: hosts.close() class RemoteServer(object): """ Container class for SSH functionality. Needs to be used in a with statement. """ def __init__(self): ssh_client = None user = None password = None logtarget = ‘‘ server = ‘‘ status = True #set all the reulst is running successfully self.ssh_client = paramiko.SSHClient() self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.logtarget = os.path.basename(__file__).split(‘.‘)[0] print(os.path.basename(__file__))
def __setattr__(self, key, value): self.__dict__[key]=value def __exit__(self, exc_type, exc_val, exc_tb): try: if self.ssh_client is not None: self.logger.info(‘Exit remote server (%s) now, Bye,Bye !‘, self.server) self.ssh_client.close() except: print ("received an exception closing the ssh connection.") finally: self.ssh_client = None def connect(self, server, user, password,port=22): self.server=server try: self.logger.info(‘tring to connect server %s‘,self.server) self.ssh_client.connect(server, 22,self.user, self.password) except paramiko.AuthenticationException: self.logger.error("Wrongful username or password. Please try again", exc_info=True) try: self.ssh_client.connect(server,22, username=self.user, password=self.password) except: self.logger.error("Wrongful username or password. Please try again", exc_info=True) sys.exit(1) except : self.logger.error(‘connect to remote server %s failed‘,self.server) self.logger.error("Unexpected error:", sys.exc_info()[0]) exit() return self def execute(self, commands, sudo=False): self.logfile = self.logtarget +‘.‘ + self.server.split(‘.‘)[0] self.logger = logconf.logconf(self.logfile) # init log config self.connect(self.server, self.user, self.password) self.logger.info("connect to server %s Successfully !\n", self.server) for command in commands: command = command.strip("\n") try: starttime = datetime.datetime.now() self.logger.info(‘Start execute command: (%s) on host (%s) ‘,command,self.server) stdin, stdout, stderr = self.ssh_client.exec_command(command,timeout=180) self.logger.info(‘Following is the (%s) result \n‘,command) self.logger.info(stdout.read().decode(encoding=‘utf-8‘)) self.logger.info(‘Execute ({}) used times: ({}s) \n‘.format(command, datetime.datetime.now() - starttime)) except paramiko.SSHException: self.logger.error(stderr.read().decode(encoding=‘utf-8‘),exc_info=True) self.logger.info("End of execute on server (%s)",self.server) self.logger.error("Pls check the reason and run agin", self.server) exit() if sudo: stdin.write(self.password + ‘\n‘) stdin.flush() if stderr.read().decode(encoding=‘utf-8‘): self.logger.error(stderr.read().decode(encoding=‘utf-8‘), exc_info=True) self.logger.info("End of execute on server (%s)", self.server) self.logger.error("Pls check the command (%s) and run agin",command) self.status = False break if self.status == False: self.logger.info("One of the command is run failed on (%s),pls check the logfile (%s) for detail information !!! \n", self.server,os.path.join(os.path.dirname(os.getcwd()),‘worklog‘,self.logfile)) else: self.logger.info("all the batch process running successfully on (%s) \n", self.server)if __name__ == ‘__main__‘: # cmd = [‘date‘,‘hostname‘,‘cat /etc/security/limits.conf|grep -v ^#|grep -v ^$‘,‘cat /etc/security/limits.d/20-nproc.conf |grep -v ^#|grep -v ^$‘,‘cat /etc/sysctl.conf|grep -v ^#|grep -v ^$‘] commands="" with open("command.txt",‘r‘,encoding=‘utf-8‘) as batch: commands = batch.readlines() print("following command will be execute %s" %(commands)) username = input("Please Input Username :") # 用户名 password = getpass.getpass(‘please type password of %s :‘ % username) # command = input("pls input command , which can be split by comma :") print("........................................Batch Process Begin........................................") for targethost in hostnamelist(): targethost=targethost.strip("\n") sshhost = RemoteServer() sshhost.server,sshhost.port,sshhost.user = targethost,22,username sshhost.password = password startbatch = threading.Thread(sshhost.execute(commands=commands)) startbatch.start() startbatch.join() print("........................................Batch Process End........................................")
输出结果
07-06-2020 14:11:49 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - connect - INFO - tring to connect server d4cdb.doufupi.com 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - connect to server d4cdb.doufupi.com Successfully ! 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (hostname -f) on host (d4cdb.doufupi.com) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (hostname -f) result 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - d4cdb.doufupi.com 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (hostname -f) used times: (0:00:00.062719s) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (date) on host (d4cdb.doufupi.com) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (date) result 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Sun Jun 7 14:12:09 +08 2020 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (date) used times: (0:00:00.053681s) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (uname) on host (d4cdb.doufupi.com) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (uname) result 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Linux 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (uname) used times: (0:00:00.067252s) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (cat /etc/security/limits.conf|grep -v ^#|grep -v ^$) on host (d4cdb.doufupi.com) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (cat /etc/security/limits.conf|grep -v ^#|grep -v ^$) result 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - oracle soft nofile 1024 oracle hard nofile 65536 oracle soft nproc 16384 oracle hard nproc 16384 oracle soft stack 10240 oracle hard stack 32768 oracle hard memlock 134217728 oracle soft memlock 134217728 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (cat /etc/security/limits.conf|grep -v ^#|grep -v ^$) used times: (0:00:00.065132s) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (cat /etc/security/limits.d/20-nproc.conf |grep -v ^#|grep -v ^$) on host (d4cdb.doufupi.com) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (cat /etc/security/limits.d/20-nproc.conf |grep -v ^#|grep -v ^$) result 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - * soft nproc 4096 root soft nproc unlimited 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (cat /etc/security/limits.d/20-nproc.conf |grep -v ^#|grep -v ^$) used times: (0:00:00.056713s) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (cat /etc/sysctl.conf|grep -v ^#|grep -v ^$) on host (d4cdb.doufupi.com) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (cat /etc/sysctl.conf|grep -v ^#|grep -v ^$) result 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - fs.file-max = 6815744 kernel.sem = 250 32000 100 128 kernel.shmmni = 4096 kernel.shmall = 1073741824 kernel.shmmax = 4398046511104 kernel.panic_on_oops = 1 net.core.rmem_default = 262144 net.core.rmem_max = 4194304 net.core.wmem_default = 262144 net.core.wmem_max = 1048576 net.ipv4.conf.all.rp_filter = 2 net.ipv4.conf.default.rp_filter = 2 fs.aio-max-nr = 1048576 net.ipv4.ip_local_port_range = 9000 65500 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (cat /etc/sysctl.conf|grep -v ^#|grep -v ^$) used times: (0:00:00.058715s) 07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) on host (d4cdb.doufupi.com) 07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) result 07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Preparing... ######################################## 07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) used times: (0:00:00.105539s) 07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 40 - pylinuxautomulti.py - execute - ERROR - NoneType: None 07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - End of execute on server (d4cdb.doufupi.com) 07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 40 - pylinuxautomulti.py - execute - ERROR - Pls check the command (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) and run agin 07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - One of the command is run failed on (d4cdb.doufupi.com),pls check the logfile (H:\PO\worklog\pylinuxautomulti.d4cdb) for detail information !!!
7-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (date) on host (d4cdb.doufupi.com)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (date) result
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Sun Jun 7 14:12:09 +08 2020
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (date) used times: (0:00:00.053681s)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (uname) on host (d4cdb.doufupi.com)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (uname) result
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Linux
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (uname) used times: (0:00:00.067252s)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (cat /etc/security/limits.conf|grep -v ^#|grep -v ^$) on host (d4cdb.doufupi.com)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (cat /etc/security/limits.conf|grep -v ^#|grep -v ^$) result
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - oracle soft nofile 1024
oracle hard nofile 65536
oracle soft nproc 16384
oracle hard nproc 16384
oracle soft stack 10240
oracle hard stack 32768
oracle hard memlock 134217728
oracle soft memlock 134217728
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (cat /etc/security/limits.conf|grep -v ^#|grep -v ^$) used times: (0:00:00.065132s)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (cat /etc/security/limits.d/20-nproc.conf |grep -v ^#|grep -v ^$) on host (d4cdb.doufupi.com)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (cat /etc/security/limits.d/20-nproc.conf |grep -v ^#|grep -v ^$) result
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - * soft nproc 4096
root soft nproc unlimited
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (cat /etc/security/limits.d/20-nproc.conf |grep -v ^#|grep -v ^$) used times: (0:00:00.056713s)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (cat /etc/sysctl.conf|grep -v ^#|grep -v ^$) on host (d4cdb.doufupi.com)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (cat /etc/sysctl.conf|grep -v ^#|grep -v ^$) result
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - fs.file-max = 6815744
kernel.sem = 250 32000 100 128
kernel.shmmni = 4096
kernel.shmall = 1073741824
kernel.shmmax = 4398046511104
kernel.panic_on_oops = 1
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.default.rp_filter = 2
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (cat /etc/sysctl.conf|grep -v ^#|grep -v ^$) used times: (0:00:00.058715s)
07-06-2020 14:12:09 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Start execute command: (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) on host (d4cdb.doufupi.com)
07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Following is the (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) result
07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Preparing... ########################################
07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - Execute (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) used times: (0:00:00.105539s)
07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 40 - pylinuxautomulti.py - execute - ERROR -
NoneType: None
07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - End of execute on server (d4cdb.doufupi.com)
07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 40 - pylinuxautomulti.py - execute - ERROR - Pls check the command (rpm -ivh /mnt/hgfs/RPM/compat-libcap1-1.10-7.el7.x86_64.rpm) and run agin
07-06-2020 14:12:10 - pylinuxautomulti.d4cdb - 20 - pylinuxautomulti.py - execute - INFO - One of the command is run failed on (d4cdb.doufupi.com),pls check the logfile (H:\PO\worklog\pylinuxautomulti.d4cdb) for detail information !!!
标签:names bat create list() containe 配置 write nic mis
原文地址:https://www.cnblogs.com/tingxin/p/13060440.html