zoukankan      html  css  js  c++  java
  • 使用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、如果还有其他更好的方式望周知。

  • 相关阅读:
    poj 3528 (三维几何求凸包+凸包表面积)
    dijkstra模板(好像是斐波那契额堆优化,但我为什么看起来像优先队列优化,和spfa一样)
    最大空凸包模板
    ICPC 2017–2018, NEERC, Northern Subregional Contest St Petersburg, November 4, 2017 I题
    hdu 5248 序列变换
    hdu 2063(二分图模板测试)
    组合数
    85. Maximal Rectangle 由1拼出的最大矩形
    750. Number Of Corner Rectangles四周是点的矩形个数
    801. Minimum Swaps To Make Sequences Increasing 为使两个数组严格递增,所需要的最小交换次数
  • 原文地址:https://www.cnblogs.com/volare/p/4318824.html
Copyright © 2011-2022 走看看