Java 批量ping操作

批量ping操作

1 批量ping操作,所需jar包

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk13</classifier>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna-platform</artifactId>
            <version>4.0.0</version>
        </dependency>

2 工具包下载

点击下载工具包 dpso,解压后,拷贝到linux下,如:/opt/log/目录下

3 Java测试代码

package com.wutongyu.test;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by wutongyu on 2018/2/24.
 */
public class TestPing {
    public interface LgetLib extends Library {
        LgetLib INSTANCE = (LgetLib) Native.loadLibrary("/opt/log/dpso/host_scan/lib/libdba_capi.so", LgetLib.class);
        Pointer callFunc(String action, String params );
        void freeFunc( Pointer paramPointer );
    }

    public static String call(String action, String params) {
        Pointer point=  LgetLib.INSTANCE.callFunc(action, params);
        String ret = point.getString(0L);
        LgetLib.INSTANCE.freeFunc(point);
        return ret;

    }
    public static void main(String[] args) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        while (true){
            Date date=new Date();
            String dateStr = bartDateFormat.format(date);
            String callstr = TestPing.call("host_scan", "{\"count\":5, \"timeout\":10000, \"interval\":100, \"hosts\":[\"10.238.129.11\",\"10.238.129.41\",\"10.238.129.18\",\"10.238.129.47\",]}");
            System.out.println(dateStr+"    "+callstr);
            try {
                Thread.sleep(120*1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

4 返回值及解析

返回如下Json串:
{
    "errcode":"ERRCODE",
    "errmsg":"ERRMSG",
    "data":
    [
        {
            "host":"10.238.129.11",
            "min_reply_msec":"0",
            "max_reply_msec":"0",
            "avg_reply_msec":"0",
            "total_count":"5",
            "lost_count":"5",
            "lost_ratio":"1.0"
        },
        ...
    ]
}
参数说明:
    host: ipv4或者ipv6地址,
    min_reply_msec: 最小响应时间,单位:毫秒,
    max_reply_msec: 最大响应时间,单位:毫秒,
    avg_reply_msec: 平均响应时间,单位:毫秒,
    total_count: 总包数,
    lost_count: 丢包数,
    lost_ratio: 丢包率,以实数表示,例如:0.5表示丢包率50%
一般的,当丢包率为1.0时,认为ping不通  

猜你喜欢

转载自blog.csdn.net/wutongyuWxc/article/details/80705116