实现ping功能,就肯定要用到ping命令,那么在Linux下ping命令为:

ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面>][-l<前置载入>][-p<范本样式>][-s<数据包大小>][-t<存活数值>][主机名称或IP地址]

ping命令说明

PING()                System Manager's Manual: iputils                PING(8)

NAME
       ping, ping6 - send ICMP ECHO_REQUEST to network hosts

SYNOPSIS
       ping  [-LRUbdfnqrvVaAB] [-c count] [-m mark] [-i interval] [-l preload]
       [-p pattern] [-s packetsize] [-t ttl] [-w deadline] [-F flowlabel]  [-I
       interface]  [-M  hint] [-N nioption] [-Q tos] [-S sndbuf] [-T timestamp
       option] [-W timeout] [hop ...] destination

       ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面>][-l<前置载入>]
       [-p<范本样式>][-s<数据包大小>][-t<存活数值>][主机名称或IP地址] 

DESCRIPTION
       ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit
       an  ICMP  ECHO_RESPONSE from a host or gateway.  ECHO_REQUEST datagrams
       (``pings'') have an IP and ICMP header, followed by  a  struct  timeval
       and  then  an  arbitrary  number  of ``pad'' bytes used to fill out the
       packet.

       ping6 can also send Node Information Queries (RFC4620).

OPTIONS
       -a     Audible ping.

       -b     Allow pinging a broadcast address.允许ping一个广播地址

       -B     Do not allow ping to  change  source  address  of  probes.   The
              address is bound to one selected when ping starts.

       -m mark
              use  mark to tag the packets going out. This is useful for vari‐
              ety of reasons within the kernel such as using policy routing to
              select specific outbound processing.

       -c count
              Stop  after  sending  count  ECHO_REQUEST packets. With deadline
              option, ping waits for count ECHO_REPLY packets, until the time‐
              out expires.打印语句

       -d     Set  the SO_DEBUG option on the socket being used.  Essentially,
              this socket option is not used by Linux kernel.

       -F flow label
              Allocate and  bit flow  label  on  echo  request  packets.
              (Only  ping6).  If  value  is zero, kernel allocates random flow
              label.

       -f     Flood ping. For  every  ECHO_REQUEST  sent  a  period  ``.''  is
              printed,  while  for  ever  ECHO_REPLY  received  a backspace is
              printed.  This provides a rapid display of how many packets  are
              being  dropped.   If  interval is not given, it sets interval to
              zero and outputs packets as fast as they come back or  one  hun‐
              dred  times  per second, whichever is more.  Only the super-user
              may use this option with zero interval.

       -i interval
              Wait interval seconds between sending each packet.  The  default
              is  to  wait for one second between each packet normally, or not
              to wait in flood mode. Only super-user may set interval to  val‐
              ues less 0.2 seconds.

       -I interface address
              Set  source address to specified interface address. Argument may
              be numeric IP address or name of device. When pinging IPv6 link-
              local address this option is required.

       -l preload
              If  preload is specified, ping sends that many packets not wait‐
              ing for reply.  Only the super-user may select preload more than
              .

       -L     Suppress  loopback of multicast packets.  This flag only applies
              if the ping destination is a multicast address.

       -n     Numeric output only.  No attempt will be made to lookup symbolic
              names for host addresses.

       -p pattern
              You may specify up to  ``pad'' bytes to fill  out  the  packet
              you send.  This is useful for diagnosing data-dependent problems
              in a network.  For example, -p ff will cause the sent packet  to
              be filled with all ones.

       -D     Print  timestamp  (unix  time + microseconds as in gettimeofday)
              before each line.

       -q     Quiet output.  Nothing is displayed except the summary lines  at
              startup time and when finished.

       -R     Record   route.    Includes   the  RECORD_ROUTE  option  in  the
              ECHO_REQUEST packet and displays the route  buffer  on  returned
              packets.   Note that the IP header is only large enough for nine
              such routes.  Many hosts ignore or discard this option.

       -r     Bypass the normal routing tables and send directly to a host  on
              an  attached  interface.   If  the  host  is  not on a directly-
              attached network, an error is returned.  This option can be used
              to  ping  a  local  host  through an interface that has no route
              through it provided the option -I is also used.

       -s packetsize
              Specifies the number of data bytes to be sent.  The  default  is
              ,  which translates into  ICMP data bytes when combined with
              the  bytes of ICMP header data.

       -S sndbuf
              Set socket sndbuf. If not specified, it is  selected  to  buffer
              not more than one packet.

       -t ttl Set the IP Time to Live.

       -T timestamp option
              Set  special  IP  timestamp  options.   timestamp  option may be
              either  tsonly  (only  timestamps),  tsandaddr  (timestamps  and
              addresses) or tsprespec host1 [host2 [host3 [host4]]] (timestamp
              prespecified hops).

       -U     Print  full  user-to-user  latency (the old behaviour). Normally
              ping prints network round trip time, which can be different f.e.
              due to DNS failures.

       -v     Verbose output.

       -V     Show version and exit.

       -w deadline
              Specify  a  timeout, in seconds, before ping exits regardless of
              how many packets have been sent or received. In this  case  ping
              does  not  stop after count packet are sent, it waits either for
              deadline expire or until count probes are answered or  for  some
              error notification from network.

       -W timeout
              Time to wait for a response, in seconds. The option affects only
              timeout in absense of any responses, otherwise  ping  waits  for
              two RTTs.

