📄 writedhcpc.c
字号:
#include<sys/types.h>#include<stdio.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include<stdlib.h>#define PATH "/config/www/" main(){ char mac[20];char ip[20];char mask[20];char gateway[20];char buf[160];char dns1[20];char dns2[20];char ntpserver[150];strcpy(mac,"AA:11:12:13:14:15");strcpy(ip,"192.168.1.56");strcpy(mask,"255.255.255.0");strcpy(gateway,"192.168.1.1");strcpy(dns1,"61.235.70.98");strcpy(dns2,"211.98.4.1");strcpy(ntpserver,"61.129.42.44");/********************read the config file*********************************************/ FILE *netfpr; char filename_net[100]; strcpy(filename_net,PATH); strcat(filename_net,"network.conf"); //read MAC info if ( (netfpr=fopen(filename_net,"r"))==NULL ) {printf("can not open network.conf to read!<br>\n"); } while(fscanf(netfpr,"MACADDR=%s\n",mac)!=EOF) { fscanf(netfpr,"%*[^\n]%*c"); if (strlen(mac)!=0) break; } fclose(netfpr); //read IP info if ( (netfpr=fopen(filename_net,"r"))==NULL ) {printf("can not open network.conf to read!<br>\n"); } while(fscanf(netfpr,"IPADDR=%s\n",ip)!=EOF) { fscanf(netfpr,"%*[^\n]%*c"); if (strlen(ip)!=0) break; } fclose(netfpr); //read mask info if ( (netfpr=fopen(filename_net,"r"))==NULL ) {printf("can not open network.conf to read!<br>\n"); } while(fscanf(netfpr,"NETMASK=%s\n",mask)!=EOF) {fscanf(netfpr,"%*[^\n]%*c"); if (strlen(mask)!=0) break; } fclose(netfpr); //read gateway info if ( (netfpr=fopen(filename_net,"r"))==NULL ) {printf("can not open network.conf to read!<br>\n"); } while(fscanf(netfpr,"GATEWAY=%s\n",gateway)!=EOF) {fscanf(netfpr,"%*[^\n]%*c"); if (strlen(gateway)!=0) break; } fclose(netfpr); //read dns1 info if ( (netfpr=fopen(filename_net,"r"))==NULL ) {printf("can not open network.conf to read!<br>\n"); } while(fscanf(netfpr,"DNS1=%s\n",dns1)!=EOF) {fscanf(netfpr,"%*[^\n]%*c"); if (strlen(dns1)!=0) break; } fclose(netfpr); //read dns2 info if ( (netfpr=fopen(filename_net,"r"))==NULL ) {printf("can not open network.conf to read!<br>\n"); } while(fscanf(netfpr,"DNS2=%s\n",dns2)!=EOF) {fscanf(netfpr,"%*[^\n]%*c"); if (strlen(dns2)!=0) break; } //read ntpserver info if ( (netfpr=fopen(filename_net,"r"))==NULL ) {printf("can not open network.conf to read!<br>\n"); } while(fscanf(netfpr,"NTPSERVER=%s\n",ntpserver)!=EOF) {fscanf(netfpr,"%*[^\n]%*c"); if (strlen(dns2)!=0) break; } fclose(netfpr); //***/********************read the config file*********************************************///read dhcpc temp file,get the ip and dns FILE *dhcpcfp; char filename_udhcpc[100]; strcpy(filename_udhcpc,PATH); strcat(filename_udhcpc,"udhcpc.txt"); if ((dhcpcfp= fopen(filename_udhcpc, "r")) == NULL) { printf("Error in open configure file.<br>\n"); exit(1); };char head1[20];char head2[20];while(fscanf(dhcpcfp,"%s",head1)!=EOF) { //printf("head1=%s\n",head1); if (strcmp(head1,"Lease")==0) //scan Lease { fscanf(dhcpcfp,"%s",head2); if (strcmp(head2,"of")==0) {fscanf(dhcpcfp,"%s",ip); fscanf(dhcpcfp,"%*[^\n]%*c"); } else fscanf(dhcpcfp,"%*[^\n]%*c"); } else if (strcmp(head1,"adding")==0) //scan adding { fscanf(dhcpcfp,"%s",head2); if (strcmp(head2,"dns")==0) {fscanf(dhcpcfp,"%s",dns1); fscanf(dhcpcfp,"%*[^\n]%*c"); fscanf(dhcpcfp,"%s",head1);fscanf(dhcpcfp,"%s",head2);fscanf(dhcpcfp,"%s",dns2); fscanf(dhcpcfp,"%*[^\n]%*c"); } else fscanf(dhcpcfp,"%*[^\n]%*c"); } else fscanf(dhcpcfp,"%*[^\n]%*c"); //jump} fclose(dhcpcfp);printf("mac=%s\n",mac);printf("ip=%s\n",ip);printf("mask=%s\n",mask);printf("dns1=%s\n",dns1);printf("dns2=%s\n",dns2);//write configure file network.conf FILE *netfpw; if ( !(netfpw=fopen(filename_net,"w+")) ) {printf("can not open network.conf to write!<br>\n"); } fprintf(netfpw,"MACADDR=%s\n",mac); fprintf(netfpw,"IPADDR=%s\n",ip); fprintf(netfpw,"NETMASK=%s\n",mask); fprintf(netfpw,"GATEWAY=%s\n",gateway); fprintf(netfpw,"DNS1=%s\n",dns1); fprintf(netfpw,"DNS2=%s\n",dns2); fprintf(netfpw,"NTPSERVER=%s\n",ntpserver); fprintf(netfpw,"DHCPC=1\n"); fprintf(netfpw,"DHCPD=0\n"); fclose(netfpw); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -