Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2633759
  • 博文数量: 333
  • 博客积分: 4817
  • 博客等级: 上校
  • 技术积分: 4413
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-28 10:51
文章分类

全部博文(333)

文章存档

2017年(20)

2016年(57)

2015年(27)

2014年(20)

2013年(21)

2012年(164)

2011年(24)

分类: LINUX

2016-06-08 16:26:15

本脚本主要用于修改IP地址为静态!
#!/bin/bash
#
#Target: Auto Chmod IP Information!
#Date: 2015-05-10
#Author: Jacken
#QQ:654001593
#QQ Group:170544180
#Version: 1.0
#Only allow define variable
shopt -s -o nounset
#Define Variable
ETHCONFIG='/etc/sysconfig/network-scripts/ifcfg-eth1'
HOSTS='/etc/hosts'
NETWORK='/etc/sysconfig/network'
BAKDIR="/data/backup/`date +%Y%m%d`"
NETMASK='255.255.255.0'
GATEWAY='192.168.1.254'
DNS1='8.8.8.8'
################################################################################
####################################Define Function#############################
################################################################################
function CHANGE_IP(){
#Create Directory
if [ ! -d $BAKDIR ];then
mkdir -p $BAKDIR
fi
#Prompt Information
#Backup Network File
cp $ETHCONFIG $BAKDIR/`date +%Y%m%d`.${ETHCONFIG:37}$$ &&
echo -e "\e[32mNow Change Ip Address, Backup Interface ${ETHCONFIG:37} to $BAKDIR Done\e[0m"
#Judge Get Ip Mode
if `grep -q  -i  'dhcp' $ETHCONFIG` ;then
#Change Get Ip Mode
sed -i 's/dhcp/static/Ig' $ETHCONFIG
sed -i 's/^DNS/#DNS/Ig'  $ETHCONFIG
echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=$GATEWAY\nDNS1=$DNS1" >> $ETHCONFIG
        echo -e '\e[32mIP Change Success!\e[0m'
else
#Warning Information
echo -e "\e[31mThe Network alreday is static,Please ensure YES or NO: "
read i
                #Again Confirm
if [ "$i" == "y" -o "$i" == "yes" -o "$i" == "YES" ];then
#Comment Old Configure
sed -i -e 's/^IPADDR/#IPADDR/g' -e 's/^NETMASK/#NETMASK/g' -e 's/^GATEWAY/#GATEWAY/g' -e 's/^DNS/#DNS/g' $ETHCONFIG
echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=$GATEWAY\nDNS1=$DNS1" >> $ETHCONFIG
echo -e "\e[32mThe Ip Address is $IPADDR ,Change Success!\e[0m"
else
#Exit
echo -e "\e[31mThis $ETHCONFIG static exist, exiting...\e[0m"
fi
fi
}
################################################################################
#Check Ip Address
function CHECK_IP(){
read -p "Please Input Ip address: " IPADDR
#Test IP Format
echo $IPADDR|grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" > /dev/null 2>&1
num=$?
if [ $num -ne 0 ];then
echo -e "\e[31mPlease Check IP Format exiting....\e[0m"
exit 2
else
#Get Part Of Ip
a=`echo $IPADDR|awk -F. '{print $1}'`
b=`echo $IPADDR|awk -F. '{print $2}'`
c=`echo $IPADDR|awk -F. '{print $3}'`
d=`echo $IPADDR|awk -F. '{print $4}'`
if [ $a -gt 255 ] ||  [ $a -le 0 ];then
echo -e "\e[31mPlease Check IP Format exiting...\e[0m"
exit 2
fi
if [ $b -gt 255 ] || [ $b -lt 0 ];then
echo -e "\e[31mPlease Check IP Format exiting...\e[0m"
exit 2
fi
if [ $c -gt 255 ] || [ $c -lt 0 ];then
echo -e "\e[31mPlease Check IP Format exiting...\e[0m"
exit 2
fi
if [ $d -ge 255 ] || [ $d -le 0 ];then
echo -e "\e[31mPlease Check IP Format exiting...\e[0m"
exit 2
fi
export $IPADDR
fi
}
#Define PS3
PS3="Please Select Your Choose:"
select i in "Change IP And DNS For ${ETHCONFIG:37}" "Show ${ETHCONFIG:37} Config File"  "Restart Device ${ETHCONFIG:37}" "Show ${ETHCONFIG:37} Status"  "Exit"
do
CHOOSE=$REPLY
case $CHOOSE in
1)
CHECK_IP
CHANGE_IP
;;
2)
cat $ETHCONFIG
;;
3)
ifdown ${ETHCONFIG:37} > /dev/null 2>&1 && ifup ${ETHCONFIG:37} > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -e "\e[32mRestart Device ${ETHCONFIG:37} Success.\e[0m"
else
echo -e "\e[31mRestart Device ${ETHCONFIG:37} Failure.\e[0m"
fi
;;
4)
ifconfig ${ETHCONFIG:37}
;;
5)
echo -e '\e[32mByeBye!\e[0m'
exit
;;
*)
echo -e '\e[31mError Number,Please Input Again!'
esac
done
演示:

阅读(1108) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~