shell-automatic change of Linux server IP

Source: Internet
Author: User

#!/bin/bash
Echo"Enter The old IP:"$1
Echo"Enter The new IP:"$2

if[[ $1==""]];then
Echo"Usage: + $ Enter the new ip:$1"
Echo"Usage: $ Enter the old ip:$2"
Exit0
Fi

I=0
forLineinch' Ls/etc/sysconfig/network-scripts | Grep-e"Ifcfg-em|ifcfg-eth"`
Do
strip= ' cat/etc/sysconfig/network-scripts/$line | Grep-i" $"`
strboot= ' cat/etc/sysconfig/network-scripts/$line | Grep-i"^onboot"| Awk-f'=''{print $}'| Grep-i"Yes"`
if[-N"$strip"-a-n"$strboot"];then
Echo $line
file= $line
Echo $strip
Echo $strboot
ipfile="/etc/sysconfig/network-scripts/$file"
hostnamefile="/etc/sysconfig/network"
Sed-i's/^ipaddr/#IPADDR/g'$ipfile
#sed-I.'s/^netmask/#NETMASK/g'$ipfile
Sed-i's/^gateway/#GATEWAY/g'$ipfile
Sed-i's/^hostname/#HOSTNAME/g'$ipfile
Sed-i's/^hostname/#HOSTNAME/g'$hostnamefile
newip=$2
newgateway=
Newhostname=
Cat >> $ipfile <<eof
Ipaddr="$newip"
gateway="$newgateway"
Eof
Echo"hostname=\ "$newhostname \"">> $hostnamefile
Hostname"$newhostname"
Echo"OK"
Else
Echo $line
Echo $strip
Echo $strboot
Echo"Error"
Fi
Done
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.