Javaリモート・サーバ・ログの監視


// An highlighted block
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class client {
     
    public static void main(String args[]) throws JSchException, IOException {
     

        Map<JSch,String> map = new HashMap<JSch, String>();
        map.put(new JSch(),"100.88.57.129");
        map.put(new JSch(),"11.166.32.1000004");
        map.put(new JSch(),"11.166.225.50");
        Set<JSch> keys = map.keySet();
        for(JSch key: keys) {
     
            String host = map.get(key);
            String username = "log";
            Session session = key.getSession(username, host, 22);
            session.setPassword("log_xixihaha");
            session.setConfig("StrictHostKeyChecking", "no");
            session.setTimeout(3000);
            try {
     
                session.connect();
                System.out.println();
                String command1 = "tail -f  /home/admin/logs/mrchmobile/*.log | grep 2088102154804654";
                String command2 = "head -n 1  /home/admin/logs/mrchmobile/*.log";
                ChannelExec channel = (ChannelExec) session.openChannel("exec");
                channel.setCommand(command2);
                channel.connect();
                BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream()));

                String msg;
                System.out.println(" :" + host + " :");
                while ((msg = in.readLine()) != null) {
     
                    System.out.println(msg);
                }
                channel.disconnect();
                session.disconnect();
            }catch (JSchException e){
     
                System.out.println(" :"+host+" ");

            };

        }