配置DHCP的shell脚本

#!/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

你可能感兴趣的:(shell)