探測網路裝置(主機、路由器)存活的方法
阿新 • • 發佈:2019-02-01
//=======================ping============================
void ping_test(************)
{
char command[100]="ping";
char ipaddr[20];
strcpy(ipaddr,ipaddress);
strcat(command,ipaddr);
system(command);
printf("路由ping驗證開啟,等待%s響應!\n",ipaddress);
}
//==================tracerouet==========================
void traceroute_test(************)
{
char command[100]="traceroute ";
char ipaddr[20];
strcpy(ipaddr,ipaddress);
strcat(command,ipaddr);
system(command);
printf("路由traceroute驗證開啟,等待%s響應!\n",ipaddress);
}
//======================syn==============================
int syn_test(***********)
{
char ipaddr[20];
strcpy(ipaddr,ipaddress);
int sockfd;
struct sockaddr_in serv_addr;
FILE *fp;
fp = fopen("test.txt","w+");
if(fp == NULL)
{
fprintf(fp,"開啟檔案錯誤!\n");
return 0;
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(fp, "socket creating error!\n");
return 0;
}
else
printf("socket creating success!\n");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = inet_addr(ipaddr);
int c = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
if(c == -1)
{
fprintf(fp,"%s : SYN探測包傳送成功,等待迴應...\n\n",ipaddr);
if(errno == 111)
fprintf(fp,"%s : Host is alive!",ipaddr);
else if(errno == 113)
fprintf(fp,"%s : No route to host!",ipaddr);
else
{
if(errno == 146)
fprintf(fp,"%s : Host is alive!",ipaddr);
else if(errno == 148)
fprintf(fp,"%s : No route to host!",ipaddr);
else
fprintf(fp,"Host is alive!但有錯誤值返回 : %d",errno);
}
}
else if(c == 0)
{
fprintf(fp,"%s : SYN探測包傳送成功,等待迴應...\n\n",ipaddr);
fprintf(fp,"%s : Host is alive!\n",ipaddr);
}
else
{
fprintf(fp,"%s : SYN探測包傳送失敗!\n\n",ipaddr);
fprintf(fp,"Connect error ID : %d",c);
}
fclose(fp);
close(sockfd);
return 0;
}
void ping_test(************)
{
char command[100]="ping";
char ipaddr[20];
strcpy(ipaddr,ipaddress);
strcat(command,ipaddr);
system(command);
printf("路由ping驗證開啟,等待%s響應!\n",ipaddress);
}
//==================tracerouet==========================
void traceroute_test(************)
{
char command[100]="traceroute ";
char ipaddr[20];
strcpy(ipaddr,ipaddress);
strcat(command,ipaddr);
system(command);
printf("路由traceroute驗證開啟,等待%s響應!\n",ipaddress);
}
//======================syn==============================
int syn_test(***********)
{
char ipaddr[20];
strcpy(ipaddr,ipaddress);
int sockfd;
struct sockaddr_in serv_addr;
FILE *fp;
fp = fopen("test.txt","w+");
if(fp == NULL)
{
fprintf(fp,"開啟檔案錯誤!\n");
return 0;
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(fp, "socket creating error!\n");
return 0;
}
else
printf("socket creating success!\n");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = inet_addr(ipaddr);
int c = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
if(c == -1)
{
fprintf(fp,"%s : SYN探測包傳送成功,等待迴應...\n\n",ipaddr);
if(errno == 111)
fprintf(fp,"%s : Host is alive!",ipaddr);
else if(errno == 113)
fprintf(fp,"%s : No route to host!",ipaddr);
else
{
if(errno == 146)
fprintf(fp,"%s : Host is alive!",ipaddr);
else if(errno == 148)
fprintf(fp,"%s : No route to host!",ipaddr);
else
fprintf(fp,"Host is alive!但有錯誤值返回 : %d",errno);
}
}
else if(c == 0)
{
fprintf(fp,"%s : SYN探測包傳送成功,等待迴應...\n\n",ipaddr);
fprintf(fp,"%s : Host is alive!\n",ipaddr);
}
else
{
fprintf(fp,"%s : SYN探測包傳送失敗!\n\n",ipaddr);
fprintf(fp,"Connect error ID : %d",c);
}
fclose(fp);
close(sockfd);
return 0;
}