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

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

ping命令说明

  1. PING() System Manager's Manual: iputils PING(8)
  2.  
  3. NAME
  4. ping, ping6 - send ICMP ECHO_REQUEST to network hosts
  5.  
  6. SYNOPSIS
  7. ping [-LRUbdfnqrvVaAB] [-c count] [-m mark] [-i interval] [-l preload]
  8. [-p pattern] [-s packetsize] [-t ttl] [-w deadline] [-F flowlabel] [-I
  9. interface] [-M hint] [-N nioption] [-Q tos] [-S sndbuf] [-T timestamp
  10. option] [-W timeout] [hop ...] destination
  11.  
  12. ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面>][-l<前置载入>]
  13. [-p<范本样式>][-s<数据包大小>][-t<存活数值>][主机名称或IP地址]
  14.  
  15. DESCRIPTION
  16. ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit
  17. an ICMP ECHO_RESPONSE from a host or gateway. ECHO_REQUEST datagrams
  18. (``pings'') have an IP and ICMP header, followed by a struct timeval
  19. and then an arbitrary number of ``pad'' bytes used to fill out the
  20. packet.
  21.  
  22. ping6 can also send Node Information Queries (RFC4620).
  23.  
  24. OPTIONS
  25. -a Audible ping.
  26.  
  27. -b Allow pinging a broadcast address.允许ping一个广播地址
  28.  
  29. -B Do not allow ping to change source address of probes. The
  30. address is bound to one selected when ping starts.
  31.  
  32. -m mark
  33. use mark to tag the packets going out. This is useful for vari
  34. ety of reasons within the kernel such as using policy routing to
  35. select specific outbound processing.
  36.  
  37. -c count
  38. Stop after sending count ECHO_REQUEST packets. With deadline
  39. option, ping waits for count ECHO_REPLY packets, until the time
  40. out expires.打印语句
  41.  
  42. -d Set the SO_DEBUG option on the socket being used. Essentially,
  43. this socket option is not used by Linux kernel.
  44.  
  45. -F flow label
  46. Allocate and bit flow label on echo request packets.
  47. (Only ping6). If value is zero, kernel allocates random flow
  48. label.
  49.  
  50. -f Flood ping. For every ECHO_REQUEST sent a period ``.'' is
  51. printed, while for ever ECHO_REPLY received a backspace is
  52. printed. This provides a rapid display of how many packets are
  53. being dropped. If interval is not given, it sets interval to
  54. zero and outputs packets as fast as they come back or one hun
  55. dred times per second, whichever is more. Only the super-user
  56. may use this option with zero interval.
  57.  
  58. -i interval
  59. Wait interval seconds between sending each packet. The default
  60. is to wait for one second between each packet normally, or not
  61. to wait in flood mode. Only super-user may set interval to val
  62. ues less 0.2 seconds.
  63.  
  64. -I interface address
  65. Set source address to specified interface address. Argument may
  66. be numeric IP address or name of device. When pinging IPv6 link-
  67. local address this option is required.
  68.  
  69. -l preload
  70. If preload is specified, ping sends that many packets not wait
  71. ing for reply. Only the super-user may select preload more than
  72. .
  73.  
  74. -L Suppress loopback of multicast packets. This flag only applies
  75. if the ping destination is a multicast address.
  76.  
  77. -n Numeric output only. No attempt will be made to lookup symbolic
  78. names for host addresses.
  79.  
  80. -p pattern
  81. You may specify up to ``pad'' bytes to fill out the packet
  82. you send. This is useful for diagnosing data-dependent problems
  83. in a network. For example, -p ff will cause the sent packet to
  84. be filled with all ones.
  85.  
  86. -D Print timestamp (unix time + microseconds as in gettimeofday)
  87. before each line.
  88.  
  89. -q Quiet output. Nothing is displayed except the summary lines at
  90. startup time and when finished.
  91.  
  92. -R Record route. Includes the RECORD_ROUTE option in the
  93. ECHO_REQUEST packet and displays the route buffer on returned
  94. packets. Note that the IP header is only large enough for nine
  95. such routes. Many hosts ignore or discard this option.
  96.  
  97. -r Bypass the normal routing tables and send directly to a host on
  98. an attached interface. If the host is not on a directly-
  99. attached network, an error is returned. This option can be used
  100. to ping a local host through an interface that has no route
  101. through it provided the option -I is also used.
  102.  
  103. -s packetsize
  104. Specifies the number of data bytes to be sent. The default is
  105. , which translates into ICMP data bytes when combined with
  106. the bytes of ICMP header data.
  107.  
  108. -S sndbuf
  109. Set socket sndbuf. If not specified, it is selected to buffer
  110. not more than one packet.
  111.  
  112. -t ttl Set the IP Time to Live.
  113.  
  114. -T timestamp option
  115. Set special IP timestamp options. timestamp option may be
  116. either tsonly (only timestamps), tsandaddr (timestamps and
  117. addresses) or tsprespec host1 [host2 [host3 [host4]]] (timestamp
  118. prespecified hops).
  119.  
  120. -U Print full user-to-user latency (the old behaviour). Normally
  121. ping prints network round trip time, which can be different f.e.
  122. due to DNS failures.
  123.  
  124. -v Verbose output.
  125.  
  126. -V Show version and exit.
  127.  
  128. -w deadline
  129. Specify a timeout, in seconds, before ping exits regardless of
  130. how many packets have been sent or received. In this case ping
  131. does not stop after count packet are sent, it waits either for
  132. deadline expire or until count probes are answered or for some
  133. error notification from network.
  134.  
  135. -W timeout
  136. Time to wait for a response, in seconds. The option affects only
  137. timeout in absense of any responses, otherwise ping waits for
  138. two RTTs.

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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <error.h>
  6.  
  7. #define error_exit(_errmsg) error(EXIT_FAILURE , errno, _errmsg)
  8.  
  9. int ping_ip(const char * ip)
  10. {
  11.  
  12. FILE * fp = NULL;
  13. ] = {};
  14. ;
  15. char * pstr = NULL;
  16. ] = {};
  17. sprintf(_ip, "ping -c 1 %s > ./ping.txt",ip);
  18. system("sudo touch ./ping.txt");
  19. system("chmod 777 ./ping.txt");
  20. system(_ip);
  21.  
  22. fp = fopen("./ping.txt","r");
  23. if(fp == NULL)
  24. {
  25. error_exit("fopen");
  26. }
  27.  
  28. , fp) > )
  29. {
  30. line++;
  31. )
  32. {
  33. if((pstr = strstr(buffer, "64 bytes from")) != NULL)
  34. {
  35. while(strstr(pstr, "ttl=64"))
  36. {
  37. printf("net is ok\n");
  38. ;
  39. }
  40. }
  41. else
  42. {
  43. printf("net is error\n");
  44. ;
  45. }
  46. }
  47. memset(buffer, ,);
  48. }
  49. ;
  50. }
  51.  
  52. int main(int argc, const char *argv[])
  53. {
  54. ping_ip("192.168.123.24");
  55. ;
  56. }

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. 【Cf #502 H】The Films(莫队)

    题面的简述:总共有$m$种书,书架上共有$n$本书,给出$n$本书的种类,并有$Q$个询问,每次询问给出$l, r, k$.每次询问时都会先出现$k * m$本书,每种书各$k$本,然后再加入书架上的 ...

  2. 【bzoj1095】 ZJOI2007—捉迷藏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1095 (题目链接) 题意 一棵树,求最远的两黑点之间的距离,每次可以将黑点染白或者将白点染黑. So ...

  3. CVE-2018-1111劫持dhcp造成centos代码执行漏洞

    0x01 漏洞概述 近日,红帽官方发布了安全更新,修复了编号为CVE-2018-1111的远程代码执行漏洞,攻击者可以通过伪造DHCP服务器发送响应包,攻击红帽系统,获取root权限并执行任意命令. ...

  4. C/C++ 多继承{虚基类,虚继承,构造顺序,析构顺序}

    C/C++:一个基类继承和多个基类继承的区别 1.对多个基类继承会出现类之间嵌套时出现的同名问题,如果同名变量或者函数出现不在同一层次,则底层派生隐藏外层比如继承基类的同名变量和函数,不会出现二义性, ...

  5. WEB下面路径的问题

    web 中的  /  到底代表什么? 绝对路径-以Web站点根目录为参考基础的目录路径.之所以称为绝对,意指当所有网页引用同一个文件时,所使用的路径都是一样的.相对路径-以引用文件之网页所在位置为参考 ...

  6. 语法:c++对关于空指针0/NULL/nullptr三者的演变

    来源: https://blog.csdn.net/u010558281/article/details/77793644 字面意义上的解释: 0:整型常量 NULL:预处理符号 nullptr:空指 ...

  7. R kernel for Jupyter Notebook 支持r

    1/2) Installing via supplied binary packages(default on Windows + Mac OS X) You can install all pack ...

  8. java反射机制的理解

    反射机制是什么概念?大多都有介绍,指的是程序在运行状态中,能够加载一个只有类名的类,加载完之后会在堆上产生一个Class对象.通过这个 Class对象可以获得类的属性.方法和其他类信息.之前对反射的应 ...

  9. android studio run 的时候,报the apk file does not exist on disk,

    1.首先 clean rebuild,重启,不能解决的话,再找到这个 然后是这里: 不用填,点ok,ok即可,他喵的,卡我俩小时

  10. Rolling in the Deep (Learning)

    Rolling in the Deep (Learning) Deep Learning has been getting a lot of press lately, and is one of t ...