编写脚本实现DHCP服务与DHCP中继自动化执行

编写脚本实现DHCP服务与DHCP中继自动化执行

本脚本是在liunx搭建DHCP服务器以及DHCP中继服务器实验环境下实现的https://www.cnblogs.com/yuzly/p/10539317.html

源码如下:

#!/bin/bash
#该脚本用于自动化配置DHCP服务器以及DHCP中继
#作者:雨中落叶
#博客:https://www.cnblogs.com/yuzly/
echo "****************************
1.部署DHCP服务器
2.部署网关
3.部署DHCP中继
****************************"
read -p "请输入部署选项:" Num
case $Num in
1)
  #DHCP服务器配置
  #关闭防火墙避免影响实验
  service iptables stop &>/dev/null
  setenforce 0
  #1.获取用户输入的DHCP服务器的相关网络参数
  read -p "请输入DHCP服务器的IP地址:" IP
  read -p "请输入DHCP服务器的子网掩码:" MASK
  read -p "请输入DHCP服务器的网关地址:" GW
  read -p "请输入DHCP服务器的首选DNS地址:" DNS1
  read -p "请输入DHCP服务器的辅助DNS地址:" DNS2
  #2.设置DHCP服务器的网卡IP地址
  ETH=$(ifconfig | grep "^eth" | awk '{print $1}')
  MAC=$(ifconfig | grep "^eth" | awk '{print $5}')
  echo "DEVICE=$ETH
    HWADDR=$MAC
    TYPE=Ethernet  
    ONBOOT=yes
    BOOTPROTO=static 
    IPADDR=$IP
    NETMASK=$MASK
    GATEWAY=$GW
    DNS1=$DNS1
    DNS2=$DNS2" >/etc/sysconfig/network-scripts/ifcfg-$ETH
  #重启网络服务
  #service NetworkManager stop 
  #为了防止下次启动时,该服务重新启动,解决方法: chkconfig NetworkManager off再重启网卡,就可以了,启动网络服务报错然后用ifdown eth1 ifdown eth1报错,只能关闭NetworkManager服务来解决
  service NetworkManager stop
  service network restart
  #3.检测是否安装DHCP服务,配置DHCP服务
  #定义函数 
  dhpool(){
    #配置dhcp地址池1
    echo "******配置DHCP地址池1**********"
    read -p "请输入DHCP作用域网段:" DHNET
    read -p "请输入DHCP作用域网段的子网掩码:" DHMASK
    read -p  "请输入地址池开始IP地址:" DHIP1
    read -p "请输入地址池结束IP地址:" DHIP2
    read -p "请输入DNS:" DHDNS
    read -p "请输入网关地址:" DHGW
    read -p "请输入广播地址:" DHB
    #把dhcp配置模板中部分需要的内容复制到dhcp的配置文件中
    conf=/etc/dhcp/dhcpd.conf 
    grep -v "#" /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample | grep -v "^$" | sed -n '17,25p' >$conf
    #修改dhcp配置文件
    sub=$(grep subnet $conf |awk '{print $2}')
    submask=$(grep subnet $conf |awk '{print $4}')
    range1=$(grep range $conf | awk '{print $2}')
    range2=$(grep range $conf | awk '{print $3}' | awk -F";"  '{print $1}')
    dns=$(grep domain-name-servers $conf |awk '{print $3}'|awk -F";" '{print $1}')
    gw=$(grep routers $conf | awk '{print $3}' | awk -F";" '{print $1}') 
    broadcast=$(grep broadcast-address $conf | awk '{print $3}'|awk -F";" '{print $1}') 
    #sed -i 是替换字符串
    sed -i "s/$sub/$DHNET/g" $conf
    sed -i "s/$submask/$DHMASK/g" $conf
    sed -i "s/$range1/$DHIP1/g" $conf
    sed -i "s/$range2/$DHIP2/g" $conf
    sed -i "s/$dns/$DHDNS/g" $conf
    sed -i "s/$gw/$DHGW/g" $conf
    sed -i "s/$broadcast/$DHB/g" $conf
    #配置dncp地址池2
    echo "******配置DHCP地址池2*********"
    read -p "请输入DHCP作用域网段:" dhNET
    read -p "请输入DHCP作用域网段的子网掩码:" dhMASK
    read -p  "请输入地址池开始IP地址:" dhIP1
    read -p "请输入地址池结束IP地址:" dhIP2
    read -p "请输入DNS:" dhDNS
    read -p "请输入网关地址:" dhGW
    read -p "请输入广播地址:" dhB
    #把dhcp配置模板中部分需要的内容复制到dhcp的配置文件中
    conf=/etc/dhcp/dhcpd.conf 
    grep -v "#" /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample | grep -v "^$" | sed -n '17,25p' >>$conf
    #修改dhcp配置文件
    sub1=$(grep subnet $conf |awk '{print $2}' | sed -n 2p)
    submask1=$(grep subnet $conf |awk '{print $4}'| sed -n 2p)
    range11=$(grep range $conf | awk '{print $2}'| sed -n 2p)
    range22=$(grep range $conf | awk '{print $3}'| sed -n 2p | awk -F";"  '{print $1}')
    dns1=$(grep domain-name-servers $conf |awk '{print $3}'|awk -F";" '{print $1}'|sed -n 2p)
    gw1=$(grep routers $conf | awk '{print $3}' | awk -F";" '{print $1}'| sed -n 2p) 
    broadcast1=$(grep broadcast-address $conf | awk '{print $3}'|awk -F";" '{print $1}'|sed -n 2p) 
    #sed -i 是替换字符串
    sed -i "10,18s/$sub1/$dhNET/g" $conf
    sed -i "10,18s/$submask1/$dhMASK/g" $conf
    sed -i "10,18s/$range11/$dhIP1/g" $conf
    sed -i "10,18s/$range22/$dhIP2/g" $conf
    sed -i "10,18s/$dns1/$dhDNS/g" $conf
    sed -i "10,18s/$gw1/$dhGW/g" $conf
    sed -i "10,18s/$broadcast1/$dhB/g" $conf
    echo "********************************************"
    read -p  "是否给指定主机分配指定IP(y/n):" zhiding
    #注意=两边各有一个空格,这是unix shell的要求
    if [ $zhiding = "y" ]
    then
      read -p  "请输入要指定的主机的MAC地址:" zMAC
      read -p  "请输入要给主机指定分配的IP(IP必须在地址池内):" zIP
      echo "host joe {
                    hardware ethernet $zMAC;
                    fixed-address $zIP;
                  }" >>$conf
    fi
    #启动dhcp服务
    service dhcpd start
    }
  n=$(rpm -qa | grep dhcp |wc -l)
  if [ $n -eq 2 ]
  then
    #调用函数  
    dhpool
  else
    #挂载光盘,开始安装dhcp服务 
    echo "当前电脑没有安装dhcp服务,开始安装....."
    mount /dev/sr0 /mnt &>/dev/null
    rpm -ivh /mnt/Packages/dhcp-4.1.1-38.P1.el6.x86_64.rpm &>/dev/null
    echo "dhcp服务安装完成!" 
    #调用函数
    dhpool
  fi  
