CGI 脚本查找包含指定手机号或文件名的文件

本文介绍了一个基于Nagios的日志查询系统搭建过程,包括修改Apache配置以支持CGI执行,创建HTML页面接收用户输入,并通过Shell脚本处理用户请求。该系统能够查询特定电话号码的日志记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编辑/app/nagios-test/apache-2.2.9/conf/httpd.conf文件

将下面 Options None 改为 Options ExecCGI
<Directory "/app/nagios-test/apache-2.2.9/cgi-bin">
    AllowOverride None
#    Options None
    Options ExecCGI
    Order allow,deny
    Allow from all
</Directory>

 

添加/app/nagios-test/apache-2.2.9/htdocs目录的index.html文件

<html>
<head>
<title>Query Log and Nagios</title>
</head>



<body>
<h1>

Log for search phone number and file:


<p><font color="#FF0000>"</font></p>

<form method="POST" action="/cgi-bin/login.cgi">
wap: <input type="text" width="30" name="wap_phone"><br>
<input type="submit">
</form>

<form method="POST" action="/cgi-bin/login.cgi">
esge:<input type="text" width="30" name="esge_phone"><br>
<input type="submit">
</form>

<form method="POST" action="/cgi-bin/login.cgi">
esgm:<input type="text" width="30" name="esgm_file"><br>
<input type="submit">
</form>

goto nagios: <input type="submit" width="30" name="Submit" value="nagios" onclick="window.open('nagios')"

</h1>
</body>


</html>


添加/app/nagios-test/apache-2.2.9/cgi-bin/login.cgi文件--shell脚本实现

#!/bin/sh

set -f
echo "Content-type: text/html"
echo

###################################

#echo "<h1>"

read LINE
echo "$LINE"
echo "<br>"

if [ "$LINE" = "wap_phone=" ] ; then
        echo "is null<br>"
        exit -1
fi

if [ "$LINE" = "esge_phone=" ] ; then
        echo "is null<br>"
        exit -1
fi

if [ "$LINE" = "esgm_file=" ] ; then
        echo "is null<br>"
        exit -1
fi

if [ "$LINE" = "" ] ; then
#       echo "window.open('http://localhost')"
#       echo "<input type=submit width=30 name=Submit value=nagios onclick=window.open('nagios')"

        echo "please return previous page!"
        exit -1
fi


##PHONE_NUM=`echo $LINE | cut -d '=' -f 2`
#echo "PHONE_NUM="$PHONE_NUM
#echo "<br>"

##esgm_file=`echo $LINE | cut -d '=' -f 2`


#echo "PHONE_NUM="$PHONE_NUM
#echo "<br>"

if echo $LINE | grep wap > /dev/null || echo $LINE | grep esge > /dev/null ; then
        PHONE_NUM=`echo $LINE | cut -d '=' -f 2`
        #echo "PHONE_NUM_bit=""$PHONE_NUM_bit"
        #echo "<br>"

        if ! echo "$PHONE_NUM" | grep '^[0-9]*$' > /dev/null ; then
                echo "phone number input is not correct"
                exit -1
        fi

        PHONE_NUM_BIT=$((`echo $PHONE_NUM | wc -c` - 1))

        if [ "$PHONE_NUM_BIT" != 11 ] ; then
                echo "phone number bits isn't enough"
                exit -1
        fi
else
        ESGM_FILE=`echo $LINE | cut -d '=' -f 2`
fi


#echo "$LINE"
#echo "<br>"

MARK=0

if echo $LINE | grep wap > /dev/null ; then
        MARK=1
elif echo $LINE | grep esge > /dev/null  ; then
        MARK=2
else
        MARK=3
fi

#echo "MARK="$MARK"<br>"

#echo `whoami`"<br>"

case $MARK in
        1)      /app/nagios-test/apache-2.2.9/cgi-bin/grep_client-wap-tcp-2011-12-23 $PHONE_NUM > /tmp/cgi_tmp.file
                ;;
        2)      /app/nagios-test/apache-2.2.9/cgi-bin/grep_client-esge-udp-2011-12-23 $PHONE_NUM > /tmp/cgi_tmp.file
                ;;
        3)      /app/nagios-test/apache-2.2.9/cgi-bin/grep_client-esgm-udp-2011-12-23 $ESGM_FILE > /tmp/cgi_tmp.file
                ;;
        *)      break
                ;;
esac

if [ -f /tmp/cgi_tmp.file ] ; then
        sed -i -e '/^$/d' -e 's/$/&<br>/g' /tmp/cgi_tmp.file
        cat /tmp/cgi_tmp.file
        /bin/rm /tmp/cgi_tmp.file
else
        echo "/tmp/cgi_tmp.file not exist.<br>"
        exit -1
fi

#echo "</h1>"

exit 0


grep_client-wap-tcp-2011-12-23.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/wait.h>
#include <errno.h>
//#include <fcntl.h>

//#define MYPORT 8800
#define MYPORT 10009
#define HISPORT 10010

#define IP_COUNT 13
//#define IP_COUNT 3
#define TIMEOUT 10

int sd;

sigjmp_buf pos0;

char buf[1024000];
char tmp_buf[10240];

void alm_handle(int s) {

        close(sd);

        siglongjmp(pos0,1);
}

void chld_handle(int s) {
        while(waitpid(-1,NULL,WNOHANG)>0);
}

