Код: Выделить всё
/*
-------------------
#gcc b545.c -o deos
-------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>
#include <time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#define COUNT_BUFFER 1024
#define SBUF 1024
#define WORK_DIR "/var/log/545"
#define LOG_FILE "/var/log/545/list.log"
#define ERR_FILE "/var/log/545/error.log"
#define PID_FILE "/var/log/545/pid"
#define FWDIR "/var/log/545/ifcfg-"
#define NETIFCOPY "cp -rf /etc/sysconfig/network-scripts/ifcfg-eth0 /home"
#define NETIFCP_1 "cp -rf /etc/sysconfig/network-scripts/ifcfg-"
#define NETIFCP_2 " /home/ifcfg-"
#define NETIFCPBBACK_1 "cp -rf "
#define NETIFCPBBACK_3 " /etc/sysconfig/network-scripts/"
#define RSTNETSRV "service network restart"
#define RSTNETSRV0 "systemctl restart network.servic"
#define RSTNETSRV1 "service networking restart"
#define RSTNETSRV2 "service network restart"
#define RSTNETSRV3 "/etc/init.d/network restart"
#define IFCFG_U1 "/sbin/ifconfig "
#define IFCFG_IPADDR " | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'"
#define IFCFG_MASK " | grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}'"
#define IFCFG_BCAST " | grep 'inet addr:' | cut -d: -f3 | awk '{ print $1}'"
#define STATIC_IP "BOOTPROTO=\"static\""
//#define IFCFG "ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'"
//#define IFCFG2 ""
//#define IFCFG3 "hostname -I"
//#define IFCFG4 "hostname --all-ip-addresses"
//#define IFCFG5 "ip -4 addr show eth0 | grep -oP "(?<=inet ).*(?=/)""
//#define IFCFC_a "ip r l"
//#define IFCFG_S1 "ip -4 addr show "
//#define IFCFG_S3 " | grep -oP \"(?<=inet ).*(?=/)\""
//#define IFCFG_S4 " | grep -oP \"(?<=inet ).*(?=brd)\""
//#define IFCFG6 "hostname -I | awk '{ print $1 }'"
//#define IFCFG7 "/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'"
void err(char *str);
char* getTime();
char* get1hTime();
int writeLog(char msg[128]);
void SetPidFile(char * path_file);
char* g_cmd(char command[128]);
int cmp_s(char *s1, char *s2);
void err(char *str)
{
perror(str);
exit(1);
}
char* getTime()
{
time_t now;
struct tm *ptr;
static char tbuf[64];
bzero(tbuf,64);
time(&now);
ptr = localtime(&now);
strftime(tbuf,64, "%Y-%m-%e %H:%M:%S", ptr);
// strftime(tbuf,64, "%b %d %H", ptr);
return tbuf;
}
char* get1hTime()
{
char *mon;
int i,ln =0;
static char temp[64];
time_t now;
struct tm *ptr;
static char tbuf[64];
bzero(tbuf,64);
time(&now);
ptr = localtime(&now);
strftime(tbuf,64, "%c", ptr);
ln = strlen(tbuf);
bzero(temp,64);
for(i =4;i<ln-9;i++){temp[i-4] = tbuf[i];}
return temp;
}
int writeLog(char msg[512])
{
FILE * pLog;
pLog = fopen(LOG_FILE, "a+");
if(pLog == NULL) {
return 1;
}
char str[512];
bzero(str, 512);
snprintf(str,sizeof str,"%s\t%s\n",getTime(),msg);
fputs(str, pLog);
//fwrite(msg, 1, sizeof(msg), pLog);
fclose(pLog);
return 0;
}
void SetPidFile(char *path_file){
FILE* f;
//char *path_file = PID_FILE;
f = fopen(path_file, "w+");
if (f)
{
fprintf(f, "%u", getpid());
fclose(f);
}
}
char* g_cmd(char command[128])
{
FILE *pcm, *log;
static char bf[SBUF];
int j,lastchar;
//** delete "cd " and run chdir()
char *istr = strstr(command,"cd ");
if(istr != NULL){
int sln= strlen(command);
int nsln=sln-3;
char *cmd; cmd=(char *)malloc(nsln);
for(j=3;j<sln;j++){cmd[j-3]=command[j];}
cmd[nsln]='\0';chdir(cmd);free(cmd);return "0";
};
//**
pcm=popen(command,"r");
//if(pcm== NULL){writeLog("Error Command"); return "1";}
if(pcm== NULL){log = fopen(ERR_FILE, "a+"); writeLog("Error run command");fclose(log); return "1";}
bzero(bf,SBUF);
lastchar = fread(bf,1,SBUF,pcm);
bf[lastchar]='\0';
pclose(pcm);
return bf;
}
int cmp_s(char *s1, char *s2)
{
int t,i;
int c,j;
c =0;
int ln1;
ln1=strlen(s1);
int ln2;
ln2=strlen(s2);
//intf("%d\t%d\n",ln1,ln2);
//puts(s1);
//puts(s2);
for(i=0;i<ln1;i++){
for(j=0;j<ln2;j++){
if(s2[j]==s1[j+i]){
//printf("%c=%c\n",s2[j],s1[j+i]);
c=c+1;
//getchar();
//printf("%d\n",j+i);
}
}
if(c==ln2){return 0;}
c=0;t=ln2+i;
if(t>ln1){ return 1;}
}
//puts("We Here");
return 1;
}
int main(int argc, char* argv[]) {
int status;
int pid;
//cnt =0;
char ccmd[128],flnmo[128],flnmi[128];
char str[1024];
char *ipaddr,*netmask,*brdcast,*tmp;
int m=1,c,a,k,k0,k1,k2,k3;
c=1;
if(argc !=3){
//bzero(inf,2048);
fprintf(stderr,"===============\n\n");
fprintf(stderr," usage: Please Enter name of interface and command restart interface..\n\n\r");
fprintf(stderr," example: deosdeosjj eth0 0 \n\n");
fprintf(stderr," 0 - systemctl restart network.service \n\n");
fprintf(stderr," example: deos eth0 1 \n\n");
fprintf(stderr," 1 - service networking restart \n\n");
fprintf(stderr," example: deos eth0 2 \n\n");
fprintf(stderr," 2 - service network restart \n\n");
fprintf(stderr," example: deos eth0 3 \n\n");
fprintf(stderr," 3 - /etc/init.d/network restart\n\n");
fprintf(stderr," ***** Working directories\n\n");
fprintf(stderr," /home.... ifcfg-eth0 ifcfg-eth0.orig\n\n");
fprintf(stderr," /var/log/545... logs \n\n");
exit(1);
}
//puts(argv[1]);
a =cmp_s(argv[2],"0" );
if(a==0){k=0;}
a =cmp_s(argv[2],"1" );
if(a==0){k=1;}
a =cmp_s(argv[2],"2" );
if(a==0){k=2;}
a =cmp_s(argv[2],"3" );
if(a==0){k=3;}
//if(argv[2]=="1"){puts("1"); }
//a=(int)argv[3]-0;
//printf("%c",a );
//exit(1);
struct stat st = {0};
if(stat(WORK_DIR,&st ) == -1){
mkdir(WORK_DIR,0700);}
bzero(ccmd,128);
snprintf(ccmd,sizeof ccmd,"%s%s%s%s%s%s",NETIFCP_1,argv[1],NETIFCP_2,argv[1],".orig","\0");
//snprintf(ccmd,sizeof ccmd,"%s%s",NETIFCOPY,"\0");
writeLog(g_cmd(ccmd));
bzero(flnmi,128);
bzero(flnmo,128);
snprintf(flnmi,sizeof flnmi,"%s%s%s","/home/ifcfg-",argv[1],"\0");
snprintf(flnmo,sizeof flnmo,"%s%s%s%s","/home/ifcfg-",argv[1],".orig","\0");
//snprintf(flnmi,sizeof flnmi,"%s%s%s",FWDIR,argv[1],"\0");
//snprintf(flnmo,sizeof flnmo,"%s%s%s%s",FWDIR,argv[1],".orig","\0");
FILE *fpo, *fpi ;
fpi = fopen(flnmi,"w+");
fpo = fopen(flnmo,"r");
////////
bzero(str,1024);
//while(fscanf(fpo,"%s",str)!=EOF){
/*
while(fgets(str,1024,fpo)!=NULL){
m = cmp_s(str,"static");
if(m==0){puts("the interface already has static ip address"); fclose(fpo);fclose(fpi); exit(1);}
}
fseek(fpo,0,SEEK_SET);
*/
while(fgets(str,1024,fpo)!=NULL){
printf("%s",str);
//m1 = cmp_s(str,"DHCP");
m = cmp_s(str,"dhcp");
if(m==0){c=0;fprintf(fpi,"%s%s",STATIC_IP,"\n"); }
else{fprintf(fpi,"%s",str);}
bzero(str,1024);
}
if(c==1){puts("the interface already has static ip address"); fclose(fpo);fclose(fpi); exit(1);}
bzero(ccmd,128);
//snprintf(ccmd,sizeof ccmd,"%s%s%s%s",IFCFG_U1,argv[1],IFCFG_U3,"\0");
snprintf(ccmd,sizeof ccmd,"%s%s%s%s",IFCFG_U1,argv[1],IFCFG_IPADDR,"\0");
//puts(ccmd);
ipaddr = g_cmd(ccmd);
writeLog(ipaddr);
//puts(ipaddr);
fprintf(fpi,"%s%s%s","IPADDR=",ipaddr,"\0");
bzero(ccmd,128);
//snprintf(ccmd,sizeof ccmd,"%s%s%s%s",IFCFG_U1,argv[1],IFCFG_U3,"\0");
snprintf(ccmd,sizeof ccmd,"%s%s%s%s",IFCFG_U1,argv[1],IFCFG_MASK,"\0");
//puts(ccmd);
netmask = g_cmd(ccmd);
writeLog(netmask);
//puts(netmask);
fprintf(fpi,"%s%s%s","NETMASK=",netmask,"\0");
bzero(ccmd,128);
//snprintf(ccmd,sizeof ccmd,"%s%s%s%s",IFCFG_U1,argv[1],IFCFG_U3,"\0");
snprintf(ccmd,sizeof ccmd,"%s%s%s%s",IFCFG_U1,argv[1],IFCFG_BCAST,"\0");
//puts(ccmd);
brdcast = g_cmd(ccmd);
writeLog(brdcast);
//puts(brdcast);
fprintf(fpi,"%s%s%s","BROADCAST=",brdcast,"\0");
fprintf(fpi,"%s%s","GATEWAY=\n","\0");
fprintf(fpi,"%s%s","DEFROUTE=NO\n","\0");
/////////
fclose(fpo);
fclose(fpi);
////////////////////
//NETIFCPBBACK_1
//a=(int)argv[2];
//printf("%d",a );
//exit(0);
bzero(ccmd,128);
snprintf(ccmd,sizeof ccmd,"%s%s%s%s",NETIFCPBBACK_1,flnmi,NETIFCPBBACK_3,"\0");
//puts(ccmd);
//TNETSRV
//g_cmd(ccmd);
writeLog(g_cmd(ccmd));
//puts(g_cmd(ccmd));
bzero(ccmd,128);
//snprintf(ccmd,sizeof ccmd,"%ss",RSTNETSRV0,"\0");
switch(k)
{
case 0: snprintf(ccmd,sizeof ccmd,"%s%s",RSTNETSRV0,"\0");tmp = g_cmd(ccmd);writeLog(tmp);break;
case 1: snprintf(ccmd,sizeof ccmd,"%s%s",RSTNETSRV1,"\0");tmp = g_cmd(ccmd);writeLog(tmp);break;
case 2: snprintf(ccmd,sizeof ccmd,"%s%s",RSTNETSRV2,"\0");tmp = g_cmd(ccmd);writeLog(tmp);break;
case 3: snprintf(ccmd,sizeof ccmd,"%s%s",RSTNETSRV3,"\0");tmp = g_cmd(ccmd);writeLog(tmp);break;
default: snprintf(ccmd,sizeof ccmd,"%s%s",RSTNETSRV2,"\0");tmp = g_cmd(ccmd);writeLog(tmp);break;
//case 0: tmp = g_cmd(RSTNETSRV);tmp = g_cmd(ccmd);writeLog(tmp);break;
};
exit(1);
/////////////////////////////////////////////////TEMP
/*
pid_t parpid, sid;
parpid = fork();
if(parpid < 0) {
exit(1);
} else if(parpid != 0) {
exit(0);
}
SetPidFile(PID_FILE);
umask(0);
sid = setsid();
if(sid < 0) {
exit(1);
}
if((chdir("/")) < 0) {
exit(1);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// return de(argv[1]);
return de();
*/
//////////////////////////////////////////////TEMP
//fclose(list);
}