我的番茄炒蛋
生活如此精彩,挑战无处不在!

导航

 
快速搞定DHCP的MAC与IP绑定的dhcpd.conf文件



在Linux下,要实现dhcp的MAC与IP绑定,就需要在/etc/dhcpd.conf文件中为每一个MAC指定一个IP,如果仅仅是在几台计算机组成的网络中,用手工编写一下dhcpd.conf不需要多少时间,但如果是上百台,甚至是上千台,那情况就不同了。

那如何能利用手头的MAC地址与IP地址的对照表来快速生成dhcpd.conf呢?最近因为有这方面的需要,想想也只有编个小程序来实现,因为只是实现一个简单的功能,没有在错误检测上花太大的精力,程序源代码如下:

<php
/********************************************************
* 程序用途:由文本文件生成使用MAC与IP绑定的dhcpd.conf
* 使用方法: (1)请设置下面的环境变量
* (2)把符合格式要求的文件复制到工作目录中
* (3)运行本程序即可生成需要的格式文件
*
* Written by Sideway 2006.06.30 zhangqi357@gmail.com
* 发布协议:GPL
********************************************************/


/***********************************
* 环境变量设置
*
* 此区域可由用户修改成实际的数据
***********************************/


/********************************
* 工作目录,源文件与目标文件存放的目录
********************************/
$workDir = "c:/www/";


/********************************
* 源文件的文件名
* 源文件的格式必须为CSV格式,
* 共三个字段,依次为主机名、MAC地址、IP地址
* 如下:
* 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";


/********************************
* 目标文件的文件名
********************************/
$targetFilename = "dhcpd.conf.txt";


/********************************
* dhcpd.conf文件的头部信息
********************************/
$dhcpHeard1 = "default-lease-time 3600;\n";
$dhcpHeard2 = "max-lease-time 7200;\n";
$dhcpHeard3 = "ddns-update-style none;\n";
$dhcpHeard4 = "option subnet-mask 255.255.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\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\n";
$dhcpHeard13 = "group {\n";


/********************************
* dhcpd.conf文件的尾部信息
********************************/
$dhcpFoot = "}";



/***********************************
* 以下为系统处理部份
*
* 此区域无需修改,除非需要更改程序的功能
***********************************/
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))
{
/***************************************
*
* 每一循环取出源文件的一行进行处理
*
***************************************/
$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); //需去除最后的换行符
$FieldCont = "";
$Line1 = "host $Hostname {\n";
$Line2 = " hardware ethernet $Mac;\n";
$Line3 = " fixed-address $Ip;\n";
$Line4 = "}\n\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";
?>
posted on 2009-10-30 16:04  bluesky  阅读(1781)  评论(0编辑  收藏  举报