首页 > 代码库 > DHCP自动化脚本
DHCP自动化脚本
#!/bin/bash
#
dhcp="/mnt/Server/dhcp-3.0.5-23.el5.i386.rpm"
file=/etc/sysconfig/network-scripts/ifcfg-eth0
grep dhcp $file
if [ $? -eq 0 ]
then
#设置IP
read -p "请输入IP 地址": ip
read -p "请输入子网掩码": net
read -p "请输入网关地址": ga
sed -i ‘s/BOOTPROTO=.*/BOOTPROTO=static/‘ $file
echo IPADDR="$ip" >> $file
echo GATEWAY=$ga >> $file
echo NETMASK=$net >> $file
service network restart
else
#设置IP
read -p "请输入IP 地址": ip
read -p "请输入子网掩码": net
read -p "请输入网关地址": ga
sed -i ‘s/^IPADDR.*$/IPADDR=‘$ip‘/‘ $file
sed -i ‘s/^BOOTPROTO=.*/BOOTPROTO=static/‘ $file
sed -i ‘s/^NETMASK.*$/NETMASK=‘$net‘/‘ $file
sed -i ‘s/^GATEWAY.*$/GATEWAY=‘$ga‘/‘ $file
service network restart
fi
rpm -q dhcp &> /dev/null
if [ $? -eq 1 ]
then
echo "正在挂载光盘"
if [ ! -d /mnt/Server ]
then
umount /dev/cdrom &> /dev/null
mount /dev/cdrom /mnt &> /dev/null
echo "正在安装服务"
rpm -ivh $dhcp &> /dev/null
cp -p /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
sed -i "4 s/192.168.0.0/192.168.1.0/" /etc/dhcpd.conf
sed -i "4 s/255.255.255.0/$net/" /etc/dhcpd.conf
sed -i "7 s/192.168.0.1/$ga/" /etc/dhcpd.conf
sed -i "12 s/192.168.1.1/$ip/" /etc/dhcpd.conf
sed -i "21 s/192.168.0.128/192.168.1.100/" /etc/dhcpd.conf
sed -i "21 s/192.168.0.254/192.168.1.254/" /etc/dhcpd.conf
service dhcpd restart
chkconfig dhcpd on
else
echo "光盘已挂载"
rpm -ivh $dhcp &> /dev/null
cp -p /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
sed -i ‘4 s/192.168.0.0/192.168.1.0/‘ /etc/dhcpd.conf
sed -i "4 s/255.255.255.0/$net/" /etc/dhcpd.conf
sed -i "7 s/192.168.0.1/$ga/" /etc/dhcpd.conf
sed -i "12 s/192.168.1.1/$ip/" /etc/dhcpd.conf
sed -i ‘21 s/192.168.0.128/192.168.1.100/‘ /etc/dhcpd.conf
sed -i ‘21 s/192.168.0.254/192.168.1.254/‘ /etc/dhcpd.conf
service dhcpd restart
chkconfig dhcpd on
fi
else
echo "服务已安装"
fi
本文出自 “LinuxCBB” 博客,请务必保留此出处http://linuxcbb.blog.51cto.com/9434555/1559331
DHCP自动化脚本