|
楼主 |
发表于 2018-12-26 14:25
|
显示全部楼层
完整的C语言程序
- #include <stdio.h>
- #include <string.h>
- char ip[] = "192.168.89.80";
- char mac[] = "aa:bb:cc:dd:ee:ff";
- char *file_name="dhcpd.conf";
- int main()
- {
- int i = 0;
- char str[1024] = {0};
-
- for(i=0;i<strlen(mac);i++)
- {
- mac[i] = tolower(mac[i]);
- }
-
- /* add start */
- sprintf(str+strlen(str),"\\ host %s\\n",ip);
- sprintf(str+strlen(str)," {\\n");
- sprintf(str+strlen(str)," hardware ethernet %s;\\n",mac);
- sprintf(str+strlen(str)," fixed-address %s;\\n",ip);
- sprintf(str+strlen(str)," }");
-
- printf("%s",str);
-
- char cmd[1024] = {0};
- snprintf(cmd,sizeof(cmd),"sed -i '$i %s' %s",str,file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
-
- /* add end*/
-
- /* delete start */
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/host %s/,+4d' %s",ip,file_name);
- printf("cmd=%s\n",cmd);
- //system(cmd);
- /* delete end */
-
- /* modify vendor start*/
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/vendor-encapsulated-options/d' %s",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
-
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/default-lease-time/i\\ option vendor-encapsulated-options "%s";' %s","aaa_testaa",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
- /*modify vendor end*/
-
- /* modify dns start*/
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/domain-name-servers/d' %s",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
-
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/range/a\\ option domain-name-servers %s, %s;' %s","1.1.1.2","2.2.2.3",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
- /*modify dns end*/
-
-
- /* modify subnet start*/
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/subnet/d' %s",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
-
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/range/isubnet %s netmask %s {' %s","192.168.40.0","255.255.0.0",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
- /*modify subnet end*/
-
-
- /* modify range start*/
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/range/d' %s",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
-
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/subnet/a\\ range %s %s;' %s","192.168.40.3","192.168.40.254",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
- /*modify range end*/
-
-
- /* modify gateway start*/
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/option routers/d' %s",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
-
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/domain-name-servers/a\\ option routers %s;' %s","192.168.40.1",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
- /*modify gateway end*/
-
- /* modify lease-time start*/
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/default-lease-time/d' %s",file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
-
- memset(cmd,0x0,sizeof(cmd));
- snprintf(cmd,sizeof(cmd),"sed -i '/max-lease-time/i\\ default-lease-time %d;' %s",800,file_name);
- printf("cmd=%s\n",cmd);
- system(cmd);
- /*modify lease-time end*/
- return 0;
- }
复制代码 |
|