# -*- coding:utf-8 -*-
#working for fs2log
import sys,os
import time
#查找路径
def file_dir():
path = sys.path[0]
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
#输出结果
def output(log,path,month):
txtlog = log
recordfile = os.path.join(path+'/'+month)
try:
fp = open(recordfile , "a+")
print >>fp , txtlog
fp.close()
except:
pass
运行shell
def runshell(path,command,day):
cmd = command
a = os.popen(cmd.replace('data',day).replace('path',path)).read()
return a
读取shell
def readconfig(path,day,month):
thecmd = open(path+'/command','rb')
printout = ''
printout = day
try:
for line in thecmd.xreadlines():
printout=printout+' '+runshell(path,line,day)[:-1]
print printout
thecmd.close()
except:
pass
output(printout,path,month)
#参数时间,
def main():
if len(sys.argv)>1:
LogDate=sys.argv[1]
LogMonth=sys.argv[1][0:6]
readconfig(file_dir(),LogDate,LogMonth)
else:
LogDate=time.strftime('%Y%m%d',time.localtime(time.time()-86400))
LogMonth=LogDate[0:6]
readconfig(file_dir(),LogDate,LogMonth)
if __name__ == "__main__":
main()
python linux shell
最新推荐文章于 2024-08-23 20:18:15 发布
本文介绍了一个用于处理日志文件的Python脚本,该脚本能够读取指定日期的日志,执行shell命令并收集结果,最后将汇总信息保存到指定目录下的文件中。此脚本适用于自动化日志分析任务。
236

被折叠的 条评论
为什么被折叠?



