DHCP脚本

交互式的dhcp脚本

#!/bin/bash
#function: DHCP服务部署
#author: zhubinbin  20230426
##############################
#########root判断#########
if
  [  "$USER"  != "root"   ]
then
   echo "错误:非root用户,权限不足!"
  exit  0
fi
############防火墙与高级权限##########
systemctl stop firewalld && systemctl disable firewalld  && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g'  /etc/selinux/config  && echo "关闭selinux"
##安装##
yum -y install dhcp*  $2 > /dev/null
##################判断安装是否成功########################
 if
  [  $?  =  0   ]
then
   echo "DHCP安装成功!"
   else
   echo "DHCP安装失败,即将退出!"
  exit  0
fi
#####定义变量#####
read -p "请输入本机ip:" a
read -p "请输入本机网关:" b
read -p "要分配的ip地址池(前):" d
read -p "要分配的ip地址池(后):" e
##########修改网卡##########
cat > /etc/sysconfig/network-scripts/ifcfg-ens33 <<KEYI
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="c7bb7075-abdd-49d9-b5ad-3aa2b06c6720"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="$a"
NETMASK="255.255.255.0"
GATEWAY="$b"
DNS1="$b"
DNS2=114.114.114.114
KEYI
########重载网卡########
ifdown ens33  && ifup ens33 $2 > /dev/null
#########取ip网络位#############
IP=$(hostname -I | awk '{print $1}' | awk -F '.' '{print $1"."$2"."$3}')
########配置文件#######
cat > /etc/dhcp/dhcpd.conf <<EOF
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200;

# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.

subnet 192.168.122.1 netmask 255.255.255.255 {
}

# This is a very basic subnet declaration.

#subnet 10.254.239.0 netmask 255.255.255.224 {
#  range 10.254.239.10 10.254.239.20;
#  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
#}

# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.

#subnet 10.254.239.32 netmask 255.255.255.224 {
#  range dynamic-bootp 10.254.239.40 10.254.239.60;
#  option broadcast-address 10.254.239.31;
#  option routers rtr-239-32-1.example.org;
#}

# A slightly different configuration for an internal subnet.
subnet $IP.0 netmask 255.255.255.0 {
  range $d $e;
  option domain-name-servers $b,114.114.114.114;
  option domain-name "internal.example.org";
  option routers $b;
  option broadcast-address $IP.255;
  default-lease-time 600;
  max-lease-time 7200;
}

# Hosts which require special configuration options can be listed in
# host statements.   If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.

host passacaglia {
  hardware ethernet 0:0:c0:5d:bd:95;
  filename "vmunix.passacaglia";
  server-name "toccata.fugue.com";
}

# Fixed IP addresses can also be specified for hosts.   These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP.   Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
host fantasia {
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address fantasia.fugue.com;
}

# You can declare a class of clients and then do address allocation
# based on that.   The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.

class "foo" {
  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}

shared-network 224-29 {
  subnet 10.17.224.0 netmask 255.255.255.0 {
    option routers rtr-224.example.org;
  }
  subnet 10.0.29.0 netmask 255.255.255.0 {
    option routers rtr-29.example.org;
  }
  pool {
    allow members of "foo";
    range 10.17.224.10 10.17.224.250;
  }
  pool {
    deny members of "foo";
    range 10.0.29.10 10.0.29.230;
  }
}
EOF
###########开启DHCP############
systemctl  start dhcpd.service
if
  [  $?  =  0   ]
then
   echo "DHCP服务已经开启成功,记住要关闭虚拟机软件的DHCP"
   echo "DHCP服务ip地址分配范围$d-$e"
   else
   echo "DHCP服务开启失败,即将退出!"
  exit  0
fi


 

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值