Linux C语言写的超级简单端口扫描器

转载自:http://blog.csdn.net/kongjiajie/article/details/4799986

 Linux C语言写的超级简单端口扫描器



这个本来以前也写过的,今天无聊复习下 再写一遍。简单的一塌糊涂,写的不咋地大家见谅哦!有空再加强 嘿嘿!

 

 

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sys/socket.h>  
  4. #include <time.h>  
  5. #include <sys/types.h>  
  6. #include <netinet/in.h>  
  7. #include <netdb.h>  
  8. void msg()  
  9. {  
  10.   printf("EP:scan ip startport endport/nEP:scan ip 127.0.0.1 20 2009/n");  
  11. }  
  12. int main(int argc,char** argv)  
  13. {  
  14.   char *ip;  
  15.   int startport,endport,sockfd,i;  
  16.   struct sockaddr_in to;  
  17.   float costtime;  
  18.   clock_t start,end;  
  19.   if(4!=argc)  
  20.   {  
  21.     msg();  
  22.     return 0;  
  23.   }  
  24.   ip=argv[1];  
  25.   startport=atoi(argv[2]);  
  26.   endport=atoi(argv[3]);  
  27.   if(startport<1 || endport>65535 || endport<startport)  
  28.   {  
  29.     printf("端口范围出错/n");  
  30.     return 0;   
  31.   }  
  32.   else  
  33.     printf("IP:%s %d-%d/n",ip,startport,endport);  
  34.   to.sin_family=AF_INET;  
  35.   to.sin_addr.s_addr=inet_addr(ip);  
  36.   start=clock();  
  37.   for(i=startport;i<=endport;i++)  
  38.   {  
  39.     sockfd=socket(AF_INET,SOCK_STREAM,0);  
  40.     to.sin_port=htons(i);  
  41.     if(connect(sockfd,(struct sockaddr *)&to,sizeof(struct sockaddr))==0)  
  42.     {  
  43.       printf("%s    %d/n",ip,i);  
  44.       close(to);  
  45.     }  
  46.   }  
  47.   end=clock();  
  48.   costtime=(float)(end-start)/CLOCKS_PER_SEC;  
  49.   printf("用时:%f秒/n",costtime);  
  50.   return 0;  
  51. }  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值