绿色脚本
#!/bin/bash
CONFIG=/etc/dhcp/dhcpd.conf
CONFIG_option="
subnet 192.168.${1}.0 netmask 255.255.255.0 {
range 192.168.${1}.${2} 192.168.${1}.${3};
option routers 192.168.${1}.1;
option broadcast-address 192.168.${1}.255;
default-lease-time 600;
max-lease-time 7200;
}"
if [ ! -e $CONFIG ];then
yum -y install dhcp &>/dev/null
echo -e "\033[32m The dhcpd install complete! \033[0m"
fi
if [ $# -eq 3 ];then
echo "$CONFIG_option" > $CONFIG
if [ $? -eq 0 ];then
echo -e "\033[32m config is successfully!\033[0m"
systemctl start dhcpd &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[32m The dhcpd is running!\033[0m"
else
echo -e "\033[31m The dhcpd is running failed!\033[0m"
fi
else
echo -e "\033[31m config is failed!\033[0m"
exit
fi
else
echo -e "\033[31m Error,please input location variable! \033[0m"
echo -e "\033[31m syntax:sh $0 13 100 200\033[0m"
echo -e "\033[31m subnet:[1-255]\nrange start:[2-254]\nrange end:[2-254]\nrange start < range end\033[0m"
fi
执行一下
[root@haha ~]# sh DHCP.sh 13 100 150
config is successfully!
The dhcpd is running!
[root@haha ~]# sh DHCP.sh
Error,please input location variable!
syntax:sh DHCP.sh 13 100 200
subnet:[1-255]
range start:[2-254]
range end:[2-254]
range start < range end