;;
2)
  #网关服务器配置
  service iptables stop &>/dev/null
  setenforce 0 
  #1.获取用户输入的网关服务器的相关网络参数
  ETH1=$(ifconfig | grep "^eth" |awk '{print $1}'|sed -n 1p)
  ETH2=$(ifconfig | grep "^eth" |awk '{print $1}'|sed -n 2p)
  GWMAC1=$(ifconfig |grep "^eth" |sed -n 1p |awk '{print $5}')
  GWMAC2=$(ifconfig |grep "^eth" |sed -n 2p |awk '{print $5}')
  
  read -p "请输入网关服务器的$ETH1接口IP地址:" GWIP1
  read -p "请输入网关服务器的$ETH1接口IP地址的子网掩码:" GWMASK1
  read -p "请输入网关服务器的$ETH1接口首选DNS地址:" GWDNS1
  read -p "请输入网关服务器的$ETH1接口辅助DNS地址:" GWDNS2
  
  read -p "请输入网关服务器的$ETH2接口IP地址:" GWIP2
  read -p "请输入网关服务器的$ETH2接口IP地址的子网掩码:" GWMASK2
  read -p "请输入网关服务器的$ETH2接口首选DNS地址:"  GWDNS11
  read -p "请输入网关服务器的$ETH2接口辅助DNS地址:" GWDNS22
  #设置网关服务器的IP地址
  echo "DEVICE=$ETH1
  HWADDR=$GWMAC1
  TYPE=Ethernet  
  ONBOOT=yes
  BOOTPROTO=static 
  IPADDR=$GWIP1
  NETMASK=$GWMASK1
  DNS1=$GWDNS1
  DNS2=$GWDNS2" >/etc/sysconfig/network-scripts/ifcfg-$ETH1 
  
  echo "DEVICE=$ETH2
  HWADDR=$GWMAC2
  TYPE=Ethernet  
  ONBOOT=yes
  BOOTPROTO=static 
  IPADDR=$GWIP2
  NETMASK=$GWMASK2
  DNS1=$GWDNS11
  DNS2=$GWDNS22" >/etc/sysconfig/network-scripts/ifcfg-$ETH2
  #重启网络服务
  service NetworkManager stop
  service network restart
  #开启路由转发功能,在内核中配置,然后sysctl -p使内核配置生效
  sed -i "s/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g" /etc/sysctl.conf
  sysctl -p &>/dev/null
