一篇文章讲明白Linux下的ping命令用法与实现

简介: 一篇文章讲明白Linux下的ping命令用法与实现

ping 命令用于查看网络上的主机是否在工作,它向该主机发送ICMPECHO_REQUEST 包。有时我们想从网络上的某台主机上下载文件,可是又不知道那台主机是否开着,就需要使用ping 命令查看。

ping命令的一般格式为:

ping 【-dfnqrRv】【-c 发送次数】【-i 间隔秒数】【-I 网络界面】【-l 前置载入】【-p 范本样式】【-s 数据包大小】【-t 存活数值】【主机名或IP地址】

参数说明:

-d 使用Socket的SO_DEBUG功能。

-f 极限检测。大量且快速地送网络封包给一台机器,看它的回应。

-n 只输出数值。

-q 不显示任何传送封包的信息,只显示最后的结果。

-r 忽略普通的Routing Table,直接将数据包送到远端主机上。通常是查看本机的网络接口是否有问题。

-R 记录路由过程。

-v 详细显示指令的执行过程。

-c 数目 在发送指定数目的包后停止。

-i 秒数 设定间隔几秒送一个网络封包给一台机器,预设值是一秒送一次。

-I 网络界面 使用指定的网络界面送出数据包。

-l 前置载入 设置在送出要求信息之前,先行发出的数据包。

-p 范本样式 设置填满数据包的范本样式。

-s 字节数 指定发送的数据字节数,预设值是56,加上8字节的ICMP头,一共是64ICMP数据字节。

-t 存活数值 设置存活数值TTL的大小。

:linux下的ping和windows下的ping稍有区别,linux下ping不会自动终止,需要按ctrl+c终止或者用参数-c指定要求完成的回应次数

linux下测试本机与目标主机连通性的命令是ping,这里主要讲解两个参数 –c 与 – i

其中 –c count 次数,也就是ping的次数

-i interval 间隔 ,每次ping之间的时间空格

介绍一下ping原理

ping程序实现

@头文件common.h定义,包含程序中用到的头文件及公共变量、宏、常量、静态变量定义

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define IP_HEAD_LEN 20

#define ICMP_LEN 8

#define BUFFER_SIZE 50 1024

/

原始套接字的描述符,由于需要在信号处理器

和主程序中共享,所以定义为外部变量(全局)

/

int ip_fd;

/进程号/

int p_id;

/packet_len为IP包头和ICMP包头长度之和/

extern int packet_len;

/对端地址/

struct sockaddr_in send_addr;

/发送应用缓冲区/

char send_buf【1024】;

/报文序列号/

extern int sequence;

/主机名指针/

struct hostent host;

/标识是否已经接收到回文/

int flag;

@主函数main.c定义

#include "common.h"

main(int argc, char *argv)

{

/命令是ping host(主机名)|ip_address(ip地址)/

if(argc != 2)

{

/命令不正确/

fprintf(stderr, "usage: ping .n");

exit(1);

}

/创建使用ICMP的原始套接字,这种套接字只有root才能生成/

ip_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

if(ip_fd < 0)

{

fprintf(stderr, "raw socket error.n");

exit(1);

}

/改变进程的用户ID, 回收root权限,设置当前用户权限/

setuid(getpid());

ping(argv【1】);

}

@ping框架的建立ping.c用于初始化ping相关信息及端口信息

#include "common.h"

/

handle_alarm用于定时发送IP数据包

/

void handle_alarm(int signo)

{

send_ip();

alarm(1);

}

ping(char argv)

