python linux shell

本文介绍了一个用于处理日志文件的Python脚本,该脚本能够读取指定日期的日志,执行shell命令并收集结果,最后将汇总信息保存到指定目录下的文件中。此脚本适用于自动化日志分析任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- 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()

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值