Linux C/C++ fping命令(检查主机是否存在)

article/2025/10/23 17:27:26

ping基本上是验证网络连接的最简单工具。我们可以验证专用或公共网络中任意两个设备之间的连接。但是今天我们要讲的是fping,因为fping是一个类似ping的程序,它使用Internet控制消息协议(ICMP)回显请求来确定目标主机是否正在响应。

fping与ping的不同之处在于,您可以在命令行上指定任意数量的目标,或者指定包含要ping的目标列表的文件。fping不会发送到一个目标直到超时或回复,而是发送一个ping数据包,然后以循环方式转到下一个目标。

在默认模式下,如果目标回复,则会将其标记并从要检查的目标列表中删除;如果目标在某个时间限制和/或重试限制内没有响应,则将其指定为不可访问。

fping 命令介绍

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-D         print timestamp before each output line-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)-H n       Set the IP TTL value (Time To Live hops)-i n       interval between sending ping packets (in millisec) (default 25)-I if      bind to a particular interface-l         loop sending pings forever-m         ping multiple interfaces on target host-M         set the Don't Fragment flag-n         show targets by name (-d is equivalent)-N         output compatible for netdata (-l -Q are required)-o         show the accumulated outage time (lost packets * packet interval)-O n       set the type of service (tos) flag on the ICMP packets-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)-R         random packet data (to foil link data compression)-s         print final stats-S addr    set source address-t n       individual target initial timeout (in millisec) (default 500)-T n       ignored (for compatibility with fping 2.4)-u         show targets that are unreachable-v         show versiontargets    list of targets to check (if no -f specified)

1.检查www.baidu.com是否存在


2. 检查192.168.227.1/24主机是否存在:

将同时显示多个IP地址,它将显示状态为活动或无法访问

3.从文件中读取目标列表

我们创建了一个名为fping.txt的文件,其IP地址到fping

  1. 检查192.168.227.1到192.168.227.5之间的主机是否存在

shell脚本快速判断网段内主机存活数

ping 检测的时候会一般加上-c参数来指定请求次数,避免使用Ctrl+C结束命令执行

在这里插入图片描述

当要检测的主机较多时,直接使用ping命令搬砖有点力不从心,写个脚本方便点。

