Created
September 16, 2012 00:24
-
-
Save fakessh/3730542 to your computer and use it in GitHub Desktop.
scan multiple ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* compile with gcc -Wall -Wextra -ggdb -pedantic -o scantest scantest.c */ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <sys/select.h> | |
#include <sys/time.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#define MAX_FD 30 | |
#define PORT 21 | |
int main(int argc,char *argv[]) | |
{ | |
unsigned long ip, temp; | |
struct sockaddr_in sin; | |
int n,err; | |
unsigned int len; | |
int max=0; | |
fd_set fd_group; | |
struct timeval timeout; | |
struct ip_fd | |
{ | |
unsigned long ip; | |
int fd; | |
} tab_ip[MAX_FD]; | |
len=sizeof(err); | |
if(argc!=2) | |
{ | |
printf("Usage: %s <ip_addr>\n",argv[0]); | |
exit(1); | |
} | |
if((ip=inet_addr(argv[1]))==INADDR_NONE) | |
{ | |
printf("Adresse ip invalide!\n"); | |
exit(1); | |
} | |
temp=htonl(ip); | |
sin.sin_port=htons(PORT); | |
sin.sin_family=AF_INET; | |
timeout.tv_sec=1; | |
timeout.tv_usec=0; | |
FD_ZERO(&fd_group); | |
for(n=0;n<MAX_FD;n++) | |
{ | |
ip=ntohl(temp); | |
tab_ip[n].ip=ip; | |
tab_ip[n].fd=socket(PF_INET,SOCK_STREAM,0); | |
max=((max>(tab_ip[n].fd))?max:(tab_ip[n].fd)); | |
sin.sin_addr.s_addr=ip; | |
fcntl(tab_ip[n].fd,F_SETFL,O_NONBLOCK); | |
if((connect(tab_ip[n].fd,(struct sockaddr*)&sin,sizeof(sin)))==-1) | |
{ | |
if(errno!=EINPROGRESS) | |
{ | |
close(tab_ip[n].fd); | |
temp++; | |
continue; | |
} | |
} | |
FD_SET(tab_ip[n].fd,&fd_group); | |
temp++; | |
} | |
select(max+1,NULL,&fd_group,NULL,&timeout); | |
for(n=0;n<MAX_FD;n++) | |
{ | |
if(FD_ISSET(tab_ip[n].fd,&fd_group)) | |
{ | |
getsockopt(tab_ip[n].fd,SOL_SOCKET,SO_ERROR,&err,&len); | |
sin.sin_addr.s_addr=tab_ip[n].ip; | |
if(err==0) | |
{ | |
printf("err=%u\n",err); | |
printf("Port ouvert sur %s\n",inet_ntoa(sin.sin_addr)); | |
close(tab_ip[n].fd); | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment