1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | //日志 1 中特殊字符信息 String uniqeStr = "RunID: 123"; //日志 1 文件位置 String serverlogFilePath = "/opt/server/log/server.log"; //日志 2 中特殊字符信息 String jobIdExpr = "id=[0-9]+"; //日志 2 文件位置 String joblogFilePath = "/opt/server/log/job.log"; JSch sshSingleton = new JSch(); Properties userProp = new Properties(); userProp.load(new FileReader("user.properties")); String userName = userProp.getProperty("username"); String password = userProp.getProperty("password"); Properties serversProp = new Properties(); serversProp.load(new FileReader("servers.properties")); Pattern p = Pattern.compile(jobIdExpr); for (Map.Entry<Object, Object> serverProp : serversProp.entrySet()) { String name = (String) serverProp.getKey(); String server = (String) serverProp.getValue(); Session session = sshSingleton.getSession(userName, server); session.setPassword(password); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); //打开执行管道 ChannelExec channel = (ChannelExec) session.openChannel("exec"); BufferedReader in = new BufferedReader(new InputStreamReader( channel.getInputStream())); //设置命令,从日志 1 中找出关键字符信息 channel.setCommand("cat " + serverlogFilePath + " |grep -w " + uniqeStr); channel.connect(); String msg; String jobId = null; while ((msg = in.readLine()) != null) { Matcher m = p.matcher(msg); if (m.find()) { jobId = m.group(); break; } } //关闭第一个执行管道 channel.disconnect(); if (jobId != null) { //日志 1 中发现关键字符信息后,在同一服务器中日志文件 2 中继续查找 System.out.println("found log in jobServer: " + name); //另外再打开一个新的执行管道 channel = (ChannelExec) session.openChannel("exec"); in = new BufferedReader(new InputStreamReader( channel.getInputStream())); String cmd = "cat " + joblogFilePath + " |grep -A 10 " + jobId; channel.setCommand(cmd); channel.connect(); //输出需要查找的日志信息 while ((msg = in.readLine()) != null) { System.out.println(msg); } channel.disconnect(); } session.disconnect(); if (jobId != null) { //已经在这台服务器中找到日志,不需要继续去其他服务器中查找了 break; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | String serverFile = "/opt/log/1.log"; String localFolder = "C:/tmp"; String localFile = "C:/tmp/user.xml"; String serverFolder = "/tmp/"; JSch sshSingleton = new JSch(); Properties userProp = new Properties(); userProp.load(new FileReader("user.properties")); String userName = userProp.getProperty("username"); String password = userProp.getProperty("password"); String server = "192.168.0.1"; //设置端口 int port = 22; Session session = sshSingleton.getSession(userName, server, port); session.setPassword(password); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); //打开 ftp 管道 Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp c = (ChannelSftp) channel; //转到根目录,便于使用绝对路径来进行文件传输 c.cd("/"); //从服务器上下载日志文件 1.log 到本地目录 C:/tmp c.get(serverFile, localFolder); //上传配置文件 user.xml 到服务器上,如果服务器上已经存在该文件,则覆盖它 c.put(localFile, serverFolder, ChannelSftp.OVERWRITE); session.disconnect(); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |