使用ganymed-ssh2-build通过ssh获得远程服务器参数

1、项目中需要检测到几台远程服务器的参数,差了很多资料,决定用的这个

2、jar包:ganymed-ssh2-build210.jar

3、原理:向远程linux服务器发送脚本命令,得到该台服务器的信息

4、代码如下:

public class Basic {
    public static void main(String[] args) {
        String hostname1 = "";
        String username1 = "";
        String password1 = "";

        String hostname2 = "";
        String username2 = "";
        String password2 = "";
        String hostname3 = "";
        String username3 = "";
        String password3 = "";

        /**
         * 服务器1
         */
        Montor montor1 = getMontor(hostname1, username1, password1);
        /**
         * 服务器2
         */
        Montor montor2 = getMontor(hostname2, username2, password2);
        /**
         * 服务器3
         */
        Montor montor3 = getMontor(hostname3, username3, password3);
        System.out.println(montor1.toString());
        System.out.println(montor2.toString());
        System.out.println(montor3.toString());

    }

    private static Montor getMontor(String hostname, String username,
            String password) {
        Montor montor = null;
        try {
            Connection conn = new Connection(hostname);
            conn.connect();
            boolean isAuthenticated;
            isAuthenticated = conn.authenticateWithPassword(username, password);

            if (isAuthenticated == false)
                throw new IOException("Authentication failed.");

            
            montor = new Montor();
            montor.setHostName(exec(conn, "hostname"));
            montor.setDiskSpace(exec(conn, "df -h | awk 'NR==2 {print $2}'"));
            montor.setUserSpace(exec(conn, "df -h | awk 'NR==2 {print $3}'"));
            montor.setRemainingSpace(exec(conn,
                    "df -h | awk 'NR==2 {print $4}'"));
            montor.setMemory(exec(conn, "free -m | awk 'NR==2 {print $2}'"));
            montor.setUseMemory(exec(conn, "free -m | awk 'NR==2 {print $3}'"));
            montor.setRemainingMemory(exec(conn,
                    "free -m | awk 'NR==2 {print $4}'"));
            BigDecimal b1 = new BigDecimal(exec(conn,
                    "free -m | awk 'NR==2 {print $3}'"));
            BigDecimal b2 = new BigDecimal(exec(conn,
                    "free -m | awk 'NR==2 {print $2}'"));
            montor.setUsageMemory((b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP))
                    .doubleValue()
                    * 100 + "%");
            conn.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return montor;

    };
    /**
     * 
     * @param conn 连接
     * @param command 执行的sheel命令
     * @return
     */
    private static String exec(Connection conn, String command) {
        String data = "";
        try {
            Session sess = conn.openSession();
            //执行命令
            sess.execCommand(command);
            InputStream stdout = new StreamGobbler(sess.getStdout());
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(stdout));

            while (true) {
                String line = br.readLine();
                if (line == null)
                    break;
                data = line;
            }
            br.close();
            sess.close();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(2);
        }
        return data;
    }
}

5、目前还需要服务器中各项服务的联通性,如tomcat,active-mq等服务 是否挂机,如有大婶知道,望告知

6、如果还有其他更好的方式望周知。

你可能感兴趣的