红联Linux门户
Linux帮助

Linux上DHCP服务器自动绑定IP和MAC地址shell脚本

发布时间:2016-10-10 14:41:43来源:linux网站作者:gcogle
由于公司安全策略要求,需要绑定了MAC地址的机器才能联网,每次新增主机都要手动绑定IP和MAC地址。非常麻烦,所以自动绑定脚本应运而生,这是根据我们公司编写的shell脚本,大婶们见笑了,请多多指教。
 
##################代码开始##################
 
#!/bin/bash  
list=(`sed -rn '/([0−9|a−z|A−Z]+:)5[0−9|a−z|A−Z]+/p' tail2000.log | \  
awk  '{print $8" "$9}'|sed -e 's/[(|)]/ /g'|sort -rn|uniq`)  
count=${#list[@]}  
path=/home/dhcpd.conf  
string_133="range 10.34.133.44 10.34.133.44;"  
string_134="range 10.34.134.21 10.34.135.240;"  
string_135="range 10.34.135.21 10.34.135.240;"  
string_129="range 10.34.129.21 10.34.135.240;"  
i=0  
while [ $i -lt $count ]  
do  
num=`echo ${list[$i]:6:3}`  
grep -Ewq "${list[$i]}|${list[$i+1]}" $path  
if [ $? -ne 0 ];then  
case $num in  
129)  
NO=`date +%N`  
hosts=`echo "host CN${NO:1:5}{hardware ethernet ${list[$i]};  fixed-address ${list[$i+1]};}"`  
sed -i "/$string_129/a '\t'$hosts" $path  
sed -ir "s/'.'/\t/g" $path;;  
133)  
NO=`date +%N`  
hosts=`echo "host CN${NO:1:5}{hardware ethernet ${list[$i]};  fixed-address ${list[$i+1]};}"`  
sed -i "/$string_133/a '\t'$hosts" $path  
sed -ir "s/'.'/\t/g" $path;;  
134)  
NO=`date +%N`  
hosts=`echo "host CN${NO:1:5}{hardware ethernet ${list[$i]};  fixed-address ${list[$i+1]};}"`  
sed -i "/$string_134/a '\t'$hosts" $path  
sed -ir "s/'.'/\t/g" $path;;  
135)  
NO=`date +%N`  
hosts=`echo "host CN${NO:1:5}{hardware ethernet ${list[$i]};  fixed-address ${list[$i+1]};}"`  
sed -i "/$string_135/a '\t'$hosts" $path  
sed -ir "s/'.'/\t/g" $path;;  
esac  
else  
echo ${list[$i+1]} is exist!  
fi  
i=$(($i+2))  
done
 
##################代码结束##################
 
本文永久更新地址:http://www.linuxdiyf.com/linux/24905.html