编写脚本实现DHCP服务与DHCP中继自动化执行
编写脚本实现dhcp服务与dhcp中继自动化执行
本脚本是在liunx搭建dhcp服务器以及dhcp中继服务器实验环境下实现的
源码如下:
#!/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中继服务器:
上一篇: 社会生活中爆笑尬事不少呢!