一篇文章讲明白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

相关文章
|
6天前
|
安全 Linux 数据处理
深入探究Linux的pathchk命令
`pathchk`是Linux命令,用于检查文件名和路径的可移植性,遵循POSIX规范。它检测不可移植字符,确保文件在不同系统间的兼容性。选项如 `-p`, `-P` 和 `--portability` 提供不同级别的检查。基本用法是 `pathchk [选项] [文件名/路径名] [后缀]`。例如,`pathchk /etc/httpd/conf/httpd.conf` 检查路径的可移植性。使用时要注意目标系统的特性,谨慎处理警告,并结合其他命令如`find`使用。在操作前备份数据以防止损失。
|
2天前
|
机器学习/深度学习 固态存储 Linux
一篇文章讲明白Linux下的ping命令用法与实现
一篇文章讲明白Linux下的ping命令用法与实现
|
1月前
|
域名解析 网络协议 Linux
Linux 中的 Nslookup 命令怎么使用?
【4月更文挑战第12天】
152 6
Linux 中的 Nslookup 命令怎么使用?
|
网络协议 安全 Linux
Linux基本功系列之ping命令实战
Linux基本功系列之ping命令实战
202 0
Linux基本功系列之ping命令实战
|
安全 网络协议 Linux
一文让你熟练掌握Linux的ncat(nc)命令
一文让你熟练掌握Linux的ncat(nc)命令
426 0
|
网络协议 Linux
|
Linux Shell
Linux中man命令的使用方法再解释
原文链接:http://www.linuxidc.com/Linux/2017-03/142407.htm Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可。
1120 0