案例一
关闭防火墙
关闭SELinux上下文
配置ip地址
#!/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=getenforce/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
echo "输入正确"
else
echo "输入错误"
exit 1
fi
done
read -p "请输入配置网络参数的方式(dhcp|static):" tp
tp=$1
case "$tp" in
"dhcp")
echo "TYPE=Ethernet
BOOTPROTO=$tp
NAME=$nic
DEVICE=$nic
ONBOOT=yes"> /etc/sysconfig/network-scripts/ifcfg-$nic
ifdown $nic ; ifup $nic
;;
"static")
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
;;
*)
echo "无效参数,请重新输入"
;;
esac
案例二
关闭防火墙
关闭SELinux上下文
配置ip地址
#!/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 ns1.internal.example.org;
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;
}
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