作為一名Linux SA,日常運(yùn)維中很多地方都會用到腳本,而服務(wù)器的ip一般采用靜態(tài)ip或者M(jìn)AC綁定,當(dāng)然后者比較操作起來相對繁瑣,而前者我們可以設(shè)置主機(jī)名、ip信息、網(wǎng)關(guān)等配置。修改成特定的主機(jī)名在維護(hù)和管理方面也比較方便。如下腳本用途為:修改ip和主機(jī)名等相關(guān)信息,可以根據(jù)實(shí)際需求修改,舉一反三!
#!/bin/sh
#auto Change ip netmask gateway scripts
#wugk 2012-12-17
cat EOF
++++++++自動(dòng)修改ip和主機(jī)名等相關(guān)信息+++++++++
ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0
HOSTS=/etc/hosts
NETWORK=/etc/sysconfig/network
DIR=/data/backup/`date +%Y%m%d`
NETMASK=255.255.255.0
+++++++++-------------------------+++++++++++
EOF
#Define Path 定義變量,可以根據(jù)實(shí)際情況修改
ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0
HOSTS=/etc/hosts
NETWORK=/etc/sysconfig/network
DIR=/data/backup/`date +%Y%m%d`
NETMASK=255.255.255.0
echo "================================================"
echo
#定義change_ip函數(shù)
function Change_ip ()
{
#判斷備份目錄是否存在,中括號前后都有空格,!嘆號在shell表示相反的意思#
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
echo "Now Change ip address ,Doing Backup Interface eth0"
cp $ETHCONF $DIR
grep "dhcp" $ETHCONF
#如下$?用來判斷上一次操作的狀態(tài),為0,表示上一次操作狀態(tài)正確或者成功#
if
[ $? -eq 0 ];then
#read -p 交互輸入變量IPADDR,注冒號后有空格,sed -i 修改配置文件#
read -p "Please insert ip Address:" IPADDR
sed -i 's/dhcp/static/g' $ETHCONF
#awk -F. 意思是以.號為分隔域,打印前三列#
echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF
echo "This IP address Change success !"
else
echo -n "This $ETHCONF is static exist ,please ensure Change Yes or NO":
read i
fi
if
[ "$i" == "y" -o "$i" == "yes" ];then
read -p "Please insert ip Address:" IPADDR
count=(`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'`)
#定義數(shù)組, ${#count[@]}代表獲取變量值總個(gè)數(shù)#
A=${#count[@]}
#while條件語句判斷,個(gè)數(shù)是否正確,不正確循環(huán)提示輸入,也可以用[0-9]來判斷ip#
while
[ "$A" -ne "4" ]
do
read -p "Please re Inster ip Address,example 192.168.0.11 ip": IPADDR
count=(`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'`)
A=${#count[@]}
done
#sed -e 可以連續(xù)修改多個(gè)參數(shù)#
sed -i -e 's/^IPADDR/#IPADDR/g' -e 's/^NETMASK/#NETMASK/g' -e 's/^GATEWAY/#GATEWAY/g' $ETHCONF
#echo -e \n為連續(xù)追加內(nèi)容,并自動(dòng)換行#
echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF
echo "This IP address Change success !"
else
echo "This $ETHCONF static exist,please exit"
exit $?
fi
}
#定義hosts函數(shù)
############function hosts##############
function Change_hosts ()
{
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
cp $HOSTS $DIR
read -p "Please insert ip address": IPADDR
host=`echo $IPADDR|sed 's/\./-/g'`
cat $HOSTS |grep 127.0.0.1 |grep "$host"
if
[ $? -ne 0 ];then
sed -i "s/127.0.0.1/127.0.0.1 $host/g" $HOSTS
echo "This hosts change success "
else
echo "This $host IS Exist .........."
fi
}
###########fuction network###############
#定義network函數(shù)
function Change_network ()
{
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
cp $NETWORK $DIR
read -p "Please insert ip address": IPADDR
host=`echo $IPADDR|sed 's/\./-/g'`
grep "$host" $NETWORK
if
[ $? -ne 0 ];then
sed -i "s/^HOSTNAME/#HOSTNAME/g" $NETWORK
echo "NETWORK=$host" >>$NETWORK
else
echo "This $host IS Exist .........."
fi
}
#PS3一般為菜單提示信息#
PS3="Please Select ip or hosts Menu":
#select為菜單選擇命令,格式為select $var in ..command.. do .... done
select i in "Change_ip" "Change_hosts" "Change_network"
do
#case 方式,一般用于多種條件下的判斷
case $i in
Change_ip )
Change_ip
;;
Change_hosts )
Change_hosts
;;
Change_network )
Change_network
;;
*)
echo
echo "Please Insert $0: Change_ip(1)|Change_hosts(2)|Change_network(3)"
echo
;;
esac
done