Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4030665
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: C/C++

2012-02-09 23:47:55

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <time.h>

  9. void usage(void)
  10. {
  11.     printf("port \n");
  12.     printf("command:\n");
  13.     printf("-s set the start port\n");
  14.     printf("-e set the end port\n");
  15.     printf("-p set the ip address\n");

  16.     return;
  17. }

  18. int main(int argc,char **argv)
  19. {
  20.     char c;
  21.     char ip[30];
  22.     int startport = 1,endport = 1024;
  23.     int sockfd = -1,i = 0;
  24.     struct sockaddr_in addr;
  25.     float costtime;
  26.     clock_t start,end;

  27.     while ((c = getopt(argc, argv, "?s:e:p:")) > 0)
  28.     {
  29.         switch (c)
  30.         {
  31.             case 's':
  32.                 startport = atoi(optarg);
  33.                 break;
  34.             case 'e':
  35.                 endport = atoi(optarg);
  36.                 break;
  37.             case 'p':
  38.                 memcpy(ip,optarg,sizeof(ip));
  39.                 break;
  40.             default:
  41.                 usage();
  42.         }
  43.     }

  44.     if(startport<1 || endport>65535)
  45.     {
  46.         printf("PORT RANGE ERROR!\n");
  47.         return -1;
  48.     }
  49.     
  50.     printf("Interesting ports %d-%d on %s\n",startport,endport,ip);
  51.     addr.sin_family=AF_INET;
  52.     addr.sin_addr.s_addr=inet_addr(ip);
  53.     start=clock();
  54.     
  55.     for(i=startport;i<=endport;i++)
  56.     {
  57.         sockfd=socket(AF_INET,SOCK_STREAM,0);
  58.         addr.sin_port=htons(i);
  59.         if(connect(sockfd,(const struct sockaddr *)&addr,sizeof(struct sockaddr_in)) == 0)
  60.         {
  61.             printf("%5d open\n",i);
  62.             close(sockfd);
  63.         }
  64.     }

  65.     end=clock();
  66.     costtime=(float)(end-start)/CLOCKS_PER_SEC;
  67.     printf("\nSmart Nmap finished: %s scanned in %f seconds\n",ip,costtime);

  68.     return 0;
  69. }

阅读(3181) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~