{

struct sigaction act;

act.sa_handler = handle_alarm;

act.sa_flags = 0;

sigemptyset(act.sa_mask);

sigaction(SIGALRM, act, NULL);

/获取main的进程id,用于设置ICMP的标志符/

p_id = getpid();

/扩大套接字接收缓冲区到50K这样做主要为了减小接收缓冲区溢出的的可能性,若无意中ping一个广播地址或多播地址,将会引来大量应答/

//setsockopt(ip_fd, SOL_SOCKET, SO_RCVBUF, BUFFER_SIZE, sizeof(BUFFER_SIZE));

/只须填写地址信息,不需要指定端口信息,因为原始套接字在传输层之下/

send_addr.sin_family = AF_INET;

/判断是主机名还是ip地址/

if(inet_addr(argv) == INADDR_NONE)

{

/是主机名/

if((host = gethostbyname(argv)) == NULL)

{

/主机名错误/

perror("get host by name error: unknow host.");

exit(1);

}

memcpy(send_addr.sin_addr, host->h_addr, host->h_length);

}

else

{

/是ip地址/

inet_aton(argv, send_addr.sin_addr);

}

printf("ping %s(%s) %d(%d) bytes of datan", argv,

inet_ntoa(send_addr.sin_addr), sizeof(struct timeval),

sizeof(struct timeval) + IP_HEAD_LEN + ICMP_LEN);

flag = 0;

/触发一个SIGALRM信号/

raise(SIGALRM);

recv_ip();

}

@发送报文send.c,建立ICMP报文并打包为IP数据包,发送

#include "common.h"

int sequence = 0;

int packet_len = IP_HEAD_LEN + ICMP_LEN;

/

send_ip用于发送包含ICMP报文的IP数据包

/

send_ip(void)

{

if(sequence != 0 !flag)

{

printf("Destination Host Unreachablen");

}

int len;

struct icmphdr icmp_p;

icmp_p = (struct icmphdr )send_buf;

/填写ICMP报文类型/

icmp_p->type = ICMP_ECHO;

/填写ICMP报文的代码/

icmp_p->code = 0;

/填写ICMP报文的标识符/

(icmp_p->un).echo.id = p_id;

/填写ICMP报文的序号,并增加ICMP的序号/

(icmp_p->un).echo.sequence = sequence ++;

/记录发送时间/

gettimeofday((struct timeval)(icmp_p + 1), NULL);

/printf("%dn", sizeof(struct icmphdr));

printf("type: %dncode: %dnchecksum: %dnun.echo.id: %dnun.echo.sequence: %dnun.gateway: %dnun.frag.unused:%d nun.frag.mtu: %dnn", icmp_p->type, icmp_p->code, icmp_p->checksum, (icmp_p->un).echo.id, (icmp_p- >un).echo.sequence, (icmp_p->un).gateway, (icmp_p->un).frag.unused, (icmp_p->un).frag.mtu);

printf("type: %dncode: %dnchecksum: %dnun.echo.id: %dnun.echo.sequence: %dnun.gateway: %dnun.frag.unused: %d nun.frag.mtu: %dnn", sizeof(icmp_p->type), sizeof(icmp_p->code), sizeof(icmp_p->checksum), sizeof((icmp_p- >un).echo.id), sizeof((icmp_p->un).echo.sequence), sizeof((icmp_p->un).gateway), sizeof((icmp_p->un).frag.unused), sizeof((icmp_p->un).frag.mtu));/

/报文的长度等于IP包长度加上ICMP报文长度和数据长度/

len = sizeof(struct timeval) + packet_len;

/使用IP计算IP包头校验和的计算方法来计算ICMP的校验和/

icmp_p->checksum = 0;

icmp_p->checksum = ip_checksum((u_short )icmp_p, len);

/发送IP数据包/

if(sendto(ip_fd, send_buf, len, 0, (struct sockaddr)send_addr, sizeof(send_addr)) < 0)

{

fprintf(stderr, "send to error.n");

}

}

@数据校验check_sum.c实现网络数据的校验工作

#include "common.h"

unsigned short ip_checksum(unsigned short pcheck, int check_len)

{

int nleft = check_len;

int sum = 0;

unsigned short p = pcheck;

//代码效果参考:http://www.zidongmutanji.com/bxxx/546600.html

unsigned short result = 0;

while(nleft > 1)

{

sum = sum + p ++;

nleft -= 2;

}

if(nleft == 1)

{

(unsigned char )(result) = (unsigned char )p;

sum += result;

}

sum = (sum ] 16) + (sum 0xFFFF);

sum += (sum ] 16);

result = sum;

return result;

}

@数据包接收receive.c,接收IP报文并分析,打印相关信息

#include "common.h"

/

recv_ip用于接受包含ICMP报文的IP数据包

/

recv_ip(void)

