×

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

hqy hqy 发表于2020-06-23 13:14:20 浏览2043 评论0

抢沙发发表评论

由于公司安全策略要求,需要绑定了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


 您阅读本篇文章共花了: 

打赏

本文链接:https://kinber.cn/post/1398.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

群贤毕至

访客