Quickly fix the DHCPD. conf file bound to the MAC address and IP address of DHCP.

Source: Internet
Author: User
Quickly fix the DHCPD. conf file bound to the MAC address and IP address of DHCP.

In Linux, to bind a dhcp mac address to an IP address, you need to bind it to/etc/DHCPD. specify an IP address for each Mac in the conf file. If it is only in a network composed of several computers, write DHCPD manually. conf does not need much time, but if there are hundreds or even thousands of machines, the situation will be different.

Then how can we use the MAC address and IP address table at hand to quickly generate DHCPD. conf? Recently, due to this need, you only need to compile a small program to implement it, because it only implements a simple function and does not put too much effort into error detection. The source code of the program is as follows:

<PHP
/*************************************** *****************
* Program purpose: generate a DHCPD. conf file bound to the Mac and IP address from a text file.
* Usage: (1) set the following environment variables.
* (2) copy the files that meet the format requirements to the working directory.
* (3) run this program to generate the required format file.
*
* Written by sideway 2006.06.30 zhangqi357@gmail.com
* Publishing Protocol: GPL
**************************************** ****************/

/***********************************
* Environment variable settings
*
* This region can be changed from user to actual data.
***********************************/

/********************************
* Working directory: directory where the source and target files are stored
********************************/
$ Workdir = "C:/www /";

/********************************
* File name of the source file
* The source file must be in CSV format,
* Three fields are in sequence: Host Name, MAC address, and IP address.
* As follows:
* Station1, 00: 0f: 20: 2f: Ce: 8e, 126.100.151.40
* Station2, 00: 0f: 20: 2f: Ce: 9e, 126.100.151.41
*
********************************/
$ Sourcefilename = "macip.csv ";

/********************************
* Target file name
********************************/
$ Targetfilename = "dhcpd.conf.txt ";

/********************************
* Header information of the DHCPD. conf file
********************************/
$ Dhcpheard1 = "default-lease-time 3600; \ n ";
$ Dhcpheard2 = "Max-lease-time 7200; \ n ";
$ Dhcpheard3 = "ddns-Update-style none; \ n ";
$ Dhcpheard4 = "option subnet-mask limit 255.0; \ n ";
$ Dhcpheard5 = "option broadcast-address 126.100.151.255; \ n ";
$ Dhcpheard6 = "option routers 126.100.151.254; \ n ";
$ Dhcpheard7 = "option domain-name-servers 126.100.151.21; \ n ";
$ Dhcpheard8 = "option domain-name \" xsj.gov.cn \ "; \ n ";
$ Dhcpheard9 = "subnet 126.100.151.0 netmask 255.255.255.0 {\ n ";
$ Dhcpheard10 = "";
$ Dhcpheard11 = "range 126.100.151.41 126.100.151.239; \ n ";
$ Dhcpheard12 = "} \ n ";
$ Dhcpheard13 = "Group {\ n ";

/********************************
* Tail information of the DHCPD. conf file
********************************/
$ Dhcpfoot = "}";

/***********************************
* The following is the system processing part.
*
* This area does not need to be modified unless you need to change the functions of the program.
***********************************/
Echo "processing, please wait ....
";
$ Sourcefile = $ workdir. $ sourcefilename;
$ Targetfile = $ workdir. $ targetfilename;
$ Dhcpheard = $ dhcpheard1. $ dhcpheard2. $ dhcpheard3. $ dhcpheard4. $ dhcpheard5. $ dhcpheard6.
$ Dhcpheard7. $ dhcpheard8. $ dhcpheard9. $ dhcpheard10. $ dhcpheard11. $ dhcpheard12. $ dhcpheard13;
$ Sourcehandle = fopen ($ sourcefile, "R + ");
$ Targethandle = fopen ($ targetfile, "W ");
Fputs ($ targethandle, $ dhcpheard );
While ($ sourcedata = fgets ($ sourcehandle ))
{
/***************************************
*
* One row of the source file is retrieved in each loop for processing.
*
***************************************/
$ Sourcedatalen = strlen ($ sourcedata );
$ Fieldnamenum = 0;
For ($ C = 0; $ C <$ sourcedatalen; $ C ++ ){
$ Currentchar = substr ($ sourcedata, $ C, 1 );
If ("," = $ currentchar)
{
$ Fieldnamenum ++;
Switch ($ fieldnamenum ){
Case 1:
$ Hostname = $ fieldcont;
$ Fieldcont = "";
Break;
Case 2:
$ MAC = $ fieldcont;
$ Fieldcont = "";
Break;
Default:
Echo "error ";
Break;
}
}
Else
{
$ Fieldcont = $ fieldcont. $ currentchar;
}
} // End of "for ($ C = 0; $ C <$ sourcedatelen; $ C ++ )"
$ IP = substr ($ fieldcont, 0, strlen ($ fieldcont)-1); // remove the last line break
$ Fieldcont = "";
$ Line1 = "host $ hostname {\ n ";
$ Line2 = "Hardware Ethernet $ MAC; \ n ";
$ Line3 = "fixed-address $ IP; \ n ";
$ Line4 = "} \ n ";
Fputs ($ targethandle, $ line1. $ line2. $ line3. $ line4 );
} // End of "while ($ sourcedata = fgets ($ sourcehandle ))"
Fputs ($ targethandle, $ dhcpfoot );
Fclose ($ sourcehandle );
Fclose ($ targethandle );
Echo "Finished !!! ";
Echo"
Please open $ workdir find $ targetfilename file ";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.