温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

auto_change_ip.sh

发布时间:2020-03-01 22:16:14 来源:网络 阅读:250 作者:vmzhang 栏目:开发技术

#!/bin/bash
#2019年11月6日14:04:34
#by author andy
IP_STATUS=cat /etc/sysconfig/network-scripts/ifcfg-eth0 | awk '/BOOTPROTO/ {print $0}'
IP=ifconfig eth0 | awk '/broadcast/ {print $0}'| awk -F' ' '{print $2}'
IP_PATH="/etc/sysconfig/network-scripts/ifcfg-eth0"
if [ $IP_STATUS == "BOOTPROTO=static" ]; then
echo "The $IP is static,Please input ip address if your want to change it;Don't change it Please enter "Ctrl+c" to EXIT.."

while  true
 read -p "Please input your ip address:" Address
 count=(`echo "$Address" | grep -v "Aa-Zz" | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | awk -F. '{print $1,$2,$3,$4}' `)
        IP1=${count[0]}
    IP2=${count[1]}
    IP3=${count[2]}
    IP4=${count[3]}
do
    if [[ $IP1 -eq 10  &&  $IP2 -eq 0 && $IP3 -eq 0 &&  $IP4 -ge 10 && $IP4 -le 20 ]] ;then
            echo "$IP1 First  is right "
            echo "$IP2 Second is right "
            echo "$IP3 Third  is right "
            echo "$IP4 Fourth is right "    
        echo "It is chenging the IP ,Wait a moment... "
        sed -i '/IPADDR/s/'${IP}'/'${Address}'/g' $IP_PATH
        if [ $? == 0 ]; then
                /etc/init.d/network restart
            echo "IP already chenge to $Address"
        else    
            echo "has a error"
            exit 
        fi
        exit 
    else
            echo "IP first   number must be 10,Please change!"
            echo "IP Second  number must be 0,Please change!"
            echo "IP Third   number must be 0,Please change!"
            echo "IP Fourth  number must be 10-20,Please change!"
    fi
done

elif [ $IP_STATUS == "BOOTPROTO=dhcp" ]; then
echo "The $IP is dhcp,Please input unmber if your want to change it;Don't change it Please enter "Ctrl+c" to EXIT.."
to_static(){
while true
read -p "Please input your ip address:" Address
count=(echo "$Address" | grep -v "Aa-Zz" | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | awk -F. '{print $1,$2,$3,$4}')
IP1=${count[0]}
IP2=${count[1]}
IP3=${count[2]}
IP4=${count[3]}
do
if [[ $IP1 -eq 10 && $IP2 -eq 0 && $IP3 -eq 0 && $IP4 -ge 10 && $IP4 -le 20 ]] ;then
echo "$IP1 First is right "
echo "$IP2 Second is right "
echo "It is chenging the IP ,Wait a moment... "
sed -i '/BOOTPROTO/s/BOOTPROTO=dhcp/BOOTPROTO=static/g' $IP_PATH
sed -i '/NETMASK/s/NETMASK=/NETMASK=255.255.255.0/g' $IP_PATH
sed -i '/GATEWAY/s/GATEWAY=/GATEWAY=10.0.0.2/g' $IP_PATH
sed -i '/IPADDR/s/IPADDR=/IPADDR='${Address}'/g' $IP_PATH
if [ $? == 0 ]; then
/etc/init.d/network restart
echo "IP already chenge to $Address"
else
echo "has a error"
exit
fi
exit
else
echo "IP first number must be 10,Please change!"
echo "IP Second number must be 0,Please change!"
echo "IP Third number must be 0,Please change!"
echo "IP Fourth number must be 10-20,Please change!"
fi
exit
done
}
$i
PS3="Please select your number:"
select i in to_static nothing_go
do
case $i in
to_static)
to_static
;;
nothing_go)
exit 1
exit
esac
done

fi

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI