日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

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

發(fā)布時(shí)間:2023/12/13 综合教程 32 生活家
生活随笔 收集整理的這篇文章主要介紹了 使用ganymed-ssh2-build通过ssh获得远程服务器参数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、項(xiàng)目中需要檢測(cè)到幾臺(tái)遠(yuǎn)程服務(wù)器的參數(shù),差了很多資料,決定用的這個(gè)

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

3、原理:向遠(yuǎn)程linux服務(wù)器發(fā)送腳本命令,得到該臺(tái)服務(wù)器的信息

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 = "";

        /**
         * 服務(wù)器1
         */
        Montor montor1 = getMontor(hostname1, username1, password1);
        /**
         * 服務(wù)器2
         */
        Montor montor2 = getMontor(hostname2, username2, password2);
        /**
         * 服務(wù)器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 執(zhí)行的sheel命令
     * @return
     */
    private static String exec(Connection conn, String command) {
        String data = "";
        try {
            Session sess = conn.openSession();
            //執(zhí)行命令
            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、目前還需要服務(wù)器中各項(xiàng)服務(wù)的聯(lián)通性,如tomcat,active-mq等服務(wù) 是否掛機(jī),如有大嬸知道,望告知

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

總結(jié)

以上是生活随笔為你收集整理的使用ganymed-ssh2-build通过ssh获得远程服务器参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。