// 2011-09-18
// msn: xorg@163.com
// client: ver0.1 for tcp protocol


struct packet {
        char data[1024];
};

//void query_log(char *ip,char *phone) {
int query_log(char *ip,char *phone) {
       // int sd,sd2,newsd,ret,ret_bind;
        int sd2,newsd,ret,ret_bind;
        struct packet pkt;
        struct sockaddr_in my_end,his_end;
        int his_end_len;

        time_t timep;
        struct tm *tp;
        char nowtime[128];

        pid_t pid;

        timep=time(NULL);
        tp=localtime(&timep);


        sprintf(nowtime,"%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);

       // printf("%s\n",nowtime);
        strcat(buf,nowtime);
        strcat(buf,"\n<br>");

        pid=fork();
        if(pid==-1) {
                fprintf(stderr,"fork() error.\n");
                exit(-1);
        }

        if(pid==0) {
                //-- send to phone number
                sd2=socket(PF_INET,SOCK_STREAM,0);
                if(sd==-1) {
                        fprintf(stderr,"socket error.\n");
                        exit(-1);
                }

                his_end.sin_family=AF_INET;
                his_end.sin_port=htons(HISPORT);
                //his_end.sin_addr.s_addr=inet_addr("10.199.75.169");
                his_end.sin_addr.s_addr=inet_addr(ip);

/*
                ret=fcntl(sd2,F_SETFL,O_NONBLOCK);
                if(ret==-1) {
                        fprintf(stderr,"fcntl() error.\n");
                }
*/

                ret=connect(sd2,(struct sockaddr *)&his_end,sizeof(his_end));
                if(ret==-1) {
                //      fprintf(stderr,"child process connect() error to %s.\n",ip);
                        fprintf(stderr,"connect() error to %s.\n",ip);
                //      perror("connect()");
                        exit(-1);
                }

                ret=write(sd2,phone,strlen(phone));
                if(ret==-1) {
                        fprintf(stderr,"write error.\n");
                        exit(-1);
                }

        //      printf("client write %d bytes.\n\n",ret);
                memset(tmp_buf,0,sizeof(tmp_buf));
                sprintf(tmp_buf,"client send %d bytes,str= %s\n\n",ret,phone);
                strcat(buf,tmp_buf);
                strcat(buf,"\n<br>");

                close(sd);

        //      printf("\n\n");
                exit(0);
        }
        else {
        //-- receve result
                sd=socket(PF_INET,SOCK_STREAM,0);
                if(sd==-1) {
                        fprintf(stderr,"socket error.\n");
                        exit(-1);
                }

                my_end.sin_family=AF_INET;
                my_end.sin_port=htons(MYPORT);
        //      my_end.sin_addr.s_addr=inet_addr("0.0.0.0");
                my_end.sin_addr.s_addr=INADDR_ANY;

/*
                struct linger lig_val;
                lig_val.l_onoff=1;
                lig_val.l_linger=0;
                ret=setsockopt(sd,SOL_SOCKET,SO_LINGER,&lig_val,sizeof(lig_val));
                if(ret==-1) {
                        fprintf(stderr,"setsockopt() error.\n");
                }
*/

                int flag=1,len=sizeof(int);
                ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);
                fprintf(stderr,"setsockopt ret=%d\n",ret);
                if(ret==-1) {
                        fprintf(stderr,"setsockopt() error.\n");
                }

                ret_bind=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));
        //      fprintf(stderr,"ret_bind=%d\n",ret_bind);
                if(ret_bind==-1) fprintf(stderr,"errno=%s\n",strerror(errno));
/*
                if(ret_bind==-1) {
                        fprintf(stderr,"bind() error.\n");
                //      exit(-1);
                        return -1;
                }
*/

                ret=listen(sd,5);
                if(ret==-1) {
                        fprintf(stderr,"listen() error.\n");
                        exit(-1);
                }

                his_end_len=sizeof(his_end);
                while(1) {
                        alarm(TIMEOUT);
                        newsd=accept(sd,(struct sockaddr *)&his_end,&his_end_len);
                        if(newsd==-1) {
                                perror("accept()");
                                exit(-1);
                        }

                        ret=read(newsd,&pkt,sizeof(pkt));
                        if(ret==-1) {
                                fprintf(stderr,"read error.\n");
                                exit(-1);
                        }
                        alarm(0);

                        (pkt.data)[ret]='\0';

                //      printf("server read %d bytes, recv=\n%s\n",ret,pkt.data);
                        if(strcmp(ip,"10.199.81.164")) {
                        //      printf("server read %d bytes:\n%s\n\n",ret,pkt.data);
                                memset(tmp_buf,0,sizeof(tmp_buf));
                                sprintf(tmp_buf,"server read %d bytes:\n%s\n\n",ret,pkt.data);
                                strcat(buf,tmp_buf);
                                strcat(buf,"\n<br>");
                        }
                        else {
                        //      printf("server read %d bytes:\n%s\n",ret,pkt.data);
                                memset(tmp_buf,0,sizeof(tmp_buf));
                                sprintf(tmp_buf,"server read %d bytes:\n%s\n\n",ret,pkt.data);
                                strcat(buf,tmp_buf);
                                strcat(buf,"\n<br>");
                        }

                        ret=close(newsd);
                        if(ret==-1) {
                                fprintf(stderr,"close() error.\n");
                                close(sd);
                                exit(-1);
                        }

                        // exit process
                        ret=close(sd);
                        if(ret==-1) {
                                fprintf(stderr,"close() error.\n");
                                exit(-1);
                        }

                        return 0;
                }
        }
}

