shell-自动更改LINUX服务器IP

来源:互联网 发布:金融数据终端 编辑:程序博客网 时间:2024/04/29 20:46
#!/bin/bashecho "Enter the old ip:"$1echo "Enter the new ip:"$2if [[ $1 == "" ]];then        echo "Usage: $0 Enter the new ip:$1"        echo "Usage: $0 Enter the old ip:$2"        exit 0fii=0for line in `ls /etc/sysconfig/network-scripts | grep -E "ifcfg-em|ifcfg-eth"`dostrip=`cat /etc/sysconfig/network-scripts/$line |  grep -i "$1"`strboot=`cat /etc/sysconfig/network-scripts/$line |  grep -i "^onboot" | awk -F '=' '{print $2}' | grep -i "yes"`if [ -n "$strip" -a -n "$strboot" ];thenecho $linefile=$lineecho $stripecho $strbootipfile="/etc/sysconfig/network-scripts/$file"hostnamefile="/etc/sysconfig/network"sed -i 's/^IPADDR/#IPADDR/g' $ipfile#sed -i 's/^NETMASK/#NETMASK/g' $ipfilesed -i 's/^GATEWAY/#GATEWAY/g' $ipfilesed -i 's/^HOSTNAME/#HOSTNAME/g' $ipfilesed -i 's/^HOSTNAME/#HOSTNAME/g' $hostnamefilenewip=$2newgateway=newhostname=cat >>$ipfile<<EOFIPADDR="$newip"GATEWAY="$newgateway"EOFecho "HOSTNAME=\"$newhostname\"" >>$hostnamefilehostname "$newhostname"echo "OK"else echo $lineecho $stripecho $strbootecho "error"fidone

0 0
原创粉丝点击