这段小程序在Linux后台,以守护进程的方式运行。如果eth0的IP发生IP,则会向日志里记录信息并发送SNMP trap。实现的方式比较简单,利用循环和sleep的方法,执行ifconfig来判断的。 也许更好的方式是读写I/O,借助proc来实现了。
以下是源程序
init.c —-初始化守护进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <unistd.h> #include <signal.h> #include <sys/param.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> void init_daemon(void) { int pid; int i; if(pid=fork()) exit(0);//是父进程,结束父进程 else if(pid< 0) exit(1);//fork失败,退出 //是第一子进程,后台继续执行 setsid();//第一子进程成为新的会话组长和进程组长 //并与控制终端分离 if(pid=fork()) exit(0);//是第一子进程,结束第一子进程 else if(pid< 0) exit(1);//fork失败,退出 //是第二子进程,继续 //第二子进程不再是会话组长 for(i=0;i< NOFILE;++i)//关闭打开的文件描述符 close(i); chdir("/var/log");//改变工作目录到/var/log umask(0);//重设文件创建掩模 return; } |
ipchanged.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | #include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> void init_daemon(void);//守护进程初始化函数 off_t get_filesize() //获得日志文件size { struct stat st; if((stat("/var/log/ipchange.log",&st))==-1) return -1; else return st.st_size; } main() { FILE *fp; time_t t; init_daemon();//初始化为Daemon /////////////////////////////////////////////// char buf[64]; char ip_old[64]; char ip_new[64]; memset (ip_old,0,sizeof(ip_old)); memset (ip_new,0,sizeof(ip_new)); //获得trap目标地址 FILE *trapaddress = NULL; char *p = NULL; char linebuff[64]; memset (linebuff,0,sizeof(linebuff)); char trap_ip[32]; memset (trap_ip,0,sizeof(trap_ip)); if (!(trapaddress = fopen ("/etc/trap_ip.conf","r"))) return -1; while (fgets (linebuff,64,trapaddress)) { if (strstr(linebuff,"TrapDesIPAddress")) break; } p = strchr (linebuff,'='); fclose(trapaddress); strncpy (trap_ip,(p+1),(strlen(p+1)-1)); //printf("%sn",trap_ip); //将snmptrap命令存放在数组trap_common中 char trap_common[128]; memset (trap_common,0,128); strcpy(trap_common,"snmptrap -v 2c -c public "); strcat (trap_common,trap_ip); strcat (trap_common," '' ucdStart sysContact.0 s 'ip change'"); //printf("%sn",trap_common); //////////////////////////////////////////////// while(1) { /////////////////////////// //删除过大日志文件 off_t maxfilesize=200000; off_t filesize=get_filesize(); if (filesize>maxfilesize) { remove("/var/log/ipchange.log"); } ////// if((fp=fopen("ipchange.log","a")) >=0){ t=time(0); FILE *stream; memset (buf,0,sizeof(buf)); stream = popen ("ifconfig |awk '/inet/{print $2}' |awk -F: '{print $2}' |grep [^$] -n | grep ^1 |awk -F: '{print $2}'","r"); fread (buf,sizeof(char),sizeof(buf),stream); pclose (stream); if ( strlen(ip_old)==0 ) { strcpy(ip_old,buf); //printf("%s",ip_old); } else { strcpy(ip_new,buf); if (strcmp(ip_new,ip_old)!=0) { strcpy(ip_old,ip_new); //printf("ip change n"); int trap_result=system (trap_common); fprintf(fp,"%s eth0`s IP has changed, trap_result:%dn",asctime(localtime(&t)), trap_result ); } else { // printf("no changen"); fprintf(fp,"%s eth0`s IP no changen",asctime(localtime(&t)) ); } } fclose(fp); } ////////////////////////// sleep(180); } } |


Everything dynamic and very positively!
i don’t kown