linux下的connect端口扫描程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> int main(int argc,char *argv[]) { if(argc!=4) { printf("please input %s <host> <start> and <end> port\n",argv[0]); return 0; } int sport=atoi(argv[2]),eport=atoi(argv[3]); int sockfd,num; struct hostent *he; struct sockaddr_in server; struct servent *sp; if((he=gethostbyname(argv[1]))==NULL) { printf("gethostbyname error\n"); return 0; } for(int i=sport;i<=eport;i++) { if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { printf("socket() error\n"); return 0; } bzero(&server,sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(i); server.sin_addr.s_addr = inet_addr(argv[1]); if(connect(sockfd,(struct sockaddr*)&server,sizeof(server))==-1) { close(sockfd); } else { sp=getservbyport(server.sin_port,"tcp"); printf("Port %d is open.%s\n",i,(sp==NULL)?"unknow ":sp->s_name); } close(sockfd); } return 1; } |
仅对同一网段内的主机有效。
发表评论
要发表评论,您必须先登录。