最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)服務(wù)器巡查的代碼

 更新時(shí)間:2021年12月07日 09:12:54   作者:csdncjh  
接到上級領(lǐng)導(dǎo)任務(wù),需要實(shí)現(xiàn)一個(gè)這樣的需求,一大批服務(wù)器,需要檢查服務(wù)器能否ping通,ssh密碼是否正常,以及檢查服務(wù)器的cpu,內(nèi)存,硬盤占用情況,下面通過java代碼實(shí)現(xiàn)服務(wù)器巡查功能,需要的朋友一起看看吧

需求

用戶,給了一大批服務(wù)器,需要檢查服務(wù)器能否ping通,ssh密碼是否正常,以及檢查服務(wù)器的cpu,內(nèi)存,硬盤占用情況。一個(gè)個(gè)檢查肯定不現(xiàn)實(shí),于是希望通過代碼實(shí)現(xiàn)。

批量ping

1.Jdk1.5的InetAddresss方式

注意:使用時(shí)應(yīng)注意,如果遠(yuǎn)程服務(wù)器設(shè)置了防火墻或相關(guān)的配制,可能會影響到結(jié)果。另外,由于發(fā)送ICMP請求需要程序?qū)ο到y(tǒng)有一定的權(quán)限,當(dāng)這個(gè)權(quán)限無法滿足時(shí), isReachable方法將試著連接遠(yuǎn)程主機(jī)的TCP端口。可能出現(xiàn)isReachable=false但是用cmd驗(yàn)證卻能夠ping通的情況。