{

char recv_buf【1024】;

int len;

int n;

struct ip ip_p;

struct timeval time_now, time_send;

struct timeval now;

int iphead_len;

int icmp_len;

struct icmphdr icmp_p;

float delay;

while(1)

{

n = recvfrom(ip_fd, recv_buf, sizeof(recv_buf), 0, NULL, NULL);

if(n < 0)

{

if(errno = EINTR)

//代码效果参考:http://www.zidongmutanji.com/zsjx/46929.html

continue;

else

{

printf("recvfrom error.n");

continue;

}

}

ip_p = (struct ip)recv_buf;

/获取IP包头长度/

iphead_len = ip_p->ip_hl[2;

/获取IP数据包中包含的ICMP报文/

icmp_p = (struct icmphdr )(recv_buf + iphead_len);

/计算ICMP报文长度,它等于接受到的长度减去IP包头的长度/

icmp_len = n - iphead_len;

if(icmp_len < 8)

{

fprintf(stderr, "error icmp len = %d.n", icmp_len);

}

/如果ICMP类型相同则输出显示*/

if(icmp_p->type == ICMP_ECHOREPLY)

{

if((icmp_p->un).echo.id != p_id)

return;

if(icmp_len < 16)

printf("icmplen = %d.n", icmp_len);

<p style="widows: 2; text-trans

相关文章
|
1月前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
73 24
Linux系统之whereis命令的基本使用
|
5天前
|
Linux
Linux od命令
本文详细介绍了Linux中的 `od`命令,包括其基本语法、常用选项和示例。通过这些内容,你可以灵活地使用 `od`命令查看文件内容,提高分析和调试效率。确保理解每一个选项和示例的实现细节,应用到实际工作中时能有效地处理各种文件查看需求。
41 19
|
16天前
|
缓存 Ubuntu Linux
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
98 25
|
14天前
|
缓存 Linux
Linux查看内存命令
1. free free命令是最常用的查看内存使用情况的命令。它显示系统的总内存、已使用内存、空闲内存和交换内存的总量。 free -h • -h 选项:以易读的格式(如GB、MB)显示内存大小。 输出示例: total used free shared buff/cache available Mem: 15Gi 4.7Gi 4.1Gi 288Mi 6.6Gi 9.9Gi Swap: 2.0Gi 0B 2.0Gi • to
27 2
|
2月前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
53 11
|
4月前
|
安全 网络协议 Linux
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。通过掌握 ping 命令,读者可以轻松测试网络连通性、诊断网络问题并提升网络管理能力。
332 3
|
10月前
|
安全 网络协议 Linux
【专栏】Linux系统中ping命令的使用,包括其基本语法、输出信息、常用参数及高级用法
【4月更文挑战第28天】本文详细介绍了Linux系统中ping命令的使用,包括其基本语法、输出信息、常用参数及高级用法。通过ping,用户可测试网络连通性、诊断故障及评估性能。此外,文章还讨论了ping在不同协议、模拟网络环境及与其他命令结合使用时的场景。注意防火墙和网络环境可能影响ping结果,理解错误信息有助于网络问题排查。熟练掌握ping命令,能助你成为Linux网络专家。不断学习和实践,提升网络技能,为构建稳定网络环境贡献力量。
677 0
|
测试技术 Linux 网络架构
|
4月前
|
Linux 网络安全 数据安全/隐私保护
Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
在 Linux 系统中,xxd 命令是一个强大的十六进制 dump 工具,可以将文件或数据以十六进制和 ASCII 字符形式显示,帮助用户深入了解和分析数据。本文详细介绍了 xxd 命令的基本用法、高级功能及实际应用案例,包括查看文件内容、指定输出格式、写入文件、数据比较、数据提取、数据转换和数据加密解密等。通过掌握这些技巧,用户可以更高效地处理各种数据问题。
413 8
|
4月前
|
监控 Linux
如何检查 Linux 内存使用量是否耗尽?这 5 个命令堪称绝了!
本文介绍了在Linux系统中检查内存使用情况的5个常用命令:`free`、`top`、`vmstat`、`pidstat` 和 `/proc/meminfo` 文件,帮助用户准确监控内存状态,确保系统稳定运行。
1284 6