rcv.c--监听514端口并收消息,调用脚本zip_file_check.sh发消息。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <signal.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <time.h> #define MYPORT 514 #define MY_FILE "/home/uniadmin/neo/test/lvwengang/zipfile" #define LOG_FILE "/home/uniadmin/neo/test/lvwengang/logfile.log" struct packet { char data[1024]; }; void handle(int sig) { fprintf(stderr,"A %d signal.\n",sig); return; } int log_file(char *path,char *mode,char *data) { int ret; FILE *fp,*fp_log; time_t timep; struct tm *tp; char nowtime[128]; if(strstr(path,"zipfile")) { fp=fopen(path,mode); if(fp==NULL) { fprintf(stderr,"fopen() error.\n"); exit(-1); } ret=fprintf(fp,"%s\n",data); if(ret==-1) { fprintf(stderr,"fprintf() error.\n"); exit(-1); } ret=fclose(fp); if(ret==EOF) { fprintf(stderr,"fclose() error.\n"); exit(-1); } } else if(strstr(path,"logfile")) { fp_log=fopen(path,"a+"); if(fp_log==NULL) { fprintf(stderr,"fopen() error.\n"); exit(-1); } 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); fprintf(fp_log,"%s\n",nowtime); fprintf(fp_log,"%s",data); fprintf(fp_log,"%s\n\n\n","Ok!! mail send success..."); ret=fclose(fp_log); if(ret==EOF) { fprintf(stderr,"fclose() error.\n"); exit(-1); } } return 0; } int main(void) { int sd,ret; int his_end_len; struct packet pkt; struct sockaddr_in my_end,his_end; /* //-- to backgroud int fd; pid_t pid; pid=fork(); if(pid==-1) { perror("fork()"); exit(-1); } if(pid!=0) { exit(0); } 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("/"); //-- */ signal(SIGCHLD,handle); sd=socket(PF_INET,SOCK_DGRAM,0); if(sd==-1) { perror("socket()"); exit(-1); } int flag=1,len=sizeof(int); ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len); if(ret==-1) { fprintf(stderr,"setsockopt() error.\n"); } my_end.sin_family=AF_INET; my_end.sin_port=htons(MYPORT); my_end.sin_addr.s_addr=inet_addr("0.0.0.0"); ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end)); if(ret==-1) { perror("bind()"); exit(-1); } his_end_len=sizeof(his_end); while(1) { do { ret=recvfrom(sd,&pkt,sizeof(pkt),0,(struct sockaddr *)&his_end,&his_end_len); // if(ret==-1 && errno==EINTR) // fprintf(stderr,"A signal.\n"); //} while(ret==-1 && errno==EINTR); } while(ret==-1); pkt.data[ret]='\0'; // write MY_FILE ret=log_file(MY_FILE,"w",pkt.data); if(ret==-1) { fprintf(stderr,"log_file(MY_FILE) error.\n"); } // send mail system("/home/uniadmin/neo/test/lvwengang/zip_file_check.sh"); // write LOG_FILE ret=log_file(LOG_FILE,"a+",pkt.data); if(ret==-1) { fprintf(stderr,"log_file(MY_FILE) error.\n"); } } exit(0); }
zip_file_check.sh--发送收到的消息
#!/bin/sh MY_PATH=/home/uniadmin/neo/test/lvwengang FILE=zipfile DATE=`date +%Y-%m-%d-%H-%M-%S` NEW_FILE=$FILE-$DATE # my mail my_mail() { if [ -e $MY_PATH/$FILE ] ; then /bin/mv $MY_PATH/$FILE $MY_PATH/$NEW_FILE # /bin/mail -s '75.61 log ...' [email protected] < $MY_PATH/$NEW_FILE # /bin/mail -s '75.61 log ...' -c [email protected] [email protected] < $MY_PATH/$NEW_FILE /bin/mail -s '75.61 log ...' [email protected] < $MY_PATH/$NEW_FILE fi } # 30 day ago delete file delete_file() { find $MY_PATH -name 'zipfile*' -mtime +3 -exec rm {} \; } # execute my_mail delete_file exit 0