代碼實(shí)現(xiàn):

   /**
     * 這個(gè)不可行,和ping的結(jié)果并不完全一致 盡量別用
     * @param ip
     * @return
     */
    public static boolean netOk(String ip){
        InetAddress address=null;
        boolean netOk=false;
        try {
            address =InetAddress.getByName(ip);
           netOk= address.isReachable(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  netOk;
    }

2.最簡單的辦法,直接調(diào)用CMD

注意:如果出現(xiàn)Windows環(huán)境下可以正常ping通的,而在centOS(Linux)里面無法ping通,可能是被ping的設(shè)備上有設(shè)置ping包大小限制,限制在了32位以內(nèi)。

最終使用

Process pro = Runtime.getRuntime().exec("ping -s 32 " + ipAddress );

并解析結(jié)果來進(jìn)行判斷。

命令行基礎(chǔ)

其中n是次數(shù),w是超時(shí)時(shí)間,單位是毫秒

ping -n 1 -w 1000 9.8.8.8

代碼實(shí)現(xiàn):

      /**
     * 
     * @param ipAddress
     * @param pingTimes 單位是毫秒
     * @param timeOut
     * @return
     */
public  static  boolean ping(String ipAddress,  int pingTimes,  int timeOut) {  
        BufferedReader in =  null;  
        Runtime r = Runtime.getRuntime();   //  將要執(zhí)行的ping命令,此命令是windows格式的命令  
        String pingCommand = "ping " + ipAddress + " -n " + pingTimes    + " -w " + timeOut;  
         try {    //  執(zhí)行命令并獲取輸出  
            System.out.println(pingCommand);   
            Process p = r.exec(pingCommand);   
             if (p ==  null) {    
                 return  false;   
            }
            in =  new BufferedReader( new InputStreamReader(p.getInputStream()));    //  逐行檢查輸出,計(jì)算類似出現(xiàn)=23ms TTL=62字樣的次數(shù)  
             int connectedCount = 0;   
            String line =  null;   
             while ((line = in.readLine()) !=  null) {    
                connectedCount += getCheckResult(line);   
            }    //  如果出現(xiàn)類似=23ms TTL=62這樣的字樣,出現(xiàn)的次數(shù)=測試次數(shù)則返回真  
             return connectedCount == pingTimes;  
        }  catch (Exception ex) {   
            ex.printStackTrace();    //  出現(xiàn)異常則返回假  
             return  false;  
        }  finally {   
             try {    
                in.close();   
            }  catch (IOException e) {    
                e.printStackTrace();   
            }  
        }
    }
 
// 若line含有=18ms TTL=16字樣,說明已經(jīng)ping通,返回1,否則返回0.
     private  static  int getCheckResult(String line) {   //  System.out.println("控制臺輸出的結(jié)果為:"+line);  
        Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)",    Pattern.CASE_INSENSITIVE);  
        Matcher matcher = pattern.matcher(line);  
         while (matcher.find()) {
             return 1;
        }
         return 0; 
    }

3.Java調(diào)用shell執(zhí)行ping命令

注意:這種需要在linux環(huán)境執(zhí)行

命令行基礎(chǔ)

其中 -c是次數(shù),-W是超時(shí)時(shí)間,單位是秒

ping -c 2 -W 1 8.8.8.8

  /**
     *
     * @param ipAddress
     * @param pingTimes
     * @param timeOut 單位 s
     * @return
     */
    public static boolean pingByShell(String ipAddress,  int pingTimes,  int timeOut){
        String sh="ping -c "+pingTimes+" -W "+timeOut+" "+ipAddress;
        String result = exeShell(sh);
        if(result!=null){
          if(result.contains("ttl")){
              return true;
          }
        }
        return false;
    }
 
 
/**
     * 執(zhí)行shell并且返回結(jié)果
     * @param sh
     * @return
     */
    public static String exeShell(String sh){
        Process process = null;
        List<String> processList = new ArrayList<String>();
        try {
            process = Runtime.getRuntime().exec(sh);
 
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                processList.add(line);
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String result = StringUtils.join(processList, "\n");
        return result;
    }

批量ssh檢查

需求:

先檢查網(wǎng)絡(luò)是否通(這里我是先檢查ssh用的22端口),不通的記錄下來,端口通的,再用文檔記錄的登錄信息ssh登錄,將登錄成功的和不成功的分別記錄下來。

注意:有的服務(wù)器有安全策略,不要同一臺服務(wù)器多次嘗試。

依賴引入

<!-- ssh  -->
<dependency>
    <groupId>ch.ethz.ganymed</groupId>
    <artifactId>ganymed-ssh2</artifactId>
    <version>262</version>
</dependency>

工具封裝

package com.isi.utils;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Calendar;
 
import org.apache.commons.lang3.StringUtils;
 
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
 
 
public class SshUtil {
    private static String DEFAULT_CHAR_SET = "UTF-8";
    private static String tipStr = "=======================%s=======================";
    private static String splitStr = "=====================================================";
 
    /**
     * 登錄主機(jī)
     * @return
     *      登錄成功返回true,否則返回false
     */
    public static Connection login(String ip, String userName, String password){
        boolean isAuthenticated = false;
        Connection conn = null;
        long startTime = Calendar.getInstance().getTimeInMillis();
        try {
            conn = new Connection(ip);
            conn.connect(); // 連接主機(jī)
 
            isAuthenticated = conn.authenticateWithPassword(userName, password); // 認(rèn)證
            if(isAuthenticated){
                System.out.println(String.format(tipStr, "認(rèn)證成功"));
            } else {
                System.out.println(String.format(tipStr, "認(rèn)證失敗"));
            }
        } catch (IOException e) {
            System.err.println(String.format(tipStr, "登錄失敗"));
            e.printStackTrace();
        }
        long endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("登錄用時(shí): " + (endTime - startTime)/1000.0 + "s\n" + splitStr);
        return conn;
    }
 
 
    public static boolean pwdCheck(String ip, String userName, String password){
        boolean isAuthenticated = false;
        Connection conn = null;
        long startTime = Calendar.getInstance().getTimeInMillis();
        try {
            conn = new Connection(ip);
            conn.connect(); // 連接主機(jī)
 
            isAuthenticated = conn.authenticateWithPassword(userName, password); // 認(rèn)證
            if(isAuthenticated){
                System.out.println(String.format(tipStr, "認(rèn)證成功"));
                return true;
            } else {
                System.out.println(String.format(tipStr, "認(rèn)證失敗"));
                return false;
            }
        } catch (Exception e) {
            System.err.println(String.format(tipStr, "登錄失敗"));
            e.printStackTrace();
        }finally {
            if(conn!=null){
                conn.close();
            }
        }
        long endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("登錄用時(shí): " + (endTime - startTime)/1000.0 + "s\n" + splitStr);
        return false;
    }
 
 
    /**
     * 遠(yuǎn)程執(zhí)行shell腳本或者命令
     * @param cmd
     *      即將執(zhí)行的命令
     * @return
     *      命令執(zhí)行完后返回的結(jié)果值
     */
    public static String execute(Connection conn, String cmd){
        String result = "";
        Session session = null;
        try {
            if(conn != null){
                session = conn.openSession();  // 打開一個(gè)會話
                session.execCommand(cmd);      // 執(zhí)行命令
                result = processStdout(session.getStdout(), DEFAULT_CHAR_SET);
 
                //如果為得到標(biāo)準(zhǔn)輸出為空,說明腳本執(zhí)行出錯(cuò)了
                if(StringUtils.isBlank(result)){
                    System.err.println("【得到標(biāo)準(zhǔn)輸出為空】\n執(zhí)行的命令如下:\n" + cmd);
                    result = processStdout(session.getStderr(), DEFAULT_CHAR_SET);
                }else{
                    System.out.println("【執(zhí)行命令成功】\n執(zhí)行的命令如下:\n" + cmd);
                }
            }
        } catch (IOException e) {
            System.err.println("【執(zhí)行命令失敗】\n執(zhí)行的命令如下:\n" + cmd + "\n" + e.getMessage());
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
            if (session != null) {
                session.close();
            }
        }
        return result;
    }
 
    /**
     * 解析腳本執(zhí)行返回的結(jié)果集
     * @param in 輸入流對象
     * @param charset 編碼
     * @return
     *       以純文本的格式返回
     */
    private static String processStdout(InputStream in, String charset){
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line = null;
            while((line = br.readLine()) != null){
                buffer.append(line + "\n");
            }
        } catch (UnsupportedEncodingException e) {
            System.err.println("解析腳本出錯(cuò):" + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("解析腳本出錯(cuò):" + e.getMessage());
            e.printStackTrace();
        }
        return buffer.toString();
    }
 
    /**
     * 判斷端口是否通
     * @param host
     * @param port
     * @param timeout
     * @return
     */
    public static boolean isHostConnectable(String host, int port,int timeout) {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(host, port),timeout);
            socket.setSoTimeout(timeout);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
 
}

密碼核對

    public static void main(String[] args){
        String ip = "192.168.4.188";   // 此處根據(jù)實(shí)際情況,換成自己需要訪問的主機(jī)IP
        String userName = "root";
        String password = "123456";
        boolean flag = pwdCheck(ip, userName, password);
        System.out.println("flag===="+flag);
 
    }

資源巡查

使用封裝的sshUtil

    public static void main(String[] args){
        // 此處根據(jù)實(shí)際情況,換成自己需要訪問的主機(jī)IP
        String ip = "192.168.4.188";
        String userName = "root";
        String password = "123456";
/*        boolean flag = pwdCheck(ip, userName, password);
        System.out.println("flag===="+flag);*/
 
        Connection connection = login(ip, userName, password);
 
        //查看最大的盤 指定---為分隔符,默認(rèn)的分隔符java無法指定
        String max_disk_cmd="df | sort -k2nr | head -1 | awk '{print $2,$3,$4,$5}' OFS='---' ";
        //查看內(nèi)存使用情況
        String free_mem_cmd="free | sort -k2nr | head -1 | awk '{print $2,$3,$4}' OFS='---'";
        //計(jì)算cpu使用率, -n number:指定在結(jié)束前應(yīng)該產(chǎn)生的最大迭代次數(shù)或幀數(shù),一般迭代次數(shù)越大,獲取到的cpu使用率越準(zhǔn)確; 本腳本中是迭代了5次,"-n5"
        String cpu_use_cmd="top -b -n5 | fgrep \"Cpu(s)\" | tail -1 | awk -F'id,' '{split($1, vs, \",\"); v=vs[length(vs)]; sub(/\\s+/, \"\", v);sub(/\\s+/, \"\", v); printf \"%d\", 100-v;}'";
 
        String result = execute(connection, cpu_use_cmd);
        System.out.println("result = " + result);
        String[] datas = result.split("---");
        String str = Arrays.toString(datas);
        System.out.println("str = " + str);
        closeConn(connection);
    }

使用命令組合 sort

#-n:?--numeric-sort? ? 按照數(shù)字的值進(jìn)行比較

#-k:--key=POS1[,POS2]? ? ?start a key at POS1 (origin 1), end it at POS2

? ? ? ? 按指定的列做排序

#-r:--reverse? ? 反序排列

-t:指定排序用的字段的分隔符

sort -t ":" -k 3 -nr /etc/passwd

按兩個(gè)字段做排序:

sort -t: -k7 -k3nr /etc/passwd

參考:http://m.fzitv.net/article/134816.htm

head

head?? -n? 1? 顯示文件前1行內(nèi)容

head?? -n? -1? 顯示文件除了最后1行的內(nèi)容

head?? -c?? 5? 顯示文件前n個(gè)字節(jié)

?head?? -c?? -5? 顯示文件除了最后5個(gè)字節(jié)的內(nèi)容

awk

輸出指定列

awk '{print $1,$4}' test

?

格式化輸出,指定輸出分隔符

awk '{printf "%-8s %-10s\n",$1,$4}' test

也可以用

awk '{print $1,$2}'?OFS=' '

?指定輸入分隔符

awk -F, '{print $1,$2}' test

參考:http://m.fzitv.net/article/174434.htm

到此這篇關(guān)于java實(shí)現(xiàn)服務(wù)器巡查的文章就介紹到這了,更多相關(guān)java服務(wù)器巡查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談Maven包沖突的原理及解決方法

    淺談Maven包沖突的原理及解決方法

    這篇文章主要介紹了淺談Maven包沖突的原理及解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java中struts2實(shí)現(xiàn)文件上傳下載功能

    java中struts2實(shí)現(xiàn)文件上傳下載功能

    這篇文章主要介紹了java中struts2實(shí)現(xiàn)文件上傳下載功能的方法,以實(shí)例形式分析了struts2文件上傳下載功能的實(shí)現(xiàn)技巧與相關(guān)問題,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2016-05-05
  • 在SpringBoot中利用RocketMQ實(shí)現(xiàn)批量消息消費(fèi)功能

    在SpringBoot中利用RocketMQ實(shí)現(xiàn)批量消息消費(fèi)功能

    RocketMQ 是一款分布式消息隊(duì)列,支持高吞吐、低延遲的消息傳遞,對于需要一次處理多條消息的場景,RocketMQ 提供了批量消費(fèi)的機(jī)制,這篇文章將展示如何在 Spring Boot 中實(shí)現(xiàn)這一功能,感興趣的小伙伴跟著小編一起來看看吧
    2024-11-11
  • Java Web基于Session的登錄實(shí)現(xiàn)方法

    Java Web基于Session的登錄實(shí)現(xiàn)方法

    這篇文章主要介紹了Java Web基于Session的登錄實(shí)現(xiàn)方法,涉及Java針對session的操作及表單提交與驗(yàn)證技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Maven離線模式打包全過程

    Maven離線模式打包全過程

    這篇文章主要介紹了Maven離線模式打包全過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • JPA的多表復(fù)雜查詢的方法示例

    JPA的多表復(fù)雜查詢的方法示例

    這篇文章主要介紹了JPA的多表復(fù)雜查詢的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Java?SE判斷兩個(gè)文件內(nèi)容是否相同的多種方法代碼

    Java?SE判斷兩個(gè)文件內(nèi)容是否相同的多種方法代碼

    昨天因?yàn)橐獛蛶熜值拿λ钥戳艘幌氯绾闻袛鄡蓚€(gè)文件內(nèi)容是否相同,這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于Java?SE判斷兩個(gè)文件內(nèi)容是否相同的多種方法,需要的朋友可以參考下
    2023-11-11
  • springboot中websocket簡單實(shí)現(xiàn)

    springboot中websocket簡單實(shí)現(xiàn)

    本文主要介紹了springboot中websocket簡單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Java并發(fā)編程之詳解ConcurrentHashMap類

    Java并發(fā)編程之詳解ConcurrentHashMap類

    在之前的文章中已經(jīng)為大家介紹了java并發(fā)編程的工具:BlockingQueue接口、ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue、BlockingDeque接口,本文為系列文章第八篇.需要的朋友可以參考下
    2021-06-06
  • Spring實(shí)戰(zhàn)之類級別緩存實(shí)現(xiàn)與使用方法

    Spring實(shí)戰(zhàn)之類級別緩存實(shí)現(xiàn)與使用方法

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之類級別緩存實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了Spring類級別緩存配置、屬性、領(lǐng)域模型等相關(guān)操作技巧,需要的朋友可以參考下
    2020-01-01

最新評論

山阴县| 尉氏县| 溆浦县| 确山县| 乡城县| 三原县| 伊川县| 贵溪市| 新余市| 大余县| 茶陵县| 胶南市| 石渠县| 湄潭县| 报价| 舟山市| 涞源县| 大渡口区| 西畴县| 内黄县| 崇州市| 孟州市| 曲麻莱县| 大庆市| 宁安市| 泾源县| 安平县| 武平县| 宾阳县| 怀远县| 金堂县| 师宗县| 乾安县| 宝兴县| 陕西省| 兰考县| 绥中县| 和顺县| 威海市| 兖州市| 贡觉县|