Solved

script to change the ip addresses in the ifcfg files

Posted on 2011-09-29
3
417 Views
Last Modified: 2012-05-12
Does anyone have a bash or ksh script to change the ip addresses in ifcfg-* files
0
Comment
Question by:lolaferrari
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
wesly_chen earned 500 total points
ID: 36818481
chmod  +x change_ip.sh
./change_ip.sh  <New IP>
#!/bin/bash

NEW_IP=$1
IFCFG_FILE=/etc/sysconfig/network-scripts/ifcfg-eth0

# remove the old IP address
sed '/^IPADDR=/d'  $IFCFG_FILE  > /tmp/ifcfg-eth0_$$

# Add new IP address 
echo "IPADDR=$NEW_IP" >>  /tmp/ifcfg-eth0_$$ 

# Backup the old file
/bin/mv  $IFCFG_FILE  ${IFCFG_FILE}.old

/bin/cp -f  /tmp/ifcfg-eth0_$$  $IFCFG_FILE

/bin/rm -f /tmp/ifcfg-eth0_$$

Open in new window

0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36818526
Or make is shorter

perl -i.bak -pe 's/IPADDR=.*/IPADDR=<NEW IP>/g'   /etc/sysconfig/network-scripts/ifcfg-eth0
  where replace <NEW IP> to actual IP like 192.168.0.123
0
 

Author Closing Comment

by:lolaferrari
ID: 36818649
many thanks!!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now