int main(int argc,char *argv[]) {
        int i,ret1,ret;
        char phone[12];

        char *array_ip[IP_COUNT]={"10.199.81.152","10.199.81.153","10.199.81.154","10.199.81.155","10.199.81.156","10.199.81.157","10.199.81.158","10.199.81.159","10.199.81.160","10.199.81.161","10.199.81.162","10.199.81.163","10.199.81.164"};

//      char *array_ip[IP_COUNT]={"10.199.75.219","10.199.75.226","10.199.75.227"};

        if(argc!=2) {
                printf("%s phone_num\n",argv[0]);
                exit(-1);
        }

        if(strlen(argv[1])!=11) {
                printf("input 11 bit phone number.\n");
                exit(-1);
        }

        memset(phone,0,sizeof(phone));
        strcpy(phone,argv[1]);

        for(i=0;i<11;i++) {
                if(phone[i]<48 || phone[i]>57 ) {
        //      if(!isdigit(phone[i])) {
                        printf("%c is incorrect.\n",phone[i]);
                        exit(-1);
                }
        }

        signal(SIGCHLD,chld_handle);

        signal(SIGALRM,alm_handle);

        for(i=0;i<IP_COUNT;i++) {
                if(i==0) {
                       // printf("-------------------------------------------\n");
                        memset(buf,0,sizeof(buf));
                        strcat(buf,"-------------------------------------------\n<br>");

                }
        //      printf("i=%d\n",i);
                ret1=sigsetjmp(pos0,1);
        //      printf("ret1=%d\n",ret1);
                if(ret1!=0) {
                       // printf("parent process accept or read %s time out 5 sec.\n",array_ip[i]);
                        printf("accept or read %s time out 5 sec.\n",array_ip[i]);
                        printf("-------------------------------------------\n");
                        continue;
                }

                ret=query_log(array_ip[i],argv[1]);
        //      fprintf(stderr,"query_log_ret=%d\n",ret);
        }

        printf("%s",buf);

        exit(0);
}


grep_client-esge-udp-2011-12-23.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/wait.h>

#define MYPORT 10009
#define HISPORT 10010

#define IP_COUNT 6

#define TIMEOUT 5

sigjmp_buf pos0;

char buf[1024000];
char tmp_buf[10240];

void alm_handle(int s) {
        siglongjmp(pos0,1);
}

void chld_handle(int s) {
        while(waitpid(-1,NULL,WNOHANG)>0);
}

// 2011-12-19
// msn: xorg@163.com
// client: ver0.2 for udp protocol

struct packet {
        char data[1024];
};

void query_log(char *ip,char *phone) {
        int sd,sd2,newsd,ret;
        struct packet pkt;
        struct sockaddr_in my_end,his_end;
        int his_end_len;


        time_t timep;
        struct tm *tp;
        char nowtime[128];

        pid_t pid;

        timep=time(NULL);
        tp=localtime(&timep);

        signal(SIGALRM,alm_handle);

        sprintf(nowtime,"%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);

       // printf("%s\n",nowtime);
        strcat(buf,nowtime);
        strcat(buf,"\n<br>");

        pid=fork();
        if(pid==-1) {
                fprintf(stderr,"fork() error.\n");
                exit(-1);
        }

        if(pid==0) {
                //-- send phone number
        //      sd2=socket(PF_INET,SOCK_STREAM,0);
                sd2=socket(PF_INET,SOCK_DGRAM,0);
                if(sd2==-1) {
                        fprintf(stderr,"socket error.\n");
                        exit(-1);
                }

                his_end.sin_family=AF_INET;
                his_end.sin_port=htons(HISPORT);
                //his_end.sin_addr.s_addr=inet_addr("10.199.75.169");
                his_end.sin_addr.s_addr=inet_addr(ip);


        //      ret=connect(sd2,(struct sockaddr *)&his_end,sizeof(his_end));
        //      strcpy(&pkt.data,phone);
        //      ret=sendto(sd2,&pkt,sizeof(pkt),0,(struct sockaddr *)&his_end,sizeof(his_end));
                ret=sendto(sd2,phone,strlen(phone),0,(struct sockaddr *)&his_end,sizeof(his_end));
                if(ret==-1) {
                        perror("connect()");
                        exit(-1);
                }

        //      printf("client send %d bytes,str= %s\n\n",ret,phone);
                memset(tmp_buf,0,sizeof(tmp_buf));
                sprintf(tmp_buf,"client send %d bytes,str= %s\n\n",ret,phone);
                strcat(buf,tmp_buf);
                strcat(buf,"\n<br>");
        //      printf("%s",tmp_buf);

                close(sd2);

        //      printf("\n\n");
                exit(0);
        }
        else {
        //-- receve file name
        //      sd=socket(PF_INET,SOCK_STREAM,0);
                sd=socket(PF_INET,SOCK_DGRAM,0);
                if(sd==-1) {
                        fprintf(stderr,"socket error.\n");
                        exit(-1);
                }

                my_end.sin_family=AF_INET;
                my_end.sin_port=htons(MYPORT);
        //      my_end.sin_addr.s_addr=inet_addr("0.0.0.0");
                my_end.sin_addr.s_addr=INADDR_ANY;

                int flag=1,len=sizeof(int);
                ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);
                if(ret==-1) {
                        fprintf(stderr,"setsockopt() error.\n");
                }

                ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));
                if(ret==-1) {
                        fprintf(stderr,"bind() error.\n");
                        exit(-1);
                }

                his_end_len=sizeof(his_end);
                while(1) {
                        alarm(TIMEOUT);
                        ret=recvfrom(sd,&pkt,sizeof(pkt),0,(struct sockaddr *)&his_end,&his_end_len);
                        if(ret==-1) {
                                fprintf(stderr,"recvfrom() error.\n");
                                exit(-1);
                        }
                        alarm(0);

                        (pkt.data)[ret]='\0';

                //      printf("server read %d bytes, recv=\n%s\n",ret,pkt.data);
                        if(strcmp(ip,"10.199.81.164")) {
                        //      printf("server read %d bytes:\n%s\n\n",ret,pkt.data);
                                memset(tmp_buf,0,sizeof(tmp_buf));
                                sprintf(tmp_buf,"server read %d bytes:\n%s\n\n",ret,pkt.data);
                                strcat(buf,tmp_buf);
                                strcat(buf,"\n<br>");
                        }
                        else {
                        //      printf("server read %d bytes:\n%s\n",ret,pkt.data);
                                memset(tmp_buf,0,sizeof(tmp_buf));
                                sprintf(tmp_buf,"server read %d bytes:\n%s\n\n",ret,pkt.data);
                                strcat(buf,tmp_buf);
                                strcat(buf,"\n<br>");
                        }

                        // exit process
                        ret=close(sd);
                        if(ret==-1) {
                                fprintf(stderr,"close() error.\n");
                                exit(-1);
                        }

                        return;
                }
        }
}

