当前位置: 首页 > 工具软件 > fping > 使用案例 >

Fping操作

戈博易
2023-12-01

一、fping 的优点:

1. 可以一次ping多个主机

2. 可以从主机列表文件ping

3. 结果清晰 便于脚本处理

4. 速度快

二 、常用参数介绍

-a:只显示出存活的主机(相反参数-u)
1、通过标准输入方式
fping + IP1 + IP2
-g 支持主机端的方式 192.168.1.1 192.168.1.255 或者 192.168.1.0/24
2、通过读取一个文件中IP内容
fping -f filename

Usage: fping [options] [targets...]

-a         show targets that are alive

-A         show targets by address

-b n       amount of ping data to send, in bytes (default 56)

-B f       set exponential backoff factor to f

-c n       count of pings to send to each target (default 1)

-C n       same as -c, report results in verbose format

-e         show elapsed time on return packets

-f file    read list of targets from a file ( - means stdin) (only if no -g specified)

-g         generate target list (only if no -f specified)             (specify the start and end IP in the target list, or supply a IP netmask)             (ex. fping -g 192.168.1.0 192.168.1.255 or fping -g 192.168.1.0/24)

-i n       interval between sending ping packets (in millisec) (default 25)

-l         loop sending pings forever

-m         ping multiple interfaces on target host

-n         show targets by name (-d is equivalent)

-p n       interval between ping packets to one target (in millisec)             (in looping and counting modes, default 1000)

-q         quiet (don't show per-target/per-ping results)

-Q n       same as -q, but show summary every n seconds

-r n       number of retries (default 3)

-s         print final stats

-t n       individual target initial timeout (in millisec) (default 500)

-u         show targets that are unreachable

-v         show version

targets    list of targets to check (if no -f specified)

工具类:

package com.kyland.view.Utils;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

import java.util.Queue;

import java.util.logging.Logger;

 

public class TopoUtils {

    /**

     * Ping操作

     * @return

     */

    public static boolean isConnect( String ip ){

        boolean connect = false;

        Runtime runtime = Runtime.getRuntime();

        Process process;

        try {

            process = runtime.exec("ping -n 4 " + ip);

            InputStream is = process.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);

            String line = null;

            StringBuffer sb = new StringBuffer();

            while ((line = br.readLine()) != null) {

                sb.append(line);

            }

//            System.out.println("返回值为:"+sb);

            is.close();

            isr.close();

            br.close();

 

            if (null != sb && !sb.toString().equals("")) {

                String logString = "";

                if (sb.toString().indexOf("TTL") > 0) {

                    // 网络畅通

                    connect = true;

                } else {

                    // 网络不畅通

                    connect = false;

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        return connect;

    }

 

    public static List<String> connectIpList(String start, String end ){

        List<String> connect = new ArrayList<>();

        Runtime runtime = Runtime.getRuntime();

        Process process;

        try {

            process = runtime.exec("fping -g" +start+"/"+end+" -p");

            InputStream is = process.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);

            String line = null;

            StringBuffer sb = new StringBuffer();

            while ((line = br.readLine()) != null) {

                if(line.contains("Reply") || line.contains("reply")){

                   String[] strs = line.split("from")[1].split(":");

                    connect.add(strs[0].replace(" ",""));

                    System.out.println("toptutils>>"+strs[0].replace(" ",""));

                }

            }

            is.close();

            isr.close();

            br.close();

 

        } catch (IOException e) {

            e.printStackTrace();

        }

        return connect;

    }

    /**

     * 通过IP首尾获取IP地址

     * @param ipfrom 起始IP

     * @param ipto 结束IP

     * @return

     */

    public static Queue getIPArr(String ipfrom, String ipto) {

        ArrayList<String> ips = new ArrayList<String>();

        Queue<String> ipss = new LinkedList<>();

        String[] ipfromd = ipfrom.split("\\.");

        String[] iptod = ipto.split("\\.");

        int[] int_ipf = new int[4];

        int[] int_ipt = new int[4];

        for (int i = 0; i < 4; i++) {

            int_ipf[i] = Integer.parseInt(ipfromd[i]);

            int_ipt[i] = Integer.parseInt(iptod[i]);

        }

        for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {

            for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1]

                    : 255); B++) {

                for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2]

                        : 255); C++) {

                    for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3]

                            : 255); D++) {

                        ips.add(new String(A + "." + B + "." + C + "." + D));

                        ipss.add(new String(A + "." + B + "." + C + "." + D));

//                        System.out.println(A + "." + B + "." + C + "." + D);

                    }

                }

            }

        }

        return ipss;

    }

    /**

    *@Description:

    *@Author: 

    *@date: 2019/7/25 10:30

    */

    public static List<String> connectIpList(List<String> ips){

        List<String> connect = new ArrayList<>();

        Runtime runtime = Runtime.getRuntime();

        Process process;

        StringBuffer stringBuffer=new StringBuffer();

        try {

            for (String ip : ips) {

                stringBuffer.append(ip+" ");

            }

            process = runtime.exec("fping "+stringBuffer);

            InputStream is = process.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);

            String line = null;

            StringBuffer sb = new StringBuffer();

            while ((line = br.readLine()) != null) {

                if(line.contains("Reply") || line.contains("reply")){

                    String[] strs = line.split("from")[1].split(":");

                    connect.add(strs[0].replace(" ",""));

                    System.out.println("toptutils>>"+strs[0].replace(" ",""));

                }

            }

            is.close();

            isr.close();

            br.close();

 

        } catch (IOException e) {

            e.printStackTrace();

        }

        return connect;

    }

    public static void main(String args[]){

        List<String> list=TopoUtils.connectIpList("192.168.0.10","192.168.0.15");

        for (String s : list) {

            System.out.println(s);

        }

        List<String> list1=new ArrayList<>();

        list1.add("192.168.177.1");

        list1.add("192.168.177.2");

        list1.add("192.168.177.3");

        list1.add("192.168.177.4");

        list1.add("192.168.177.5");

        list1.add("192.168.177.6");

        list1.add("192.168.177.7");

        list1.add("192.168.177.8");

        list1.add("192.168.177.9");

        list1.add("192.168.177.10");

        System.out.println("==================");

        for (String s : TopoUtils.connectIpList(list1)) {

            System.out.println(s);

        }

    }

}

参考https://blog.csdn.net/chengqiuming/article/details/78602108

 

 

 类似资料: