#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32")
void scan1(char *);
void scan2(char *,char *,char *);
void usage(char *);
int main(int argc,char *argv[])
{
if(argc != 2 && argc != 4) usage(argv[0]);
else
{
if(argc == 2) scan1(argv[1]);
else
{
if(argc == 4) scan2(argv[1],argv[2],argv[3]);
}
}
}
void usage(char *help)
{
printf ("==========================================\n");
printf ("Netxscan_port ver0.1 by netxfly@21cn.comn");
printf ("%s <targetIP> [options]\n",help);
printf ("[option]\n");
printf ("<startport> <endport>\n");
printf ("Usage:\n\n");
printf ("%s 192.168.1.1\n",help);
printf ("%s 192.168.1.1 21 5000\n",help);
printf ("==========================================\n");
exit(-1);
}
void scan1(char *target)
{
int i;
WSADATA wsadata;
SOCKET netxfly;
struct sockaddr_in sin;
int ports[18] = {21,22,23,25,53,79,80,110,111,135,139,443,445,554,1433,1521,3306,3389};
if(WSAStartup(MAKEWORD(2,2),&wsadata)!=0)
{
printf("Startup Winsock Failed...\n");
exit(-1);
}
memset(&sin,0,sizeof(sin));
for(i = 0; i < 18; i++)
{
if((netxfly = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
{
printf("Create Socket Err...\r\n");
exit(-1);
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr=inet_addr(target);
sin.sin_port = htons(ports[i]);
if(connect(netxfly,(struct sockaddr*)&sin,sizeof(sin)) != SOCKET_ERROR)
{
printf("port %d is open\r\n",ports[i]);
closesocket(netxfly);
}
closesocket(netxfly);
}
WSACleanup();
}
void scan2(char *target,char *start,char *end)
{
int i;
WSADATA wsadata;
SOCKET netxfly;
struct sockaddr_in sin;
int startPort = atoi(start);
int endPort = atoi(end);
if(WSAStartup(MAKEWORD(2,2),&wsadata)!=0)
{
printf("Startup Winsock Failed...\n");
exit(-1);
}
if((netxfly = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
{
printf("Create Socket Err...\r\n");
exit(-1);
}
memset(&sin,0,sizeof(sin));
for(i = startPort; i <= endPort; i++)
{
if((netxfly = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
{
printf("Create Socket Err...\r\n");
exit(-1);
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr=inet_addr(target);
sin.sin_port = htons(i);
if(connect(netxfly,(struct sockaddr*)&sin,sizeof(sin)) != SOCKET_ERROR)
{
printf("port %d open\r\n",i);
closesocket(netxfly);
}
closesocket(netxfly);
}
WSACleanup();
}