;;
3)
  #DHCP中继服务器配置
  service iptables stop >/dev/null
  setenforce 0
  #1.获取用户输入的DHCP中继服务器的相关网络参数  
   DHRETH=$(ifconfig | grep "^eth" | awk '{print $1}')
   DHRMAC=$(ifconfig | grep "^eth" | awk '{print $5}')
  read -p "请输入DHCP中继服务器的IP地址:" DHRIP
  read -p "请输入DHCP中继服务器的子网掩码:" DHRMASK
  read -p "请输入DHCP中继服务器的网关地址:" DHRGW
  read -p "请输入DHCP中继服务器的首选DNS地址:" DHRDNS1
  read -p "请输入DHCP中继服务器的辅助DNS地址:" DHRDNS2
  #设置DHCP中继服务器的IP地址
  echo "DEVICE=$DHRETH
    HWADDR=$DHRMAC
    TYPE=Ethernet  
    ONBOOT=yes
    BOOTPROTO=static 
    IPADDR=$DHRIP
    NETMASK=$DHRMASK
    GATEWAY=$DHRGW
    DNS1=$DHRDNS1
    DNS2=$DHRDNS2" >/etc/sysconfig/network-scripts/ifcfg-$DHRETH
  #重启网络服务
  service NetworkManager stop
  service network restart
  #DHCP中继配置
  n1=$(rpm -qa | grep dhcp |wc -l)
  if [ $n1 -eq 2 ]
  then
  read -p "请输入要中继的dhcp服务器的IP地址:" dhcrelayip
  sed -i "s/INTERFACES=\"\"/INTERFACES=\"$DHRETH\"/g" /etc/sysconfig/dhcrelay
  sed -i "s/DHCPSERVERS=\"\"/DHCPSERVERS=\"$dhcrelayip\"/g" /etc/sysconfig/dhcrelay 
  else
    echo "当前电脑没有安装dhcp服务,安装中......" 
    mount /dev/sr0 /mnt &>/dev/null
    rpm -ivh /mnt/Packages/dhcp-4.1.1-38.P1.el6.x86_64.rpm &>/dev/null 
    echo "dhcp服务安装完成!"
    read -p "请输入要中继的dhcp服务器的IP地址:" dhcrelayip
    sed -i "s/INTERFACES=\"\"/INTERFACES=\"$DHRETH\"/g" /etc/sysconfig/dhcrelay
    sed -i "s/DHCPSERVERS=\"\"/DHCPSERVERS=\"$dhcrelayip\"/g" /etc/sysconfig/dhcrelay 
  fi  
  #启动dhcp中继服务
  service dhcrelay start
;;
*)
 echo "输入错误,请输入相应的数字!"
;;
esac

1.在dhcp server端执行脚本

 

2.测试dhcp server是否搭建成功,当然也可以看dhcp配置文件看看脚本有没有写进去,注意这里设置了指定分配IP给特定主机,下图可以看到没有从起始IP开始分配,而是分配到了特定的IP

3. 在网关服务器上布置网关配置   #记得开启路由转发功能

4.查看是否配置成功 

5. 在DHCP中继服务器上配置 

6.测试DHCP中继是否配置成功

 

注意:

本次实验为例避免不必要的因素干扰实验,需要关闭防火墙,service iptables stop    关闭linux系统安全措施 setenforce 0

本次实验可能会出现的问题,redhat系统可能会出现重启网络服务(service network start),重启失败,提示“激活连接失败”,通过ifdown、ifup或者ifconfig ethx down ifconfig ethx up 还是报错,提示激活连接失败,这时可能是因为network服务与NetworkManager服务可能存在冲突,关闭NetworkManager服务即可(service NetworkManager stop)

 

------------------------------------------------------------------------------------------------------------------

liunx搭建DHCP服务器以及DHCP中继服务器:https://www.cnblogs.com/yuzly/p/10539317.html

posted @ 2019-03-17 16:13  雨中落叶  阅读(1185)  评论(0编辑  收藏  举报