配置DHCP的shell脚本

这是一个用于配置DHCP服务器的Shell脚本,通过指定参数如子网、网关、范围等更新配置文件,并实现服务重启。脚本包含了权限检查、帮助提示等功能,确保正确配置并执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash


#use config_dhcp --help to get help
#close debug info set DEBUG to NULL


#------------------------------------------------------------------
#parameters
dhcp_conf=/etc/dhcp/dhcpd.conf


SUBNET=
NETMASK=
RANGE_MIN=
RANGE_MAX=
GATEWAY=
BROADCAST=
TFTP_SERVER=
FILENAME=


DEBUG=1


#------------------------------------------------------------------


write_conf()
{
    cat << EOF > ${dhcp_conf}
ddns-update-style interim;
ignore client-updates;
subnet ${SUBNET} netmask ${NETMASK} {
   range ${RANGE_MIN} ${RANGE_MAX};
   option routers ${GATEWAY};
   option subnet-mask ${NETMASK};
   option broadcast-address ${BROADCAST};
   next-server ${TFTP_SERVER};   #指定TFTP服务器的地址
   filename "${FILENAME}";     #指定PXE引导程序的文件名
}
EOF
    echo "write conf ok!"
}


dhcp_reload()
{
    /etc/init.d/dhcpd restart
}


dhcp_config_flow()
{
    write_conf
    dhcp_reload
}


dhcp_config()
{
    if [ x${DEBUG} = 'x' ];then
        dhcp_config_flow > /dev/null 2>&1
    else
        dhcp_config_flow
    fi
}


show_help()
{
    echo -e "Usage: config_dhcp [OPTION] ...
    --subnet\t\tsubnet for dhcp.
    --netmask\t\tnetmask for dhcp.
    --range-min\t\tminmum ip range.
    --range-max\t\tmaxmum ip range.
    --gateway\t\tgate way for dhcp.
    --broadcast\t\tbroadcast for dhcp.
    --tftp-server\ttftp server location.
    --filename\t\tfile on tftp server."
}


privilege_check()
{
    if [ `id -u` -ne 0 ];then
        echo "you should be root to run this script"
        exit 1
    fi
}


parameter_check(){
    while [ $# -gt 0 ]
    do
        case $1 in
            --subnet)
            SUBNET=$2
            shift
            ;;
            --netmask)
            NETMASK=$2
            shift
            ;;
            --range-min)
            RANGE_MIN=$2
            shift
            ;;
            --range-max)
            RANGE_MAX=$2
            shift
            ;;
            --gateway)
            GATEWAY=$2
            shift
            ;;
            --broadcast)
            BROADCAST=$2
            shift
            ;;
            --tftp-server)
            TFTP_SERVER=$2
            shift
            ;;
            --filename)
            FILENAME=$2
            shift
            ;;
            *)
            show_help
            exit 1
            ;;
        esac
        shift
    done
    
    { [ x${SUBNET} != 'x' ] && [ x${NETMASK} != 'x' ] && [ x${RANGE_MIN} != 'x' ] \
    && [ x${RANGE_MAX} != 'x' ] && [ x${GATEWAY} != 'x' ] && [ x${BROADCAST} != 'x' ] \
    && [ x${TFTP_SERVER} != 'x' ] && [ x${FILENAME} != 'x' ]; } || { echo "check your \
    parameter"; show_help; exit 1;}
}
#------------------------------------------------------------------




#
#main
#
parameter_check $@
privilege_check
dhcp_config
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值