編寫腳本實現DHCP服務與DHCP中繼自動化執行 本腳本是在liunx搭建DHCP伺服器以及DHCP中繼伺服器實驗環境下實現的https://www.cnblogs.com/yuzly/p/10539317.html 源碼如下: 1.在dhcp server端執行腳本 2.測試dhcp server ...
編寫腳本實現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