Thursday, October 16, 2014

Changing ipaddress and gateway of a host on Redhat 6.5

Changing ipaddress and gateway of a host on Redhat 6.5
# cat ipdata.txt
lxserv01:172.20.0.5:255.255.255.0:172.20.0.1:192.168.210.50:255.255.255.192:192.168.210.65
lxserv02:172.20.0.2:255.255.255.0:172.20.0.1:192.168.210.51:255.255.255.192:192.168.210.65
lxserv03:172.20.0.3:255.255.255.0:172.20.0.1:192.168.210.52:255.255.255.192:192.168.210.65
lxserv04:172.20.0.4:255.255.255.0:172.20.0.1:192.168.210.53:255.255.255.192:192.168.210.65


# cat iphosts.sh
#!/bin/sh
hname=`hostname`
#############################################
# hname=`cat ipdata.txt | awk -F: '{print $1}'`
# oldip=`grep $hname "ipdata.txt"|cut -d":" -f2`
oldip=`cat ipdata.txt | grep $hname | cut -d":" -f2`
oldsubnet=`grep $hname "ipdata.txt"|cut -d":" -f3`
#oldgateway=`grep $hname "ipdata.txt"|cut -d":" -f4`
oldgateway=`cat ipdata.txt | grep $hname | cut -d":" -f4`
newip=`grep $hname "ipdata.txt"|cut -d":" -f5`
newsubnet=`grep $hname "ipdata.txt"|cut -d":" -f6`
newgateway=`grep $hname "ipdata.txt"|cut -d":" -f7`
####
sed -i "s/$oldip/$newip/g" /etc/sysconfig/network-scripts/ifcfg-bond0
sed -i "s/$oldsubnet/newsubnet/g" /etc/sysconfig/network-scripts/ifcfg-bond0
sed -i "s/$oldgateway/newgateway/g" /etc/sysconfig/network-scripts/ifcfg-bond0
sed -i "s/$oldgateway/newgateway/g" /etc/sysconfig/network
echo
echo "Old ip info listed below, please verify \n"
echo "$hname   :::  $oldip   :::  $oldsubnet   :::  $oldgateway"
echo
echo "New ip info is listed below, please verify"
echo "$newip   ::: $newsubnet   ::: $newgateway"

No comments:

Post a Comment