UNIX shell獲取IP和修改IP
#!/bin/ksh
#全局變量
#已配置信息的網卡個數
ETHNUM=0
#網卡名稱信息,多個網卡用#符號分隔
ETHS=""
#網卡IP/掩碼/默認網關信息,用:符號分隔,多個網卡用#符號分隔
ETHINFO=""
#網卡名稱信息,用數組存儲
ETHSArray[0]=""
#網卡IP/掩碼/默認網關信息,用數組存儲
ETHINFOArray[0]=""
#修改IP時原IP所在網卡在數組中的索引
ETHINDEX=0
function getIP
{
typeset ethList="";
typeset ethaddr="";
typeset ethmask="";
typeset gateway="";
typeset ethinfo="";
# 獲取所有網卡信息
ethList=`ifconfig -a | grep 'eth' | awk '{print $1}'`
# 循環所有網卡
for eth in ${ethList}
do
# 獲取網卡IP地址
ethaddr=`ifconfig ${eth} | grep 'inet addr:'| awk '{print $2}' | awk -F : '{print $2}'`
# 網卡配置了IP地址
if [ "-$ethaddr" != "-" ]
then
# 獲取網卡掩碼
ethmask=`ifconfig ${eth} | grep 'inet addr:'| awk '{print $4}' | awk -F : '{print $2}'`
# 獲取網卡默認網關
# gateway=`route ${eth}| grep default.*UG.*$eth | awk '{print $2}'`
gateway=`route | grep default.*UG.*$eth | awk '{print $2}'`
# 將該網卡的信息拼成字符串
ethinfo="${ethaddr}:${ethmask}:${gateway}"
ETHSArray[$ETHNUM]="$eth"
ETHINFOArray[$ETHNUM]="$ethinfo"
ETHNUM=`expr $ETHNUM + 1`
# ETHS為空字符串
if [ "-$ETHS" == "-" ]
then
ETHS="${eth}"
else
ETHS="${ETHS}#${eth}"
fi
# ETHINFO為空字符串
if [ "-$ETHINFO" == "-" ]
then
ETHINFO="${ethinfo}"
else
ETHINFO="${ETHINFO}#${ethinfo}"
fi
fi
done
return 0
}
function modifyIP
{
# 保證輸入4個參數
if [ $# -ne 4 ]; then
echo "The parameters is not 4!"
return 1
fi
typeset oldIP=$1
typeset newIP=$2
typeset newmask=$3
typeset newgw=$4
# 獲取當前網卡信息
getIP
# 判斷該原IP是否存在
get_eth_of_IP $oldIP
# 原IP不存在,返回1
if [ $? -ne 0 ]; then
echo "The old IP $oldIP is not exist."
return 1
fi
typeset eth="${ETHSArray[$ETHINDEX]}"
typeset ethinfo="${ETHINFOArray[$ETHINDEX]}"
typeset ethaddr=`echo $ethinfo | awk -F: '{print $1}'`
typeset ethmask=`echo $ethinfo | awk -F: '{print $2}'`
typeset ethgw=`echo $ethinfo | awk -F: '{print $3}'`
# 即時生效方式修改IP
echo "ifconfig $eth $newIP netmask $newmask"
ifconfig $eth $newIP netmask $newmask
if [ "$?" -ne 0 ]; then
echo "Modify old IP $oldIP to new IP $newIP failed."
return 1
fi
# 永久生效方式修改IP
sed "s/$oldIP/$newIP/" /etc/sysconfig/network/ifcfg-$eth > /tmp/ifcfgtemp
sed "s/$ethmask/$newmask/" /tmp/ifcfgtemp > /etc/sysconfig/network/ifcfg-$eth
rm /tmp/ifcfgtemp
#即時生效方式修改網關
route del default
echo "route del default"
route add default gw $newgw dev $eth
echo "route add default gw $newgw dev $eth"
# 永久生效方式修改網關
sed "s/$ethgw/$newgw/" /etc/sysconfig/network/routes > /tmp/routetemp
mv /tmp/routetemp /etc/sysconfig/network/routes
return 0
}
###############################################################
#函數名稱:get_eth_of_IP
#功 能:獲取IP所在網卡的索引
#輸入參數:IP
#返回值:1 沒有設置該IP的網卡,0 設置該IP的網卡所在索引。
# 2 傳入參數錯誤
# ETHINDEX值為索引位置
#
###############################################################
function get_eth_of_IP
{
# 保證輸入一個參數
if [ $# -ne 1 ]; then
echo "The parameters is not 1!"
return 2
fi
typeset oldIP=$1
typeset ethaddr=""
typeset ethinfo=""
typeset index=0
while [ $index -lt ${#ETHSArray[@]} ]
do
ethinfo="${ETHINFOArray[index]}"
ethaddr=`echo $ethinfo | awk -F: '{print $1}'`
if [ "${ethaddr}" == "${oldIP}" ];then
ETHINDEX=$index
return 0
fi
index=`expr $index + 1`
done
return 1
}
if [ $# -eq 0 ];then
getIP
echo "eas_modify_ip_result = [$ETHINFO]"
#echo $ETHINFO
#echo "]"
exit 0
fi
case $1 in
getip)
getIP
echo "eas_modify_ip_result = ["
echo $ETHINFO
echo "]"
exit 0
;;
modify)
modifyIP $2 $3 $4 $5
exit 0
;;
esac
echo "The parameters are invalid."
exit 0
;;
Linux Shell參數替換 http://www.linuxidc.com/Linux/2013-06/85356.htm
Shell for參數 http://www.linuxidc.com/Linux/2013-07/87335.htm
Linux/Unix Shell 參數傳遞到SQL腳本 http://www.linuxidc.com/Linux/2013-03/80568.htm
Shell腳本中參數傳遞方法介紹 http://www.linuxidc.com/Linux/2012-08/69155.htm
Shell腳本傳遞命令行參數 http://www.linuxidc.com/Linux/2012-01/52192.htm