Linux:自动获取静态IP地址,清空iptable,修改selinux脚本

自动获取静态IP地址,清空iptable,修改selinux脚本


环境:VMware

平台:centos6.8全新

功能:

  1)应用ifconfig -a,route -n,cat /etc/resolv.conf命令获取IP,掩码,网关,dns并自动填写到/etc/sysconfig/network-scripts/ifcfg-eth0.

  2)清空iptables规则.

  3)关闭selinux.

应用:适用全新系统  

#!/bin/bash
#本脚本是自动获取静态IP地址,并修改selin和清空iptables.
#日期:--
#编写:WK
#定义检查函数,错误和正确会显示红字并闪烁.
check() {
if [ $? != ]
then
echo -e '\e[1;5;31m ERROR \e[0m'
exit
else
echo -e '\e[1;5;31m OK \e[0m'
fi
}
#释放IP并查看IP地址
dhclient -r
ifconfig -a >/dev/null
dhclient
check
#截取IP,网关,掩码,dns段.
ip=`ifconfig -a |grep -A1 'eth0' |sed '1d'|awk -F ':' '{print $2}'|awk '{print $1}'`
gateway=`route -n |awk '{print $2}'|sed '1,3d'`
netmask=`route -n |awk '{print $3}'|sed '1,2d'|sed 2d`
dns1=`cat /etc/resolv.conf |awk '{print $2}'|sed 1d|sed 2d`
dns2=`cat /etc/resolv.conf |awk '{print $2}'|sed 1d|sed 1d`
eth0="/etc/sysconfig/network-scripts/ifcfg-eth0"
#修改ONBOOT和BOOTPROTO
sed -i 's/ONBOOT\=no/ONBOOT\=yes/' $eth0
sed -i 's/BOOTPROTO\=dhcp/BOOTPROTO\=static/' $eth0
check
#判断地址段是否存在,若存在删除并重新填入,若不存在直接填写(写的比较繁琐)
if `grep -q 'IPADDR' $eth0 `
then
sed -i '/^IP/d' $eth0
echo "IPADDR=$ip" >>$eth0
else
echo "IPADDR=$ip" >>$eth0
fi
check
if `grep -q 'GATEWAY' $eth0`
then
sed -i '/^GATE/d' $eth0
echo "GATEWAY=$gateway" >>$eth0
else
echo "GATEWAY=$gateway" >>$eth0
fi
check
if `grep -q 'NETMASK' $eth0`
then
sed -i '/^NET/d' $eth0
echo "NETMASK=$netmask" >>$eth0
else
echo "NETMASK=$netmask" >>$eth0
fi
check
if `grep -q 'DNS1' $eth0`
then
sed -i '/^DNS1/d' $eth0
echo "DNS1=$dns1" >>$eth0
else
echo "DNS1=$dns1" >>$eth0
fi
check
if `grep -q 'DNS2' $eth0`
then
sed -i '/^DNS2/d' $eth0
echo "DNS2=$dns2" >>$eth0
else
echo "DNS2=$dns2" >>$eth0
fi
check
#重启网卡
service network restart
check
#关闭selinux
sed -i 's/SELINUX\=enforcing/SELINUX\=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "enforcing" ]
then
setenforce
fi
check
#保存并清空iptables
iptables-save > /etc/sysconfig/iptables_`date +%s`
iptables -F
service iptables save
check
#检查网络是否通畅
ping -c www.qq.com
check
echo "-------------------------------->"
echo -e "\033[4;33mYour IP address is-->$ip\033[0m"

执行结果图

Linux:自动获取静态IP地址,清空iptable,修改selinux脚本

上一篇:CSS3 字体


下一篇:谈谈HashMap与HashTable