• 使用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、如果还有其他更好的方式望周知。

  • 相关阅读:
    Linq增删查改
    ObjectiveC 的一些记录
    MAX+VC版的RenderMonkey
    PHP校验身份证是否合法
    将小程序scene中的字符串转为对象
    使用JavaScript与Servlet实现客户端与服务器端验证
    分析jdbc程序的编写步骤和原理
    解析Tomcat生成源代码分析javaBean与对象的生成关系
    《Python基础教程》要点(十):充电时刻:导入模块
    《Python基础教程》要点(七):更加抽象:类
  • 原文地址:https://www.cnblogs.com/volare/p/4318824.html
Copyright © 2020-2023  润新知