Linux C简单端口扫描程序

原创
2013/04/05 19:17
阅读数 1.3K
// gcc -o PortScan PortScan.c
// ./PortScan IP

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>


int SERVER_PORT=1;
// #define  SERVER_IP   "127.0.0.1"
#define  WRITE_BUF_SIZE  1024

#define  SUCCESS 0
#define  FAILURE -1

int main(int argc, char **argv)
{
    int iSockFd;
    int iReadNum;
    char acBuf[WRITE_BUF_SIZE];
    struct sockaddr_in stServerAddr;
    struct sockaddr_in stClientAddr;
    int iServerLen;
    int iClientLen;
    int iReturn;

    printf("scanning ...... \n");

    while(SERVER_PORT<3000)
    {
    // create server socket
    iSockFd = socket(AF_INET, SOCK_STREAM, 0);
    if (iSockFd < 0)
    {
        printf("socket create failed in CommManageServerRecvThread.\n");
	return FAILURE;
    }
    stServerAddr.sin_family = AF_INET;
    inet_pton(AF_INET, argv[1], &stServerAddr.sin_addr);
    stServerAddr.sin_port = htons(SERVER_PORT);
    iServerLen = sizeof(stServerAddr);
        
      
        iReturn = connect(iSockFd, (const struct sockaddr *)&stServerAddr, iServerLen);
        if(iReturn >= 0)
        {
           sleep(1);
	   printf("port %d open\n",SERVER_PORT);
        }
        
      close(iSockFd);
     SERVER_PORT++;
    }
    return SUCCESS;
}
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部