那么用C语言实现ping过程如下:

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <error.h>

 #define error_exit(_errmsg) error(EXIT_FAILURE , errno, _errmsg)

 int ping_ip(const char * ip)
 {

     FILE * fp = NULL;
     ] = {};
     ;
     char * pstr = NULL;
     ] = {};
     sprintf(_ip, "ping -c 1 %s > ./ping.txt",ip);
     system("sudo touch ./ping.txt");
     system("chmod 777 ./ping.txt");
     system(_ip);

     fp = fopen("./ping.txt","r");
     if(fp == NULL)
     {
         error_exit("fopen");
     }

     , fp) > )
     {
         line++;
         )
         {
             if((pstr = strstr(buffer, "64 bytes from")) != NULL)
             {
                 while(strstr(pstr, "ttl=64"))
                 {
                     printf("net is ok\n");
                     ;
                 }
             }
             else
             {
                 printf("net is error\n");
                 ;
             }
         }
         memset(buffer,  ,);
     }
     ;
 }

 int main(int argc, const char *argv[])
 {
     ping_ip("192.168.123.24");
     ;
 }

Linux下实现ping功能的更多相关文章

  1. Windows 和  Linux 下 禁止ping的方法

    Windows 和Linux 下 禁止ping的方法 目的: 禁止网络上的其他主机或服务器ping自己的服务器 运行环境: Windows 03.08  linux 方法: Windows 03下: ...

  2. 实现Linux下dc的功能,计算后缀表达式的值

    提交测试截图和码云练习项目链接,实现Linux下dc的功能,计算后缀表达式的值 -将运算符写在两个操作数之后的表达式称为"后缀表达式",如上面的中缀表达式可转换为后缀表达式1 2 ...

  3. 如何开启和禁止Linux系统的ping功能

    在日常的网络维护和使用过程中,ping命令是最为常用的一个检测命令,它所使用的是ICMP协议,但是为了保护主机,很多时候我们需要禁止ICMP协议,在这种情况下,终端再使用ping命令检测,服务器是不会 ...

  4. linux下的ping工具--fping

    前言:       如果想ping 大量的不连续的地址,有想知道放回结果,在linux系统下有一个非常合适的工具--fping. Fping 官网:http://www.fping.org/ Fpin ...

  5. Linux下使用ping快速检测存活主机

    该shell脚本主要是通过ping来批量检测网段内存活主机,执行结果保存在脚本当前目录下的IPinfor目录中,写的比较匆忙,希望大家留下更好的建议. #!/usr/bin/env bash##### ...

  6. Linux下针对路由功能配置iptables的方法详解

    作为公司上网的路由器需要实现的功能有nat地址转换.dhcp.dns缓存.流量控制.应用程序控制,nat地址转换通过iptables可以直 接实现,dhcp服务需要安装dhcpd,dns缓存功能需要使 ...

  7. linux下能ping ip不能ping域名详解

    今天在开发的同事来说,内网不能通过域名访问自己的服务器!然后做了下面的测试发现这样的问题: [root@itmop ~]# ping www.downcc.com ping: unknown host ...

  8. Linux C 实现Ping功能的程序.

    ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具.ping命令的工作原理是:向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文一模一样地传回给发送者,这 ...

  9. Linux下可以ping ip地址但无法ping域名解决方法

    分析:当前系统无法解决域名至ip地址故障. 步骤阅读 2 三:解决过程: 1.分析dns故障: 2.物理机可以ping 地址,但无法ping域名: 3.检查/etc/resolv.conf: 注: ( ...

随机推荐

  1. 【BZOJ2830/洛谷3830】随机树(动态规划)

    [BZOJ2830/洛谷3830]随机树(动态规划) 题面 洛谷 题解 先考虑第一问. 第一问的答案显然就是所有情况下所有点的深度的平均数. 考虑新加入的两个点,一定会删去某个叶子,然后新加入两个深度 ...

  2. Java之IO流(字节流,字符流)

    IO流和Properties IO流 IO流是指计算机与外部世界或者一个程序与计算机的其余部分的之间的接口.它对于任何计算机系统都非常关键, 因而所有 I/O 的主体实际上是内置在操作系统中的.单独的 ...

  3. All flavors must now belong to a named flavor dimension

    FAQ: All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/ ...

  4. 解题:APIO 2014 序列分割

    题面 拆开式子我们发现切割顺序不影响答案,所以可以设计出一个$dp[i][j]$表示到$i$为止切了$j$刀的最大收益之类的,然后做个前缀和就可以转移了. $dp[i][j]=min(dp[i][j] ...

  5. Luogu 1220 关路灯(动态规划)

    Luogu 1220 关路灯(动态规划) Description 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项 ...

  6. jsp中的js中获取项目路径的方法

    在jsp中加上 <% String path = request.getContextPath(); String basePath = request.getScheme()+":/ ...

  7. 图像处理之CSC色彩转换

    1 YUV域介绍 根据三基色原理,任意一种色光F都可以用不同分量的R.G.B三色相加混合而成,即F = r [ R ] + g [ G ] + b [ B ],其中r.g.b分别为三基色参与混合的系数 ...

  8. 2.Helloworld

    1.对于Qt程序来说,main()函数一般以创建application对象(gui是QApplication,非gui程序是QCoreApplication.QApplication实际上是QCore ...

  9. Java入门系列:处理Json格式数据

    本节主要讲解: 1)json格式数据处理方法 2)第三方工具包的使用方法 3)java集合数据类型 [项目任务] 编写一个程序,显示未来的天气信息. [知识点解析] 为了方便后面代码的分析,先需要掌握 ...

  10. P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 一只鸟从1跳到n.从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制k,求每次最少耗费多少体力 很简短的题目哼. ...