int main(int argc,char *argv[]) {
        int i;
        int ret1;
        char phone[12];

        memset(buf,0,sizeof(buf));

        char *array_ip[IP_COUNT]={"10.199.81.146","10.199.81.147","10.199.81.148","10.199.81.149","10.199.81.150","10.199.81.151"};

        if(argc!=2) {
                printf("%s phone_num\n",argv[0]);
                exit(-1);
        }

        if(strlen(argv[1])!=11) {
                printf("input 11 bit phone number.\n");
                exit(-1);
        }

        signal(SIGCHLD,chld_handle);

        memset(phone,0,sizeof(phone));
        strcpy(phone,argv[1]);

        for(i=0;i<11;i++) {
        //      if(!isdigit(phone[i])) {
                if(phone[i]<48 || phone[i]>57) {
                        printf("%c is incorrect.\n",phone[i]);
                        exit(-1);
                }
        }

        for(i=0;i<IP_COUNT;i++) {
                if(i==0) {
                //      printf("-------------------------------------------\n");
                        memset(buf,0,sizeof(buf));
                        strcat(buf,"----------------------------------------------\n");
                }
        //      printf("i=%d\n",i);
                ret1=sigsetjmp(pos0,1);
        //      printf("ret1=%d\n",ret1);
                if(ret1!=0) {
                        printf("TIME OUT 5 sec: %s\n",array_ip[i]);
                        printf("-------------------------------------------\n");
                        continue;
                }
                query_log(array_ip[i],argv[1]);
        }

        printf("%s\n",buf);

        exit(0);
}


grep_client-esgm-udp-2011-12-23.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/wait.h>

#define MYPORT 10009
#define HISPORT 10010

#define IP_COUNT 3

#define TIMEOUT 5

sigjmp_buf pos0;

char buf[1024000];
char tmp_buf[10240];

void alm_handle(int s) {
        siglongjmp(pos0,1);
}

void chld_handle(int s) {
        while(waitpid(-1,NULL,WNOHANG)>0);
}

// 2011-12-19
// msn: xorg@163.com
// client: ver0.2 for udp protocol

struct packet {
        char data[1024];
};

