Python
http://www.runoob.com/python/python-tutorial.html
https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz
tar Jxvf Python-3.5.0.tar.xz
cd Python-3.5.0
./configure --prefix=/usr/local/python3
make && make install
编写python脚本文件并执行 python xxx.py
#!/usr/bin/python
import commands;
import os;
import datetime;
import time;
dateStr = time.strftime("%Y-%m-%d", time.localtime());
dateStr2 = datetime.datetime.now().strftime('%Y-%m-%d');
rep = "replaceStr".replace("replace",dateStr2);
print dateStr," ",dateStr2," ",rep;
servername = "log/my1.log";
result = os.popen("grep \'xxx\' " + servername +" | wc -l").read();
print "ERROR::日志xxx出现的次数::show::",result,"::",servername,"####";
LogPathsLive.py 文件
#!/usr/bin/python
cashierWebLive = [
'log/my.log',
'log/my1.log'
];
cashierServiceLive = [
'log/my4.log',
'log/my5.log'
];
LogPathsTomcat.py 文件
#!/usr/bin/python
cashierWebTomcat = [
'log/my2.log',
'log/my3.log'
];
日志搜索脚本文件 pythonSearch.py
#!/usr/bin/python # -*- coding: UTF-8 -*-] import LogPathsLive; import LogPathsTomcat; import commands; import os; import datetime; import sys; serverList = ['cashierWeb','cashierService']; searchInfo = raw_input("\n Please enter search information:") if searchInfo == "": sys.exit(); print "\n search information:",searchInfo; dateStr = raw_input("\n Please enter search date or now date:") tal = "old"; if dateStr == "": tal = "now"; dateStr = datetime.datetime.now().strftime('%Y-%m-%d'); print "\n search date:",dateStr; tomcatOrLive = raw_input("\n Please enter search log level live or tomcat:") if tomcatOrLive == "": tomcatOrLive = "live"; else: if tomcatOrLive == "l" or tomcatOrLive == "L": tomcatOrLive = "live"; elif tomcatOrLive == "t" or tomcatOrLive == "T": tomcatOrLive = "tomcat"; else: sys.exit(); print "\n log level:",tomcatOrLive; systemName = raw_input("\n Please enter search system name:") if systemName == "": print "\n search system name , all."; print "\n search system name:" , systemName; print "\n" if systemName != "": logs = []; if systemName == 'cashierWeb' or systemName == 'cw': if tomcatOrLive == "live": logs = LogPathsLive.cashierWebLive; else: logs = LogPathsTomcat.cashierWebTomcat; elif systemName == 'cashierService' or systemName == 'cs': if tomcatOrLive == "live": logs = LogPathsLive.cashierServiceLive; else: logs = LogPathsTomcat.cashierServiceTomcat; else: sys.exit(); else: sys.exit(); for log in logs: if tomcatOrLive == "live": if tal == "now": print "########## grep '",searchInfo,"' ",log print os.popen("grep '" + searchInfo + "' " + log).read(); else: print "########## grep '",searchInfo ,"' " ,log,"_",dateStr,".log" print os.popen("grep '" + searchInfo + "' " + log + "_" + dateStr + ".log").read(); else: if tal == "now": log = log.replace("DATESTRING",dateStr); print "########## grep '",searchInfo,"' ",log print os.popen("grep '" + searchInfo + "' " + log).read(); else: log = log.replace("DATESTRING",dateStr); log = log.replace("logs/catalina","logs/bak/catalina"); print "########## grep '",searchInfo,"' ",log print os.popen("grep '" + searchInfo + "' " + log).read();
python脚本执行API
com.jcraft.jsch.JSch
com.jcraft.jsch.Session
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency> /** * 连接远程服务器 * @param host ip地址 * @param userName 登录名 * @param password 密码 * @param port 端口 * @param cmd 要执行的命令字符串 * @throws Exception */ public String[] runPython(String host,String userName,String password,int port,String cmd){ try{ JSch jsch = new JSch(); // 创建JSch对象 Session session = jsch.getSession(userName, host, port); // 根据用户名,主机ip,端口获取一个Session对象 session.setPassword(password); // 设置密码 Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); // 为Session对象设置properties session.setTimeout(timeout); // 设置timeout时间 session.connect(); // 通过Session建立链接 ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setCommand(cmd); channelExec.setInputStream(null); channelExec.setErrStream(System.err); channelExec.connect(); InputStream in = channelExec.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8"))); String buf = null; StringBuffer sb = new StringBuffer(); //System.out.println( reader.readLine()); while ((buf = reader.readLine()) != null){ System.out.println("python脚本执行结果为:" + buf); sb.append(buf); } reader.close(); channelExec.disconnect(); session.disconnect(); String[] as = sb.toString().split("####"); System.out.println(as); return as; }catch(Exception e){ // logger.error("Auth fail",e); e.printStackTrace(); } return null; }