#!/bin/bash

dhcpconf=/etc/dhcpd.conf

rpm -q dhcp &> /dev/null
if [ $? -ne 0 ];then
   yum -y install dhcp
fi

grep "subnet" $dhcpconf  &> /dev/null

if [ $? -ne 0 ];then
   echo "ddns-update-style interim;
ignore client-updates;
default-lease-time 21600;
max-lease-time 43200;" > $dhcpconf
fi

while true
do
   read -p "please input Network:" network
   read -p "please input Netmask:" netmask
   read -p "please input Gateway:" gateway
   read -p "please input DNS:" dns
   read -p "please input Start_IP:" start_ip
   read -p "please input End_IP:" end_ip

   grep $network $dhcpconf &> /dev/null

   if [ $? -ne 0 ];then
       echo "subnet $network netmask $netmask {
              option routers                  $gateway;
           option subnet-mask              $netmask;
              option domain-name-servers      $dns;
              range dynamic-bootp $start_ip $end_ip;
       }" >> $dhcpconf

       read -p "continue to do (yes/no)?" yn

       if [ $yn = "no" ]; then
           exit 0
       fi
   fi
done


脚本二

#!/bin/bash
conf="/etc/dhcpd.conf"
rpm -q dhcp &> /dev/null
if [ $? -ne 0 ];then
   yum -y install dhcp
fi


grep "subnet" $conf &> /dev/null
if [ $? -ne 0 ]; then
   echo "
   ddns-update-style interim;
   ignore client-updates;
   default-lease-time 21600;
   max-lease-time 43200;
   " > $conf
fi

read -p "please input network:" network
read -p "please input netmask:" netmask
read -p "please input gateway:" gateway
read -p "please input DNS:" dns
read -p "please input start_ip:" start_ip
read -p "please input end_ip:" end_ip

echo "
subnet $network netmask $netmask {
       option routers                  $gateway;
   option subnet-mask              $netmask;
       option domain-name-servers      $dns;
       range dynamic-bootp $start_ip $end_ip;
}

" >> $conf
service dhcpd restart


脚本三

#!/bin/bash

dhcpconf=/etc/dhcpd.conf

rpm -q dhcp &> /dev/null
if [ $? -ne 0 ];then
   yum -y install dhcp
fi

grep "subnet" $dhcpconf  &> /dev/null

if [ $? -ne 0 ];then
   echo "ddns-update-style interim;
ignore client-updates;
default-lease-time 21600;
max-lease-time 43200;" > $dhcpconf
fi

while true
do
   read -p "please input Network:" network
   read -p "please input Netmask:" netmask
   read -p "please input Gateway:" gateway
   read -p "please input DNS:" dns
   read -p "please input Start_IP:" start_ip
   read -p "please input End_IP:" end_ip

   grep $network $dhcpconf &> /dev/null

   if [ $? -ne 0 ];then
       echo "subnet $network netmask $netmask {
              option routers                  $gateway;
           option subnet-mask              $netmask;
              option domain-name-servers      $dns;
              range dynamic-bootp $start_ip $end_ip;
       }" >> $dhcpconf

       read -p "continue to do (yes/no)?" yn

       if [ $yn = "no" ]; then
           service dhcpd status &> /dev/null
           if [ $? -ne 0 ]; then
               service dhcpd start &> /dev/null
           else
               service dhcpd reload &> /dev/null
           fi
           exit 0
       fi
   fi
done