void query_log(char *ip,char *filename) {
        int sd,sd2,newsd,ret;
        struct packet pkt;
        struct sockaddr_in my_end,his_end;
        int his_end_len;


        time_t timep;
        struct tm *tp;
        char nowtime[128];

        pid_t pid;

        timep=time(NULL);
        tp=localtime(&timep);

        signal(SIGALRM,alm_handle);

        sprintf(nowtime,"%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);

       // printf("%s\n",nowtime);
        strcat(buf,nowtime);
        strcat(buf,"\n<br>");

        pid=fork();
        if(pid==-1) {
                fprintf(stderr,"fork() error.\n");
                exit(-1);
        }

        if(pid==0) {
                //-- send filename
        //      sd2=socket(PF_INET,SOCK_STREAM,0);
                sd2=socket(PF_INET,SOCK_DGRAM,0);
                if(sd2==-1) {
                        fprintf(stderr,"socket error.\n");
                        exit(-1);
                }

                his_end.sin_family=AF_INET;
                his_end.sin_port=htons(HISPORT);
                //his_end.sin_addr.s_addr=inet_addr("10.199.75.169");
                his_end.sin_addr.s_addr=inet_addr(ip);


        //      ret=connect(sd2,(struct sockaddr *)&his_end,sizeof(his_end));
        //      strcpy(&pkt.data,filename);
        //      ret=sendto(sd2,&pkt,sizeof(pkt),0,(struct sockaddr *)&his_end,sizeof(his_end));
                ret=sendto(sd2,filename,strlen(filename),0,(struct sockaddr *)&his_end,sizeof(his_end));
                if(ret==-1) {
                        perror("connect()");
                        exit(-1);
                }

        //      printf("client send %d bytes,str= %s\n\n",ret,filename);
                memset(tmp_buf,0,sizeof(tmp_buf));
                sprintf(tmp_buf,"client send %d bytes,str= %s\n\n",ret,filename);
                strcat(buf,tmp_buf);
                strcat(buf,"\n<br>");
        //      printf("%s",tmp_buf);

                close(sd2);

        //      printf("\n\n");
                exit(0);
        }
        else {
        //-- receve file name
        //      sd=socket(PF_INET,SOCK_STREAM,0);
                sd=socket(PF_INET,SOCK_DGRAM,0);
                if(sd==-1) {
                        fprintf(stderr,"socket error.\n");
                        exit(-1);
                }

                my_end.sin_family=AF_INET;
                my_end.sin_port=htons(MYPORT);
        //      my_end.sin_addr.s_addr=inet_addr("0.0.0.0");
                my_end.sin_addr.s_addr=INADDR_ANY;

                int flag=1,len=sizeof(int);
                ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);
                if(ret==-1) {
                        fprintf(stderr,"setsockopt() error.\n");
                }

                ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));
                if(ret==-1) {
                        fprintf(stderr,"bind() error.\n");
                        exit(-1);
                }

                his_end_len=sizeof(his_end);
                while(1) {
                        alarm(TIMEOUT);
                        ret=recvfrom(sd,&pkt,sizeof(pkt),0,(struct sockaddr *)&his_end,&his_end_len);
                        if(ret==-1) {
                                fprintf(stderr,"recvfrom() error.\n");
                                exit(-1);
                        }
                        alarm(0);

                        (pkt.data)[ret]='\0';

                //      printf("server read %d bytes, recv=\n%s\n",ret,pkt.data);
                        if(strcmp(ip,"10.199.81.164")) {
                        //      printf("server read %d bytes:\n%s\n\n",ret,pkt.data);
                                memset(tmp_buf,0,sizeof(tmp_buf));
                                sprintf(tmp_buf,"server read %d bytes:\n%s\n\n",ret,pkt.data);
                                strcat(buf,tmp_buf);
                                strcat(buf,"\n<br>");
                        }
                        else {
                        //      printf("server read %d bytes:\n%s\n",ret,pkt.data);
                                memset(tmp_buf,0,sizeof(tmp_buf));
                                sprintf(tmp_buf,"server read %d bytes:\n%s\n\n",ret,pkt.data);
                                strcat(buf,tmp_buf);
                                strcat(buf,"\n<br>");
                        }

                        // exit process
                        ret=close(sd);
                        if(ret==-1) {
                                fprintf(stderr,"close() error.\n");
                                exit(-1);
                        }

                        return;
                }
        }
}

int main(int argc,char *argv[]) {
        int i;
        int ret1;
        char filename[128];

        memset(buf,0,sizeof(buf));

        char *array_ip[IP_COUNT]={"10.199.81.135","10.199.81.136","10.199.81.138"};

        if(argc!=2) {
                printf("%s filename\n",argv[0]);
                exit(-1);
        }

        /*
        if(strlen(argv[1])!=11) {
                printf("input 11 bit filename .\n");
                exit(-1);
        }
        */

        signal(SIGCHLD,chld_handle);

        memset(filename,0,sizeof(filename));
        strcpy(filename,argv[1]);

        /*
        for(i=0;i<11;i++) {
        //      if(!isdigit(filename[i])) {
                if(filename[i]<48 || filename[i]>57) {
                        printf("%c is incorrect.\n",filename[i]);
                        exit(-1);
                }
        }
        */

        for(i=0;i<IP_COUNT;i++) {
                if(i==0) {
                //      printf("-------------------------------------------\n");
                        memset(buf,0,sizeof(buf));
                        strcat(buf,"----------------------------------------------\n");
                }
        //      printf("i=%d\n",i);
                ret1=sigsetjmp(pos0,1);
        //      printf("ret1=%d\n",ret1);
                if(ret1!=0) {
                        printf("TIME OUT 5 sec: %s\n",array_ip[i]);
                        printf("-------------------------------------------\n");
                        continue;
                }
                query_log(array_ip[i],argv[1]);
        }

        printf("%s\n",buf);

        exit(0);
}


grep_server-wap-tcp-2011-09-21.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#define MYPORT 10010
#define HISPORT 10009

#define LOGFILE "/home/mtvwap/file.log"
//#define LOGFILE "/home/uniadmin/test/logfile.log"

// 2011-09-18
// msn: xorg@163.com
// server: ver0.1

struct packet {
        char data[64];
};

