用shell写一个简单DHCP配置脚本

时间:2022-12-06 14:54:55

轩轩写的这个小脚本,主要是可以进行对dhcp服务的安装、简单配置、开启、关闭/查看状态等情况

使用呢非常简单,按照步骤进行准确的设置就可以啦

#!/bin/bash
yum -y install dhcp
fun() 
{
  input=""
  output=$1
  while [ -z $input ]
        do
        read -p "$output:" input
        done
        echo $input
}
subnet=$( fun "请输入网络地址")
netmask=$( fun "请输入子网掩码")
range1=$( fun "请输入选取IP地址范围起始地址")
range2=$( fun "请输入选区IP地址范围结束地址")
gateway=$( fun "请输入网关")
echo "subnet $subnet netmask $netmask {
  range $range1 $range2;
  option domain-name-servers 8.8.8.8, 8.8.4.4;
  option routers $gateway;
  default-lease-time 600;
  max-lease-time 7200;
}
" > dhcpd.conf
mv -f dhcpd.conf /etc/dhcp/
echo '#!/bin/bash
read -p"        开启防火墙:    s
        关闭防火墙:    p
        查询防火墙状态:a
        :" i
case $i in
    start|s)
    systemctl start dhcpd.service
    echo "启动DHCP服务"
    ;;
    stop|p)
    systemctl stop dhcpd.service
    echo "关闭DHCP服务"
    ;;
    status|a)
    systemctl status dhcpd.service
    echo "查看DHCP服务状态"
    ;;
    *)
    echo "请输入正确选项..."
    sh dhcp1.sh
    ;;
esac
' > dhcp1.sh

read -p "请选择您要操作的选项:
        开启防火墙:    s
        关闭防火墙:    p
        查询防火墙状态:a
        :" i
case $i in
    start|s)
    systemctl start dhcpd.service
    echo "启动DHCP服务"
    ;;
    stop|p)
    systemctl stop dhcpd.service
    echo "关闭DHCP服务"
    ;;
    status|a)
    systemctl status dhcpd.service
    echo "查看DHCP服务状态"
    ;;
    *)
    echo "请输入正确选项..."
    sh dhcp1.sh
    ;;
esac