还是大学时学了些C语言知识,这几天心血来潮,看了几天书,一边百度,就写了个简易网络检测程序。原理还简单,经过系统的ping命令,根据返回的结果,判断网络是否为通。(system(“ping 192.168.0.1”)==0,即为通)
程序运行后会生成pingIP.txt文档,可以把要检测的IP放在此文档,且一行一个IP地址。
不通的IP自动保存到log.txt文档。
程序每10分钟,自动再次检测。
int main(void)
{
system("mode con: cols=50 lines=30");//设置窗口大小
system("color 1f");
FILE *fp;
if((fp=fopen("log.txt","wb"))==NULL)
{
printf("\nopen file fail,close!");
getchar();
exit(0);
}
fputs("\n=============勇哥网络简单检测程序V1.0=============\n",fp);
fclose(fp);
FILE *fpIPtxt;
if((fpIPtxt=fopen("PingIP.txt","r"))==NULL)
{
fpIPtxt=fopen("PingIP.txt","w+");
fputs("127.1",fpIPtxt);
}
fclose(fpIPtxt);
pingStart:
printf("\n=============勇哥网络简单检测程序V1.0=============\n");
printf("\n===============隔10分钟自动检测网络===============\n");
printf("\n==============详细请查看日志log.txt===============\n");
printf("\n请在pingIP.txt文档中设置检测IP,且每行一个IP地址。\n");
printf("\n现在时间是: ");
thisNow(0);
int lineCount=getNumberOfEdges();//IP总行数
char ip[lineCount][15];
char buf[1024]; /*缓冲区*/
FILE *fpIP; /*文件指针*/
int len; /*行字符个数*/
int numofline;
numofline=0;
if((fpIP = fopen("pingIP.txt","r"))== NULL)
{
perror("fail to read");
exit (1) ;
}
while(fscanf(fpIP, "%s", &ip[numofline])!= EOF) //读取数据到数组,直到文件结尾(返回EOF)
{
//printf("ip[%d]=%s\n",numofline,&ip[numofline]);//输出值看下对不对
numofline++;
}
printf("\n检测的IP地址 %d 个,分别为:\n",numofline);int numGe=0;for(numGe=0;numGe>>成功!<<<-------------------\n");
}
else
{
shenYing();//网不通声音提示
shenPing();//屏幕变色
shenYing();
shenPing();
printf("\n------------->>>失败,请检查日志!<<<-------------\n");
char *myLog=ip[numGe];
char myT[100]=" ---网络不通--->>> ";
strcat(myT,myLog);
thisNow(1);
writeLog(myT);
system("cls");
}
}
system("mode con: cols=50 lines=8");
//Sleep(600000);//等待时间秒
int thisMiao;
for(thisMiao=0;thisMiao<600;thisMiao++)
{
printf("\n现在时间:");
thisNow(0);
printf("\n->请在pingIP.txt文件中设置检测IP,每行一个IP地址。");
printf("\n-------->>>等待,%d秒后重新检测网络!<<<---------",600-thisMiao);
Sleep(1000);
system("cls");
}
system("mode con: cols=50 lines=30");
goto pingStart;
return 0;
}
//显示日期函数
int thisNow(int i)
{
char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t timep;
struct tm *p;
time(&timep);
//p = gmtime(&timep);
p = localtime(&timep);
if(i==0)
{
printf(" %d年%d月%d日 ", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
printf("%s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
else
{
FILE *fp;
if((fp=fopen("log.txt","a"))==NULL)
{
printf("\nopen file fail,close!");
getchar();
exit(0);
}
//fprintf(fp,myLog);
fprintf(fp,"\n\n%d年%d月%d日 ", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
fprintf(fp,"%s %d:%d:%d", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
fflush(fp); //数据刷新 数据立即更新
fclose(fp);
}
}
//网络不通时输出日志
int writeLog(char wlog[])
{
FILE *fp;
//if((fp=fopen("log.txt","wb"))==NULL)
if((fp=fopen("log.txt","a"))==NULL)
{
printf("\nopen file fail,close!");
getchar();
exit(0);
}
fprintf(fp,wlog);
//fputs(wlog,fp);
fflush(fp); //数据刷新 数据立即更新
fclose(fp);
}
int shenYing(void)
{
int m[]={0,320,391,399,445,466,494, 523}; //定义曲谱中的 do rui mi fa sou la ci 不准确 自己靠感觉调的
int s[]={5,3,5,3,5,2,1}; //我是一个粉刷匠 的曲谱 我就会这句。。。
int t[]={300,300,300,300,300,300,500}; //这是每个音的发生时间, 可以自己定义 半拍多少时间 四分之一拍多少时间
int n;
for (n=0; n<7; n++)
{
Beep(m[s[n]],t[n]); //Beep函数 第一个参数 是 发生频率 第二个参数 是发声时间
}
return 0;
}
//屏幕颜色变换
void shenPing(void)
{
Sleep(300);
system("color 0f");
Sleep(300);
system("color 1f");
Sleep(300);
system("color 2f");
Sleep(300);
system("color 3f");
Sleep(300);
system("color 4f");
Sleep(300);
system("color 5f");
Sleep(300);
system("color 6f");
Sleep(300);
system("color 7f");
Sleep(300);
system("color 8f");
Sleep(300);
system("color 9f");
Sleep(300);
system("color 1f");
}
//计算txt行数
int getNumberOfEdges()
{
char buf[1024]; /*缓冲区*/
FILE *fp; /*文件指针*/
int len; /*行字符个数*/
int numofline;
numofline=0;
if((fp = fopen("pingIP.txt","r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
while(fgets(buf,1024,fp) != NULL)
{
numofline++;
len = strlen(buf);
buf[len-1] = '\0'; /*去掉换行符*/
//printf("%s %d \n",buf,len - 1);
}
//printf("行数: %d \n",numofline);
return numofline;
}