int main() {
        FILE *file,*fp_log;
        char cmd[512];
        char line[1024];
        char data[2048];
        char hostname[128];

        time_t timep;
        struct tm *tp;
        char nowtime[128];

        //--
        pid_t pid;
        int fd;

        pid=fork();
        if(pid==-1) {
                perror("fork()");
                exit(-1);
        }

        if(pid!=0)
                exit(0);
        else {
                fd=open("/dev/null",O_RDONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDIN_FILENO);
                        dup(fd);
                        close(fd);
                }

                fd=open("/dev/null",O_WRONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDOUT_FILENO);
                        dup(fd);
                        close(STDERR_FILENO);
                        dup(fd);
                        close(fd);
                }

                setsid();

                chdir("/");
        }
        //--

        gethostname(hostname,sizeof(hostname));
        printf("%s\n",hostname);

        int sd,sd2,newsd,ret;
        struct sockaddr_in my_end,his_end;
        int his_end_len;
        struct packet pkt;

        sd=socket(PF_INET,SOCK_STREAM,0);
        if(sd==-1) {
                fprintf(stderr,"socket error.\n");
        //      exit(-1);
        }


        // my_end
        my_end.sin_family=AF_INET;
        my_end.sin_port=htons(MYPORT);
        my_end.sin_addr.s_addr=inet_addr("0.0.0.0");
        //      my_end.sin_addr.s_add=INADDR_ANY;

        int flag=1,len=sizeof(int);
        ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);
        if(ret==-1) {
                fprintf(stderr,"setsockopt() error.\n");
        }

        ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));
        if(sd==-1) {
                fprintf(stderr,"socket error.\n");
                exit(-1);
        }

        ret=listen(sd,5);
        if(ret==-1) {
                fprintf(stderr,"sd listen error.\n");
                fprintf(fp_log,"%s","listen() error.\n");
                exit(-1);
        }

        his_end_len=sizeof(his_end);
        while(1) {
                newsd=accept(sd,(struct sockaddr *)&his_end,&his_end_len);
                if(newsd==-1) {
                        perror("accept()");
                //      exit(-1);
                }
                printf("accept ok\n");
                printf("his_end=%s\n",inet_ntoa(his_end.sin_addr));


                ret=read(newsd,&pkt,sizeof(pkt));
                if(ret==-1) {
                        fprintf(stderr,"read error.\n");
                        fprintf(fp_log,"%s","newsd read() error.\n");
                //      exit(-1);
                }
                //else continue;
                (pkt.data)[ret]='\0';
                printf("read newsd ok\n");

                // log file
                fp_log=fopen(LOGFILE,"a+");
                if(fp_log==NULL) {
                        fprintf(stderr,"fopen() error.\n");
                       // exit(-1);
                }
                printf("fopen ok\n");

                timep=time(NULL);
                tp=localtime(&timep);
                sprintf(nowtime,"\n\n%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
                fprintf(fp_log,"%s\n",nowtime);
                fprintf(fp_log,"%s\n",hostname);

                printf("server read %d bytes, recv=%s\n",ret,pkt.data);
                fprintf(fp_log,"server read %d bytes, recv=%s\n",ret,pkt.data);
                // log file

                // -----------
                // sprintf(cmd,"%s","cd /home/mtvwap/portal_wap/logs/server ; grep -l 18635529831 server*");
                sprintf(cmd,"cd /home/mtvwap/portal_wap/logs/server ; grep -l %s  server*",pkt.data);
                printf("cmd=%s\n",cmd);
                if(strstr(cmd,"\n")) {
                        printf("warn:cmd is have enter, to restart.\n\n");
                        continue;
                }

                file=popen(cmd,"r");
                if(file!=NULL) {
                        sprintf(data,"%s\n",hostname);
                        while(fgets(line,1023,file)!=NULL) {
                                // printf("%s",line);
                                //      strcpy(data,hostname);
                                strcat(data,line);
                                //      strcat(data,"\n");
                        }
                }
                strcat(data,"----------------------------------------------");
                printf("popen ok\n");

                pclose(file);
                printf("pclose ok\n");
                // -----------

                /*
                   if(strstr(pkt.data,"hello"))
                   printf("execute script.\n");
                   else
                   printf("no execute script.\n");
                 */

                //--
                sleep(2);

                sd2=socket(PF_INET,SOCK_STREAM,0);
                if(sd==-1) {
                        fprintf(stderr,"socket error.\n");
                        fprintf(fp_log,"%s","sd2 socket() error.\n");
                //      exit(-1);
                }
                printf("socket sd2 ok\n");

                // his_end
                his_end.sin_family=AF_INET;
                his_end.sin_port=htons(HISPORT);
        //      his_end.sin_addr.s_addr=inet_addr("10.199.81.166");
                his_end.sin_addr.s_addr=his_end.sin_addr.s_addr;

                ret=connect(sd2,(struct sockaddr *)&his_end,sizeof(his_end));
                if(ret==-1) {
                        perror("connect()");
                        fprintf(fp_log,"%s\n","sd2 connect() error.");
                        fclose(fp_log);
                        printf("sd2 connect() stop, to restart.\n\n");
                        continue;
                //      exit(-1);
                }
                printf("connect ok\n");

                ret=write(sd2,data,strlen(data));
                if(ret==-1) {
                        fprintf(stderr,"write error.\n");
                        fprintf(fp_log,"%s\n","write error.");
                //      exit(-1);
                }
                printf("write ok\n");

                printf("\nclient write %d bytes .\nstr=%s\n",ret,data);

                // log file
                fprintf(fp_log,"client write %d bytes.\n",ret);
                fprintf(fp_log,"%s\n\n","Ok!! grep success...");

                ret=fclose(fp_log);
                if(ret==EOF) {
                        fprintf(stderr,"fclose() error.\n");
                       // exit(-1);
                }
                printf("fclose ok\n");
                // end log file
                //--

                printf("\n");

                close(newsd);
                close(sd2);
        }

        close(sd);

        exit(0);
}