### Main ###
if [ $# -ne 1 ]; thenecho -e "Enter a single IPv4 addr, IPv4 range, or IPv4 addr with cidr.\n"echo "cmd example, bash liveSearcher.sh 172.16.0.0/16"echo "cmd example, bash liveSearcher.sh 192.18.1.0-1.254"    echo "cmd example, bash liveSearcher.sh 10.10.10.100"    elif [ $1 == "--help" -o  $1 == "-h" ]; thenecho -e "Enter a single IPv4 addr, IPv4 range, or IPv4 addr with cidr.\n"echo "cmd example, bash liveSearcher.sh 10.0.0.0/8"echo "cmd example, bash liveSearcher.sh 192.168.1.0-25.0.254"echo "cmd example, bash liveSearcher.sh 192.168.1.200"# single IPv4 ping
elif [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; thenecho -e "Scanning " $1 '\n'ping -c1 $1 2>/dev/null|egrep -i "bytes from " |cut -d" " -f4,6 echo -e "\nLive host search:  Complete"# range IPv4 sweep
elif [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\-[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\-[0-9]{1,3}\.[0-9]{1,3}$ ]] ||[[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\-[0-9]{1,3}$ ]] && [[ $RANGELENGTH -eq 0 && ${RANGECHECK[0]} -gt ${IPRCHECK[3]} ]] || [[ $RANGELENGTH -eq 1 && ${RANGECHECK[0]} -gt ${IPRCHECK[2]} ]] || [[ $RANGELENGTH -eq 2 && ${RANGECHECK[0]} -gt ${IPRCHECK[1]} ]] &&[[ ${RANGECHECK[0]} -le 255 && ${RANGECHECK[1]} -le 255 && ${RANGECHECK[2]} -le 255 ]] &&[[ ${IPRCHECK[0]} -le 255 && ${IPRCHECK[1]} -le 255 && ${IPRCHECK[2]} -le 255 && ${IPRCHECK[3]} -le 255 ]]; thenrange_sweep# subnet IPv4 sweep
elif [[ $CIDR =~ [0-9]{1,2} ]] && [ $CIDR -le 30 -a $CIDR -ge 8 ] && [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] &&[[ ${IPCHECK[0]} -le 255 && ${IPCHECK[1]} -le 255 && ${IPCHECK[2]} -le 255 && ${IPCHECK[3]} -le 255 ]]; thensubnet_sweepelseecho -e "\nerror in user input"exit 1
fi

运行结果:

在这里插入图片描述
fping命令代码实现

int main( int argc, char **argv )
{...if(uid = getuid()) {seteuid( getuid() );}...while( ( c = getopt( argc, argv, "gedhlmnqusaAvDz:t:H:i:p:f:r:c:b:C:Q:B:S:I:T:O:" ) ) != EOF ){switch( c ){case 't':if( !( timeout = ( unsigned int )atoi( optarg ) * 100 ) )usage(1);break;case 'r':retry = ( unsigned int )atoi( optarg );break;case 'i':if( !( interval = ( unsigned int )atoi( optarg ) * 100 ) )usage(1);break;case 'p':if( !( perhost_interval = ( unsigned int )atoi( optarg ) * 100 ) )usage(1);break;case 'c':if( !( count = ( unsigned int )atoi( optarg ) ) )usage(1);count_flag = 1;break;case 'C':if( !( count = ( unsigned int )atoi( optarg ) ) )usage(1);count_flag = 1;report_all_rtts_flag = 1;break;case 'b':errno = 0;ping_data_size = (unsigned int) strtol(optarg, (char **)NULL, 10);if( errno )usage(1);break;case 'h':usage(0);break;case 'q':verbose_flag = 0;quiet_flag = 1;break;case 'Q':verbose_flag = 0;quiet_flag = 1;if( !( report_interval = ( unsigned int )atoi( optarg ) * 100000 ) )usage(1);break;case 'e':elapsed_flag = 1;break;case 'm':multif_flag = 1;break;case 'd': case 'n':name_flag = 1;break;case 'A':addr_flag = 1;break;case 'B':if( !( backoff = atof( optarg ) ) )usage(1);break;case 's':stats_flag = 1;break;case 'D':timestamp_flag = 1;break;case 'l':loop_flag = 1;backoff_flag = 0;break;case 'u':unreachable_flag = 1;break;case 'a':alive_flag = 1;break;case 'H':  if( !( ttl = ( u_int )atoi( optarg ) ))usage(1);break;  case 'v':printf( "%s: Version %s\n", argv[0], VERSION);printf( "%s: comments to %s\n", argv[0], EMAIL );exit( 0 );case 'f': filename = optarg;generate_flag = 0;break;case 'g':generate_flag = 1;break;case 'S':
#ifndef IPV6if( ! inet_pton( AF_INET, optarg, &src_addr ) )
#elseif( ! inet_pton( AF_INET6, optarg, &src_addr ) )
#endifusage(1);src_addr_present = 1;break;case 'I':printf( "%s: cant bind to a particular net interface since SO_BINDTODEVICE is not supported on your os.\n", argv[0] );break;case 'T':break;case 'O':if (sscanf(optarg,"%i",&tos)){if ( setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) {perror("setting type of service octet IP_TOS");}}break;default:usage(1);break;}}...return 0;
}...
long timeval_diff( struct timeval *a, struct timeval *b )
{long sec_diff = a->tv_sec - b->tv_sec;if(sec_diff == 0) {return (a->tv_usec - b->tv_usec) / 10;}else if(sec_diff < 100) {return (sec_diff * 1000000 + a->tv_usec - b->tv_usec) / 10;}else {return sec_diff * 100000;}
}void timeval_add(struct timeval *a, long t_10u)
{t_10u *= 10;a->tv_sec += (t_10u + a->tv_usec) / 1000000;a->tv_usec = (t_10u + a->tv_usec) % 1000000;
}char * sprint_tm( int t )
{static char buf[10];if( t < 0 ) {/* negative (unexpected) */sprintf( buf, "%.2g", (double) t / 100 );}else if( t < 100 ) {/* <= 0.99 ms */sprintf( buf, "0.%02d", t );}else if( t < 1000 ){/* 1.00 - 9.99 ms */sprintf( buf, "%d.%02d", t / 100, t % 100 );}else if( t < 10000 ) {/* 10.0 - 99.9 ms */sprintf( buf, "%d.%d", t / 100, ( t % 100 ) / 10 );}else if( t < 100000000 ) {/* 100 - 1'000'000 ms */sprintf( buf, "%d", t / 100 );}else {sprintf( buf, "%.2e", (double) (t / 100) );}return buf ;
}

If you need the complete source code of fping, please add WeChat number (c17865354792)​

总结

fping是一个小型命令行工具,用于向网络主机发送ICMP(Internet控制消息协议)回显请求,类似于ping,但在ping多个主机时性能更高。fping与ping的完全不同之处在于,您可以在命令行上定义任意数量的主机,或者使用要ping的IP地址或主机列表指定文件。

Welcome to follow WeChat official account【程序猿编码


http://chatgpt.dhexx.cn/article/09GPuyK8.shtml

相关文章

DeepLabV1网络简析

原论文名称&#xff1a;Semantic Image Segmentation with Deep Convolutional Nets and Fully Connected CRFs 论文下载地址&#xff1a;https://arxiv.org/abs/1412.7062 参考源码&#xff1a;https://github.com/TheLegendAli/DeepLab-Context 讲解视频&#xff1a; https:…

DeepLab v3+原理和实现

这节课讲DeepLabv3模型&#xff0c;及前身DeepLabv3模型&#xff0c;两篇论文来自Google的同一个团队。 参考资料 DeepLabv3&#xff0c;被引1000 DeepLabv3&#xff0c;被引1000 Pytorch DeepLabv3实现&#xff0c;Star 1.5k 我们讲1.模型原理2.代码实现 from PIL import Im…

deeplabcut使用

cuda11.2和cudnn8.1安装 win 最新的 Win11/WIN10 安装CUDA11.2和cuDNNlinux ubuntu 16.04 安装 cuda11.2 和cudnn8.2.1 dlc安装 创建虚拟环境 安装deeplabcut2.2.3 tensorflow2.11.0 wxPython4.0.4 pip install deeplabcut2.2.3 deeplabcut里面包含了tensorflow的不用再安…

MATLAB与深度学习(一)— Deep Learning Toolbox

MATLAB与深度学习&#xff08;一&#xff09;— Deep Learning Toolbox 最近&#xff0c;我在学习基于matlab的深度学习的内容&#xff0c;并整理出如下学习笔记。本文借鉴和引用了网上许多前辈的经验和代码&#xff0c;如有冒犯&#xff0c;请及时与我联系。 1. MATLAB与深度…

Deeplab V1 和 V2讲解

Deeplab V1 Background&#xff1a; CNN的一个特性是invariance&#xff08;不变性&#xff09;&#xff0c;这个特性使得它在high-level的计算机视觉任务比如classification中&#xff0c;取得很好的效果。但是在semantic segmentation任务中&#xff0c;这个特性反而是个障…

deeplab v3+ 源码详解

训练模型&#xff1a; 下载好voc数据集&#xff0c;并传入所需的参数即可进行训练。 参数配置&#xff1a; """ 训练&#xff1a; --model deeplabv3plus_mobilenet --gpu_id 0 --year 2012_aug --crop_val --lr 0.01 --crop_size 513 --batch_size 4 --…

Deeplabcut教程(二)使用

因为很久没用这个了所以就一直没更使用教程&#xff0c;写的安装教程收到好几条私信要使用教程&#xff0c;这几天在帮一个朋友跑这个&#xff0c;于是就有了这个使用教程 安装教程&#xff1a;Deeplabcut教程&#xff08;一&#xff09;安装&#xff08;GPU&CPU版本&…

概述DeepLab系列(deeplab v1, deeplab v2, deeplab v3, deeplab v3+)

前言&#xff1a;图像分割是指像素级别的图像识别&#xff0c;即标注出图像中每个像素所属的对象类别。 语义分割更注重类别之间的区分&#xff0c;而实例分割更注重个体之间的区别。 DeepLab是由Google团队提出的一系列图像分割算法。 DeepLab v1 &#xff08;2014年&#xf…

DeepLab系列理解

原文Blog&#xff1a;https://zhuanlan.zhihu.com/p/61208558 1、deeplab v1 针对标准的深度卷积神经网络的两个主要问题&#xff1a;1.Striding操作使得输出尺寸减小&#xff1b; 2.Pooling对输入小变化的不变性&#xff0c;v1 使用空洞卷积(atrous)条件随机场(CRFs)来解决这…

Deep Lab 系列总结

Deep Lab v1 结合了深度卷积神经网络&#xff08;DCNNs&#xff09;和概率图模型&#xff08;Dense CRFs&#xff09;的方法 问题1&#xff1a;DCNN s做语义分割时精准度不够&#xff0c;根本原因是DCNNs的高级特征的平移不变性&#xff0c;即高层次特征映射&#xff0c;根…

DeepLab V3+:DeepLab系列的极致?

这篇文章提交在arXiv上的&#xff0c;对应代码也已经开源。 理解DeepLab V3的构架首先需要理解DeepLab V3&#xff08;可以参考博主的前一篇博客&#xff09;&#xff0c;V3基本上可以理解成在原始的基础上增加了encoder-decoder模块&#xff0c;进一步保护物体的边缘细节信息…

【Deep Learning】DeepLab

【论文】SEMANTIC IMAGE SEGMENTATION WITH DEEP CONVOLUTIONAL NETS AND FULLY CONNECTED CRFS 前段时间学习了DeepLab&#xff0c;这里花时间记录一下&#xff0c;感谢几位小伙伴的分享。DeepLab的主体结构事实上是参照VGG改造的&#xff0c;它的几个优点&#xff1a;首先是速…

Deeplab笔记

一、Deeplab v2 对应论文是 DeepLab: Semantic Image Segmentiation with Deep Convolutional Nets, Atrous Convolution, and Fully Connected CRFs Deeplab 是谷歌在FCN的基础上搞出来的。FCN为了得到一个更加dense的score map&#xff0c;将一张500x500的输入图像&#…

deeplab-v3+原理详解

入门小菜鸟&#xff0c;希望像做笔记记录自己学的东西&#xff0c;也希望能帮助到同样入门的人&#xff0c;更希望大佬们帮忙纠错啦~侵权立删。 目录 一、deeplab-v3提出原因与简单介绍 二、deeplab-v3网络结构图 三、Encoder 1、Backbone&#xff08;主干网络&#xff09…

深度学习 | MATLAB Deep Learning Toolbox Deeper Networks 创建

深度学习 | MATLAB Deep Learning Toolbox Deeper Networks 目录 深度学习 | MATLAB Deep Learning Toolbox Deeper NetworksDeeper Networks创建类比深度网络深度记忆原理深度学习层输入层卷积和全连接层序列层激活层归一化、丢弃和裁剪层池化和去池化层组合层输出层 参考资料…

深度学习(11)——DeepLab v1

DeepLab v1 DeepLab 由谷歌团队提出的&#xff0c;至今有了四个版本&#xff0c;也就是v1-v4。其结合了深度卷积神经网络&#xff08;DCNNs&#xff09;和概率图模型。 在论文《Semantic image segmentation with deep convolutional nets and fully connected CRFs》中提出&…

改进 DeepLabV3+

网络整体结构图 CFF结构图 import torch import torch.nn as nn import torch.nn.functional as F from nets.xception import xception from nets.mobilenetv2 import mobilenetv2class MobileNetV2(nn.Module):def __init__(self, downsample_factor8, pretrainedTrue):supe…

DeepFaceLab

DeepFaceLab从半脸&#xff08;Half Face&#xff09;到全脸&#xff08;Full Face&#xff09;再到整脸&#xff08;Whole Face&#xff09;&#xff0c;脸部替换的区域愈来愈大&#xff0c;适用的范围也越来越广&#xff0c;效果也越来越震撼。当然很多人已经不满足与单纯换脸…

DeepLab系列总结

DeepLab系列总结 DeepLab系列DeepLab V1DeepLab V2DeepLab V3DeepLab V3 DeepLab系列 DeepLab网络是由Liang-Chieh Chen&#xff08;下文以大神指代&#xff09;与google团队提出来的&#xff0c;是一个专门用来处理语义分割的模型。目前推出了4个&#xff08;或者说3.5个&…

DeepLab系列学习

DeepLab系列 文章目录 DeepLab系列DeepLabV1简介atrous algorithm利用全卷积增加感受野并加速运算条件随机场CRF实验结果多尺度预测VOC数据集上对比 DeepLabV2主要改进简介模型主体ASPP实验结果 DeepLabV3相应的改进实验 DeepLabV3(DeepLabV3 plus)相应改进整体结构解码器结构m…