autodhcp.sh
#!/bin/sh
cd /data/autodhcp
rm -rf dhcpd.conf.new
rm -rf wxxdcilo
rm -rf wxxdcipin
rm -rf wxxdc
rm -rf dhcpd.conf.*
date=`date +%Y%m%d-%H:%M`
#curl "https://lcms.51y5.net/api/cmdb/server/searchIpInfo?token=75dc1a38e802840cdce6460bcec6f1e7&page=1&perPage=200000&idc_name=WXXDCA104" >> wxxdc
curl "https://lcms.y5ops.com/api/cmdb/server/searchIpInfo?token=75dc1a38e802840cdce6460bcec6f1e7&page=1&perPage=200000&ip=10.4.*" >> wxxdc
cat wxxdc |grep 10.5. |tr 'a-z' 'A-Z' >> wxxdcilo
sed -i 's/;//g' wxxdcilo
sh ilo.sh wxxdcilo
cat wxxdcilo |awk '{print $2}' |sed 's/10.5./10.4./g' >> wxxdcipin
sh ipin.sh wxxdcipin
cat dhcpd.conf.ipin > dhcpd.conf-$date
cat dhcpd.conf.ilo >> dhcpd.conf-$date
online=`md5sum /etc/dhcp/dhcpd.conf |awk '{print $1}'`
new=`md5sum dhcpd.conf-$date |awk '{print $1}'`
find /data/autodhcp/ -cmin +60 -name "dhcpd.conf-*" |xargs rm -rf
if [ $online = $new ];then
echo "dhcp don't need update"
else
mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf-$date
mv dhcpd.conf-$date /etc/dhcp/dhcpd.conf
systemctl restart dhcpd
if [ $? -eq 0 ];then
echo ok
find /etc/dhcp/ -cmin +60 -name "dhcpd.conf-*" |xargs rm -rf
else
echo failed
fi
fi
rm -rf dhcpd.conf.new
#rm -rf wxxdcilo
#rm -rf wxxdcipin
#rm -rf wxxdc
#rm -rf dhcpd.conf.ilo
#rm -rf dhcpd.conf.ipin
ilo.sh
#/bin/bash
cd /data/autodhcp
list=$1
rm -rf dhcpd.conf.new
duan=`cat $list |awk '{print $2}' |awk -F "." '{print $1"."$2"."$3}' |sort |uniq`
function subnet(){
for i in `echo $duan`
do
cat $list |grep -F $i | while read ipduan
do
ip=`echo $ipduan |awk '{print $2}'`
net=`echo $ip |awk -F "." '{print $1"."$2"."$3}'` #10.2.67
ip4=`echo $ip |awk -F "." '{print $4}'`
echo $net.0 >> subnet.list
done
done
cat subnet.list |sort |uniq >> subnet-list
for i in `cat subnet-list`;do
cp sub sub-ex
net=`echo $i |awk -F "." '{print $1"."$2"."$3}'`
ip4=`echo $i |awk -F "." '{print $4}'`
netgw=`echo $net.254`
ipdsm=`echo $net.200`
ipdbig=`echo $net.240`
sed -i "s/netstart/$i/g" sub-ex
sed -i "s/netgw/$netgw/g" sub-ex
sed -i "s/ipdsm/$ipdsm/g" sub-ex
sed -i "s/ipdbig/$ipdbig/g" sub-ex
cat sub-ex >> sub.conf
rm -rf sub-ex
done
}
function host() {
for i in `echo $duan`
do
cat $list |grep -F $i |while read line
do
cp host host-ex
sn=`echo "$line" |awk '{print $1}'`
ip=`echo "$line" |awk '{print $2}'`
mac=`echo "$line" |awk '{print $3}'`
ip4=`echo $ip |awk -F "." '{print $4}'`
sed -i "s/server-sn/$sn/g" host-ex
sed -i "s/server-mac/$mac/g" host-ex
sed -i "s/server-ip/$ip/g" host-ex
cat host-ex >> host-"$i.254"
rm -rf host-ex
done
done
}
function shared(){
for i in `cat subnet-list |sort |uniq `;do
net=`echo $i |awk -F "." '{print $1"."$2"."$3}'`
ip4=`echo $i |awk -F "." '{print $4}'`
# ipname=`echo $net"."0`
ipname=`echo $net"."254`
netgw=`echo $net.254`
ipdsm=`echo $net.200`
ipdbig=`echo $net.240`
# echo $ipname
cp share share-ex
sed -i "s/netstart/$i/g" share-ex
sed -i "s/netgw/$netgw/g" share-ex
sed -i "s/ipdsm/$ipdsm/g" share-ex
sed -i "s/ipdbig/$ipdbig/g" share-ex
sed -i "s/ipname/$ipname/g " share-ex
sed -i "s/netstart/$i/g" share-ex
sed -i "s/netgw/$ipname/g" share-ex
cat share-ex >> $ipname.shared.conf
rm -rf share-ex
done
}
function gather(){
for i in `cat subnet-list |sort |uniq `;do
net=`echo $i |awk -F "." '{print $1"."$2"."$3}'`
ip4=`echo $i |awk -F "." '{print $4}'`
cat host-$net.254 >> $net.254.shared.conf
echo "}" >> $net.254.shared.conf
cat $net.254.shared.conf >> dhcpd.conf.ilo
done
sed -i "s/dhcpserver/10.4.0.243/g" dhcpd.conf.ilo
}
subnet
shared
host
gather
rm -rf host-*
rm -rf *.shared.conf
rm -rf sub.conf
rm -rf subnet-list
rm -rf subnet.list
ipin.sh
#/bin/bash
list=$1
duan=`cat $list |awk '{print $1}' |awk -F "." '{print $1"."$2"."$3}' |sort |uniq`
function subnet(){
for i in `echo $duan`
do
cat $list |grep -F $i |while read ipduan
do
ip=`echo $ipduan |awk '{print $1}'`
net=`echo $ip |awk -F "." '{print $1"."$2"."$3}'`
ip4=`echo $ip |awk -F "." '{print $4}'`
echo $net.0 >> subnet.list
done
done
cat subnet.list |sort |uniq >> subnet-list
for i in `cat subnet-list`;do
cp sub sub-ex
net=`echo $i |awk -F "." '{print $1"."$2"."$3}'`
ip4=`echo $i |awk -F "." '{print $4}'`
netgw=`echo $net.254`
ipdsm=`echo $net.200`
ipdbig=`echo $net.240`
sed -i "s/netstart/$i/g" sub-ex
sed -i "s/netgw/$netgw/g" sub-ex
sed -i "s/ipdsm/$ipdsm/g" sub-ex
sed -i "s/ipdbig/$ipdbig/g" sub-ex
cat sub-ex >> sub.conf
rm -rf sub-ex
done
}
function host() {
for i in `echo $duan`
do
cat $list |grep -F $i |while read host
do
cp host host-ex
sn=`echo "$host" |awk '{print $2}'`
ip=`echo "$host" |awk '{print $1}'`
mac=`echo "$host" |awk '{print $3}'`
ip4=`echo $ip |awk -F "." '{print $4}'`
sed -i "s/server-sn/$sn/g" host-ex
sed -i "s/server-mac/$mac/g" host-ex
sed -i "s/server-ip/$ip/g" host-ex
cat host-ex >> host-"$i.254"
rm -rf host-ex
done
done
}
function shared(){
for i in `cat subnet-list |sort |uniq `;do
net=`echo $i |awk -F "." '{print $1"."$2"."$3}'`
ip4=`echo $i |awk -F "." '{print $4}'`
ipname=`echo $net"."254`
# echo $ipname
cp share share-ex
sed -i "s/ipname/$ipname/g " share-ex
sed -i "s/netstart/$i/g" share-ex
sed -i "s/netgw/$ipname/g" share-ex
sed -i "s/ipdsm/$ipdsm/g" share-ex
sed -i "s/ipdbig/$ipdbig/g" share-ex
cat share-ex >> $ipname.shared.conf
rm -rf share-ex
done
}
function gather(){
cat header >> dhcpd.conf.ipin
cat sub.conf >> dhcpd.conf.ipin
#for i in `cat subnet-list |sort |uniq `;do
# net=`echo $i |awk -F "." '{print $1"."$2"."$3}'`
# ip4=`echo $i |awk -F "." '{print $4}'`
# if [ $ip4 -eq 0 ];then
# cat host-$net.126 >> $net.126.shared.conf
# echo } >> $net.126.shared.conf
# cat $net.126.shared.conf >> dhcpd.conf.new
# else
# cat host-$net.254 >> $net.254.shared.conf
# echo } >> $net.254.shared.conf
# cat $net.254.shared.conf >> dhcpd.conf.new
# fi
#done
sed -i "s/dhcpserver/10.4.31.29/g" dhcpd.conf.ipin
}
subnet
shared
# host
gather
rm -rf host-*
rm -rf *.shared.conf
rm -rf sub.conf
rm -rf subnet-list
rm -rf subnet.list