grep_server-esge-udp-2011-12-19.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#define MYPORT 10010
#define HISPORT 10009

#define LOGFILE "/home/esge/grep_file.log"
//#define LOGFILE "/home/uniadmin/test/logfile.log"

// 2011-12-19
// msn: xorg@163.com
// server: ver0.2 for udp protocol

struct packet {
        char data[64];
};

int main() {
        FILE *file,*fp_log;
        char cmd[512];
        char line[1024];
        char data[2048];
        char hostname[128];

        time_t timep;
        struct tm *tp;
        char nowtime[128];

        //-- setsid
        pid_t pid;
        int fd;

        pid=fork();
        if(pid==-1) {
                perror("fork()");
                exit(-1);
        }

        if(pid!=0)
                exit(0);
        else {
                fd=open("/dev/null",O_RDONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDIN_FILENO);
                        dup(fd);
                        close(fd);
                }

                fd=open("/dev/null",O_WRONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDOUT_FILENO);
                        dup(fd);
                        close(STDERR_FILENO);
                        dup(fd);
                        close(fd);
                }

                setsid();

                chdir("/");
        }
        //--

        gethostname(hostname,sizeof(hostname));
        printf("%s\n",hostname);

        int sd,sd2,newsd,ret;
        struct sockaddr_in my_end,his_end;
        int his_end_len;
        struct packet pkt;

        sd=socket(PF_INET,SOCK_DGRAM,0);
        if(sd==-1) {
                fprintf(stderr,"socket error.\n");
        //      exit(-1);
        }


        // my_end
        my_end.sin_family=AF_INET;
        my_end.sin_port=htons(MYPORT);
        my_end.sin_addr.s_addr=inet_addr("0.0.0.0");
//      my_end.sin_addr.s_addr=INADDR_ANY;

        int flag=1,len=sizeof(int);
        ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);
        if(ret==-1) {
                fprintf(stderr,"setsockopt() error.\n");
        }

        ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));
        if(sd==-1) {
                fprintf(stderr,"socket error.\n");
                exit(-1);
        }


        his_end_len=sizeof(his_end);
        while(1) {
                ret=recvfrom(sd,&pkt,sizeof(pkt),0,(struct sockaddr *)&his_end,&his_end_len);
                (pkt.data)[ret]='\0';
                printf("recvfrom ok,%s\n",pkt.data);

                // log file
                fp_log=fopen(LOGFILE,"a+");
                if(fp_log==NULL) {
                        fprintf(stderr,"fopen() error.\n");
                       // exit(-1);
                }
                printf("fopen ok\n");

                timep=time(NULL);
                tp=localtime(&timep);
                sprintf(nowtime,"\n\n%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
                fprintf(fp_log,"%s\n",nowtime);
                fprintf(fp_log,"%s\n",hostname);

                printf("server read %d bytes, recv=%s\n",ret,pkt.data);
                fprintf(fp_log,"server read %d bytes, recv=%s\n",ret,pkt.data);
                // log file

                // -----------
                // sprintf(cmd,"%s","cd /home/mtvwap/portal_wap/logs/server ; grep -l 18635529831 server*");
                sprintf(cmd,"cd /home/esge/ESGEV100R001C02/logs/esge ; grep -l %s  esge*",pkt.data);
                printf("cmd=%s\n",cmd);
                if(strstr(cmd,"\n")) {
                        printf("warn:cmd is have enter, to restart.\n\n");
                        continue;
                }

                file=popen(cmd,"r");
                if(file!=NULL) {
                        sprintf(data,"%s\n",hostname);
                        while(fgets(line,1023,file)!=NULL) {
                                // printf("%s",line);
                                //      strcpy(data,hostname);
                                strcat(data,line);
                                //      strcat(data,"\n");
                        }
                }
                strcat(data,"----------------------------------------------");
                printf("popen ok\n");

                pclose(file);
                printf("pclose ok\n");
                // -----------


                //--
                sleep(2);

                sd2=socket(PF_INET,SOCK_STREAM,0);
                if(sd==-1) {
                        fprintf(stderr,"socket error.\n");
                        fprintf(fp_log,"%s","sd2 socket() error.\n");
                //      exit(-1);
                }
                printf("socket sd2 ok\n");

                // his_end
                his_end.sin_family=AF_INET;
                his_end.sin_port=htons(HISPORT);
                his_end.sin_addr.s_addr=his_end.sin_addr.s_addr;

                ret=sendto(sd,&data,strlen(data),0,(struct sockaddr *)&his_end,sizeof(his_end));
                if(ret==-1) {
                        fprintf(stderr,"sendto() error.\n");
                        exit(-1);
                }

                printf("sendto ok\n");

                printf("\nclient sendto %d bytes .\nstr=%s\n",ret,data);

                // log file
                fprintf(fp_log,"client write %d bytes.\n",ret);
                fprintf(fp_log,"%s\n\n","Ok!! grep success...");

                ret=fclose(fp_log);
                if(ret==EOF) {
                        fprintf(stderr,"fclose() error.\n");
                       // exit(-1);
                }
                printf("fclose ok\n");
                // end log file
                //--

                printf("\n");

        //      close(newsd);
                close(sd2);
        }

        close(sd);

        exit(0);
}


