Solved

script to change the ip addresses in the ifcfg files

Posted on 2011-09-29
3
425 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make a Bash alias that takes parameters 5 75
awk file 6 79
Upgrade BIOS / EUFI at Scale 4 56
CentOS 7 Linux for HP DL380 G4 32Bits 7 50
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

821 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