java实现远程连接服务器,监控某个目录下的日志文件

来源:互联网 发布:二代身份证识别器java 编辑:程序博客网 时间:2024/06/01 11:03

import java.io.BufferedReader;import java.io.DataInputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import ch.ethz.ssh2.Connection;import ch.ethz.ssh2.SCPClient;import ch.ethz.ssh2.SFTPv3Client;import ch.ethz.ssh2.Session;import ch.ethz.ssh2.StreamGobbler;public class TestRemoteConnect {    public static void main(String[] args)     {        String hostname = "xxx.xxx.xxx.xxx";        int port = 22;        String username = "xxx";        String password = "xxx";        Connection conn = new Connection(hostname,port);        Session ssh = null;        try        {            //连接到主机              conn.connect();            //使用用户名和密码校验              boolean isconn = conn.authenticateWithPassword(username, password);            if (!isconn)             {                System.out.println("用户名称或者是密码不正确");            }             else             {                System.out.println("已经连接OK");                File folder = new File("D://logs");                if(!folder.exists()){                folder.mkdir();                }                         SCPClient clt = conn.createSCPClient();                ssh = conn.openSession();                 ssh.execCommand("find /dev/shm/M/ -name '*.txt'");                InputStream is = new StreamGobbler(ssh.getStdout());                BufferedReader brs = new BufferedReader(new InputStreamReader(is));                while (true)                 {                    String line = brs.readLine();                    if (line == null)                     {                        break;                    }                                                           clt.get(line, "D://logs");                    List<File> lf = new ArrayList<File>();            test1 t = new test1();            lf = t.getFileList(new File("D://logs"),"txt");            for(File f:lf){            /*System.out.println(f.getPath());*/            String path=f.getPath();            File file = new File(path);            try{            FileReader fr = new FileReader(file);            BufferedReader br = new BufferedReader(fr);            String s = null;            Pattern p = Pattern.compile(".*?error.*?");            while((s = br.readLine()) != null){            Matcher m = p.matcher(s);            if(m.find()) {            /*System.out.println(m.matches());*/            System.out.println(line);            System.out.println("find error!");            }/*else{            System.out.println("no error");            }*/            }            br.close();            }catch(FileNotFoundException e){            System.err.println("file not found");            }catch(IOException e){            e.printStackTrace();            }                        }                            }                              }                    }         catch (IOException e)         {            System.out.println(e.getMessage());            e.printStackTrace();        }         finally         {            //连接的Session和Connection对象都需要关闭              if(ssh!=null)            {                ssh.close();            }            if(conn!=null)            {                conn.close();            }        }    }        public List<File> getFileList(File fileDir,String fileType){List<File> lfile = new ArrayList<File>();File[] fs = fileDir.listFiles();for(File f:fs){if(f.isFile()){if (fileType.equals(f.getName().substring(f.getName().lastIndexOf(".")+1, f.getName().length())))lfile.add(f);}else{List<File> ftemps = getFileList(f, fileType);lfile.addAll(ftemps);}}return lfile;}}


0 0