DHCP服务器脚本

目录

一、初始化配置

(一)编写脚本

(二)执行脚本

二、DHCP服务器

编写脚本

三、路由配置

编写脚本

四、中继转发


一、初始化配置

(一)编写脚本

复制代码
\[root@localhost \~\]# vim csh.sh
#!/bin/bash
##关闭防火墙
if systemctl status firewalld
then
systemctl disabled --now firewalld
else
echo "防火墙已经关闭"
fi
iptables -F
##关闭SELinux
if \[ \`getenforce\` == 'Disabled' \]
then
echo "SELinux已经关闭"
else
setenforce 0
sed -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 \]\]
then
continue
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' \]
then
read -p "输入IP地址:" ip
read -p "输入子网掩码:" mask
read -p "输入网关:" gw
read -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
else
echo "输入错误"
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
then
    config_dhcp
else
    yum install -y dhcp
    config_dhcp
fi

三、路由配置

编写脚本

复制代码
\[root@localhost \~\]# vim csh.sh
#!/bin/bash
##关闭防火墙
if systemctl status firewalld
then
systemctl disabled --now firewalld
else
echo "防火墙已经关闭"
fi
iptables -F
##关闭SELinux
if \[ \`getenforce\` == 'Disabled' \]
then
echo "SELinux已经关闭"
else
setenforce 0
sed -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 \]\]
then
continue
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' \]
then
read -p "输入IP地址:" ip
read -p "输入子网掩码:" mask
read -p "输入网关:" gw
read -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
else
echo "输入错误"
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

相关推荐
筑梦之路16 分钟前
ubuntu 22.04设置时区和24小时制显示——筑梦之路
linux·运维·ubuntu
西岭千秋雪_20 分钟前
计算机网络学习笔记:TCP流控、拥塞控制
网络·笔记·学习·tcp/ip·计算机网络
XiaoCCCcCCccCcccC21 分钟前
网络层协议 IP 协议介绍 -- IP 协议,网段划分,私有 IP 和 公网 IP,路由
服务器·网络·tcp/ip
程序设计实验室38 分钟前
个人数据保全计划:如何安全地备份数据
linux·个人数据保全计划
www_pp_2 小时前
# Linux:开源的力量与应用
linux
Fanmeang3 小时前
IP路由基础
网络·网络协议·华为·路由·静态路由·路由表·动态路由
(:满天星:)3 小时前
ELK技术栈全面解析与部署方案
linux·运维·服务器·elk·docker·centos
ViatorSun3 小时前
「解析」docker容器起服务,外部访问失败
linux·ubuntu·docker·容器·postman
文慧的科技江湖3 小时前
充电桩运维管理工具系统的**详细功能列表** - 慧知开源充电桩平台
运维·分布式·小程序·开源·充电桩平台·充电桩开源平台
XXXFIRE3 小时前
前端必学:💻Mac + Nginx 部署 Vue3 静态项目
运维·前端