Solved

script to change the ip addresses in the ifcfg files

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Squid Connection Pools 3 70
Rate limit for DNS queries 7 75
000webhost.com default error log 1 28
how to print sub line with  sed /awk /perl in bash file 11 26
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
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,…
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.

831 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