grep_server-esgm-udp-2011-12-19.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#define MYPORT 10010
#define HISPORT 10009

#define LOGFILE "/home/esgm/grep_file.log"
//#define LOGFILE "/home/uniadmin/test/logfile.log"

// 2011-12-19
// msn: xorg@163.com
// server: ver0.2 for udp protocol

struct packet {
        char data[64];
};

int main() {
        FILE *file,*fp_log;
        char cmd[512];
        char line[1024];
        char data[2048];
        char hostname[128];

        time_t timep;
        struct tm *tp;
        char nowtime[128];

        //-- setsid
        pid_t pid;
        int fd;

        pid=fork();
        if(pid==-1) {
                perror("fork()");
                exit(-1);
        }

        if(pid!=0)
                exit(0);
        else {
                fd=open("/dev/null",O_RDONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDIN_FILENO);
                        dup(fd);
                        close(fd);
                }

                fd=open("/dev/null",O_WRONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDOUT_FILENO);
                        dup(fd);
                        close(STDERR_FILENO);
                        dup(fd);
                        close(fd);
                }

                setsid();

                chdir("/");
        }
        //--

        gethostname(hostname,sizeof(hostname));
        printf("%s\n",hostname);

        int sd,sd2,newsd,ret;
        struct sockaddr_in my_end,his_end;
        int his_end_len;
        struct packet pkt;

        sd=socket(PF_INET,SOCK_DGRAM,0);
        if(sd==-1) {
                fprintf(stderr,"socket error.\n");
        //      exit(-1);
        }


        // my_end
        my_end.sin_family=AF_INET;
        my_end.sin_port=htons(MYPORT);
        my_end.sin_addr.s_addr=inet_addr("0.0.0.0");
//      my_end.sin_addr.s_addr=INADDR_ANY;

        int flag=1,len=sizeof(int);
        ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);
        if(ret==-1) {
                fprintf(stderr,"setsockopt() error.\n");
        }

        ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));
        if(sd==-1) {
                fprintf(stderr,"socket error.\n");
                exit(-1);
        }


        his_end_len=sizeof(his_end);
        while(1) {
                ret=recvfrom(sd,&pkt,sizeof(pkt),0,(struct sockaddr *)&his_end,&his_end_len);
                (pkt.data)[ret]='\0';
                printf("recvfrom ok,%s\n",pkt.data);

                // log file
                fp_log=fopen(LOGFILE,"a+");
                if(fp_log==NULL) {
                        fprintf(stderr,"fopen() error.\n");
                       // exit(-1);
                }
                printf("fopen ok\n");

                timep=time(NULL);
                tp=localtime(&timep);
                sprintf(nowtime,"\n\n%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
                fprintf(fp_log,"%s\n",nowtime);
                fprintf(fp_log,"%s\n",hostname);

                printf("server read %d bytes, recv=%s\n",ret,pkt.data);
                fprintf(fp_log,"server read %d bytes, recv=%s\n",ret,pkt.data);
                // log file

                // -----------
                // sprintf(cmd,"%s","cd /home/mtvwap/portal_wap/logs/server ; grep -l 18635529831 server*");
                // sprintf(cmd,"cd /home/esge/ESGEV100R001C02/logs/esge ; grep -l %s  esge*",pkt.data);
                sprintf(cmd,"cd /home/esgm/tomcat5.5.25/logs/server ; grep -l %s  server*",pkt.data);
                printf("cmd=%s\n",cmd);
                if(strstr(cmd,"\n")) {
                        printf("warn:cmd is have enter, to restart.\n\n");
                        continue;
                }

                file=popen(cmd,"r");
                if(file!=NULL) {
                        sprintf(data,"%s\n",hostname);
                        while(fgets(line,1023,file)!=NULL) {
                                // printf("%s",line);
                                //      strcpy(data,hostname);
                                strcat(data,line);
                                //      strcat(data,"\n");
                        }
                }
                strcat(data,"----------------------------------------------");
                printf("popen ok\n");

                pclose(file);
                printf("pclose ok\n");
                // -----------


                //--
                sleep(2);

                sd2=socket(PF_INET,SOCK_STREAM,0);
                if(sd==-1) {
                        fprintf(stderr,"socket error.\n");
                        fprintf(fp_log,"%s","sd2 socket() error.\n");
                //      exit(-1);
                }
                printf("socket sd2 ok\n");

                // his_end
                his_end.sin_family=AF_INET;
                his_end.sin_port=htons(HISPORT);
                his_end.sin_addr.s_addr=his_end.sin_addr.s_addr;

                ret=sendto(sd,&data,strlen(data),0,(struct sockaddr *)&his_end,sizeof(his_end));
                if(ret==-1) {
                        fprintf(stderr,"sendto() error.\n");
                        exit(-1);
                }

                printf("sendto ok\n");

                printf("\nclient sendto %d bytes .\nstr=%s\n",ret,data);

                // log file
                fprintf(fp_log,"client write %d bytes.\n",ret);
                fprintf(fp_log,"%s\n\n","Ok!! grep success...");

                ret=fclose(fp_log);
                if(ret==EOF) {
                        fprintf(stderr,"fclose() error.\n");
                       // exit(-1);
                }
                printf("fclose ok\n");
                // end log file
                //--

                printf("\n");

        //      close(newsd);
                close(sd2);
        }

        close(sd);

        exit(0);
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值