欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > DHCP服务器脚本

DHCP服务器脚本

2025/6/19 2:52:08 来源:https://blog.csdn.net/LPH3119/article/details/148723480  浏览:    关键词:DHCP服务器脚本

目录

一、初始化配置

(一)编写脚本

(二)执行脚本

二、DHCP服务器

编写脚本

三、路由配置

编写脚本

四、中继转发


一、初始化配置

(一)编写脚本

[root@localhost ~]# vim csh.sh
#!/bin/bash
##关闭防火墙
if systemctl status firewalld
thensystemctl disabled --now firewalld
elseecho "防火墙已经关闭"
fi
iptables -F
##关闭SELinux
if [ `getenforce` == 'Disabled' ]
thenecho "SELinux已经关闭"
elsesetenforce 0sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
fi
##配置IP地址
nics=`ip a | awk -F: '/ens/{print $2}' | grep -v "^$" | tr -d ' '`
echo -e "当前系统中可供配置的网卡有:\n$nics"
while true
do
​
read -p "请输入要配置的网卡名称:" nic
if ! [[ $nics =~ $nic ]]
thencontinue
fi
read -p "请输入配置网络参数的方式(dhcp|static):" tp
if [ $tp == 'dhcp' ]
then
echo "TYPE=Ethernet
BOOTPROTO=$tp
NAME=$nic
DEVICE=$nic
ONBOOT=yes"> /etc/sysconfig/network-scripts/ifcfg-$nic
ifdown $nic ; ifup $nic
elif [ $tp == 'static' ]
thenread -p "输入IP地址:" ipread -p "输入子网掩码:" maskread -p "输入网关:" gwread -p "输入dns:" dns
echo "TYPE=Ethernet
BOOTPROTO=static
NAME=$nic
DEVICE=$nic
ONBOOT=yes
IPADDR=$ip
PREFIX=$mask
GATEWAY=$gw
DNS1=$dns" > /etc/sysconfig/network-scripts/ifcfg-$nic
ifdown $nic ; ifup $nic
elseecho "输入错误"exit
fi
done
​

(二)执行脚本

二、DHCP服务器

编写脚本

#!/bin/bash
config_dhcp(){
echo "subnet 192.168.100.0 netmask 255.255.255.0 {range 192.168.100.2 192.168.100.253;option domain-name-servers 192.168.100.1;option domain-name "internal.example.org";option routers 192.168.100.254;option broadcast-address 192.168.100.255;default-lease-time 600;max-lease-time 7200;
}       
subnet 192.168.200.0 netmask 255.255.255.0 { range 192.168.200.2 192.168.100.253;option domain-name-servers 192.168.100.1;option domain-name "internal.example.org";option routers 192.168.200.254; 
option broadcast-address 192.168.200.255; 
default-lease-time 600; max-lease-time 7200; }       
host fantasia {hardware ethernet 08:00:07:26:c0:a5;fixed-address 192.168.100.100;
}" > /etc/dhcp/dhcpd.conf
systemctl enable --now dhcpd
}
if  ! rpm -q dhcp
thenconfig_dhcp
elseyum install -y dhcpconfig_dhcp
fi

三、路由配置

编写脚本

[root@localhost ~]# vim csh.sh
#!/bin/bash
##关闭防火墙
if systemctl status firewalld
thensystemctl disabled --now firewalld
elseecho "防火墙已经关闭"
fi
iptables -F
##关闭SELinux
if [ `getenforce` == 'Disabled' ]
thenecho "SELinux已经关闭"
elsesetenforce 0sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
fi
##配置IP地址
nics=`ip a | awk -F: '/ens/{print $2}' | grep -v "^$" | tr -d ' '`
echo -e "当前系统中可供配置的网卡有:\n$nics"
while true
do
​
read -p "请输入要配置的网卡名称:" nic
if ! [[ $nics =~ $nic ]]
thencontinue
fi
read -p "请输入配置网络参数的方式(dhcp|static):" tp
if [ $tp == 'dhcp' ]
then
echo "TYPE=Ethernet
BOOTPROTO=$tp
NAME=$nic
DEVICE=$nic
ONBOOT=yes"> /etc/sysconfig/network-scripts/ifcfg-$nic
ifdown $nic ; ifup $nic
elif [ $tp == 'static' ]
thenread -p "输入IP地址:" ipread -p "输入子网掩码:" maskread -p "输入网关:" gwread -p "输入dns:" dns
echo "TYPE=Ethernet
BOOTPROTO=static
NAME=$nic
DEVICE=$nic
ONBOOT=yes
IPADDR=$ip
PREFIX=$mask  > /etc/sysconfig/network-scripts/ifcfg-$nic
ifdown $nic ; ifup $nic
elseecho "输入错误"exit
fi
done

路由网卡配置:

ens33:

IPADDR=192.168.100.254

PREFIX=24

ens34:

IPADDR=192.168.200.254

PREFIX=24

四、中继转发

         echo "net.ipv4.ip_forword = 1" > /etc/sysctl.conf

            sysctl-p

           dhcrelay 192.168.100.1

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词