?? dns.c
字號:
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
#include <ctype.h>
#include <assert.h>
#include <locale.h>
void getdate(char *ctime)
{
time_t currentTime;
struct tm *tblock;
memset(ctime,0,sizeof(ctime));
time (¤tTime);
tblock=localtime(¤tTime);
sprintf(ctime,"%d%02d%02d",tblock->tm_year+1900,tblock->tm_mon+1,tblock->tm_mday);
}
void gettime( char *ctime )
{
time_t currentTime;
struct tm *tblock;
memset( ctime,0,sizeof(ctime) );
time ( ¤tTime );
tblock = localtime( ¤tTime );
sprintf( ctime,"%02d%02d%02d",tblock->tm_hour,tblock->tm_min,tblock->tm_sec);
}
int writestat(char *mess)
{
FILE * fplog;
char ctime[20];
char cdate[20];
char logfile[40];
char message[255];
memset(ctime,0,sizeof(ctime));
memset(cdate,0,sizeof(cdate));
memset(message,0,sizeof(message));
gettime(ctime);
getdate(cdate);
memset(logfile,0,sizeof(logfile));
sprintf(logfile,"log%s.txt",cdate);
if((fplog=fopen(logfile,"a+"))==NULL)
{
printf("can not open log file :%s for write \n",logfile);
return -1;
}
sprintf(message,"ctime:%s :%s\n",ctime,mess);
fprintf(fplog, "%s",message);
fclose(fplog);
}
int ping(char *ip)
{
char pipename[100];
char record[120+1];
FILE *pipe;
int status=0;
memset(record,0,sizeof(record));
sprintf( pipename,"ping %s'",ip);
if((pipe=popen(pipename,"r"))==NULL)
{
printf("popen error\n");
pclose(pipe);
return 0;
}
fgets( record,120,pipe );
if(record!=NULL&&strlen(record)>4)
{
if(strstr(record,ip)!=NULL&&strstr(record,"alive")!=NULL)
return 1;
}
pclose(pipe);
return 0;
}
int stopgf0()
{
char command[255];
char message[255];
memset(command,0,255);
memset(command,0,255);
sprintf(command,"%s","ifconfig qfe0 down unplumb");
int flag=0;
flag=system(command);
if(flag==0)
sprintf(message,"run :%s success",command);
else
sprintf(message,"run:%s error",command);
writestat(message);
return 1;
}
int startgf1()
{
char command[255];
char message[255];
memset(command,0,255);
memset(command,0,255);
int flag=0;
sprintf(command,"%s","ifconfig qfe1 plumb");
flag=system(command);
if(flag==0)
sprintf(message,"run %s success",command);
else
sprintf(message,"run %s error",command);
writestat(message);
sprintf(command,"%s","ifconfig qfe1:1 plumb");
flag=system(command);
if(flag==0)
sprintf(message,"run %s success",command);
else
sprintf(message,"run %s error",command);
writestat(message);
sprintf(command,"%s","ifconfig qfe1 192.168.10.222 netmask 255.255.255.0 up");
flag=system(command);
if(flag==0)
sprintf(message,"%s","ifconfig qfe1 192.168.10.222 netmask 255.255.255.0 up success");
else
sprintf(message,"%s","ifconfig qfe1 192.168.10.222 netmask 255.255.255.0 up error");
writestat(message);
sprintf(command,"%s","ifconfig qfe1:1 192.168.10.221 netmask 255.255.255.0 up");
flag=system(command);
if(flag==0)
sprintf(message,"%s","ifconfig qfe1:1 192.168.10.221 netmask 255.255.255.0 up success");
else
sprintf(message,"%s","ifconfig qfe1:1 192.168.10.221 netmask 255.255.255.0 up error");
writestat(message);
return 1;
}
int stopgf1()
{
int flag=0;
char command[255];
char message[255];
memset(command,0,255);
memset(command,0,255);
sprintf(command,"%s","ifconfig qfe1 down unplumb");
flag=system(command);
if(flag==0)
sprintf(message,"run :%s success",command);
else
sprintf(message,"run:%s error",command);
writestat(message);
return 1;
}
int startgf0()
{
char command[255];
char message[255];
memset(command,0,255);
memset(command,0,255);
int flag=0;
sprintf(command,"%s","ifconfig qfe0 plumb");
flag=system(command);
if(flag==0)
sprintf(message,"run %s success",command);
else
sprintf(message,"run %s error",command);
writestat(message);
sprintf(command,"%s","ifconfig qfe0:1 plumb");
flag=system(command);
if(flag==0)
sprintf(message,"run %s success",command);
else
sprintf(message,"run %s error",command);
writestat(message);
sprintf(command,"%s","ifconfig qfe0 192.168.10.222 netmask 255.255.255.0 up");
flag=system(command);
if(flag==0)
sprintf(message,"%s","ifconfig qfe0 192.168.10.222 netmask 255.255.255.0 up success");
else
sprintf(message,"%s","ifconfig qfe0 192.168.10.222 netmask 255.255.255.0 up error");
writestat(message);
sprintf(command,"%s","ifconfig qfe0:1 192.168.10.221 netmask 255.255.255.0 up");
flag=system(command);
if(flag==0)
sprintf(message,"%s","ifconfig qfe0:1 192.168.10.221 netmask 255.255.255.0 up success");
else
sprintf(message,"%s","ifconfig qfe0:1 192.168.10.221 netmask 255.255.255.0 up error");
writestat(message);
return 1;
}
int main(int argc,char *argv[])
{
int flag=0;
char pingip[30];
memset(pingip,0,30);
int swipch=0;
char message[255];
memset(message,0,255);
struct timeval tv={2,0};
if(argc<2)
{
printf("useage dns + ip\n");
exit(1);
}
sprintf(pingip,"%s",argv[1]);
while(1)
{
select(0,(fd_set *)NULL,(fd_set *)NULL,(fd_set *)NULL,&tv);
flag=ping (pingip);
if(flag==0)
{
sprintf(message,"ping %s unalive swipch==%d",pingip,swipch);
writestat(message);
if(swipch==0)
{
stopgf0();
sleep(1);
startgf1();
swipch=1;
continue;
}
if(swipch==1)
{
stopgf1();
sleep(1);
startgf0();
swipch=0;
continue;
}
}
}
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -