For loop that will replace the ifcfg data.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop that will replace the ifcfg data.
# 1  
Old 03-08-2018
For loop that will replace the ifcfg data.

Change IP to multiple servers based on a database file. Source ip file is not incremental.

# Source host
#cat hosts
Code:
server1
server2
server3
server4
server5
server6

# SOURCE file
#cat ip
Code:
Host                  IP             Netmask               GW
server1        192.0.0.1        255.255.255.0        192.0.0.0
server2        192.0.0.2        255.255.255.0        192.0.0.0
server3        192.0.0.3        255.255.255.0        192.0.0.0
server4        192.0.0.4        255.255.255.0        192.0.0.0
server5        192.0.0.5        255.255.255.0        192.0.0.0
server6        192.0.0.6        255.255.255.0        192.0.0.0
server7        192.0.0.7        255.255.255.0        192.0.0.0
server8        192.0.0.8        255.255.255.0        192.0.0.0

# file to be replace from server1
Code:
DEVICE=eth0
BOOTPROTO=dhcp
#HWADDR=
IPV6INIT=no
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
#UUID=
USERCTL=no
#IPADDR=10.1.1.16
#NETMASK=255.255.254.0

#File from server 2
Code:
DEVICE=eth0
BOOTPROTO=dhcp
#HWADDR=
IPV6INIT=no
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
#UUID=
USERCTL=no
#IPADDR=10.8.6.6
#NETMASK=255.255.250.0
#GATEWAY=10.0.0.0

Expected Output:
Server1:
Code:
DEVICE=eth0
BOOTPROTO=none
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.0.0.1
NETMASK=55.255.255.0
GATEWAY=192.0.0.0

Server2:
Code:
DEVICE=eth0
BOOTPROTO=none
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.0.0.2
NETMASK=55.255.255.0
GATEWAY=192.0.0.0

Code:
for ip in `cat ip` ;do while read ip net gw
do

Moderator's Comments:
Mod Comment Please use CODE tags for data as well as required by forum rules!

Last edited by RudiC; 03-08-2018 at 12:27 PM.. Reason: Added CODE tags.
# 2  
Old 03-08-2018
For starters, try:
Code:
while read host ip net gw
do
  ....
done < ip


or if you want to skip the first line that contains a header:

Code:
{
  read
  while read host ip net gw
  do
    ....
  done
} < ip


Last edited by Scrutinizer; 03-08-2018 at 10:51 PM.. Reason: Added host field, thanks RudiC
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-08-2018
How about this:

Code:
hosts=( $(cat hosts) )
for host in ${hosts[@]}
do
    grep "^$host" ip | {
        read server ip net gw
        sed '/NETMASK/d;/IPADDR/d;/GATEWAY/d;/^#/d' $host
        echo "IPADDR=$ip"
        echo "NETMASK=$net"
        echo "GATEWAY=$gw"
    } > ${host}_new
done

This reads your server1 file and produces server1_new with commented lines and existing NETMASK, IPADDR and GATEWAY lines removed and appends new lines with values from ip file.
Its not very efficient as it re-reads ip for every host.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace first occurrence of a string in while loop

####Solved#### Hello, My aim is to replace searched string with incremented value under ubuntu 16.04. Example: aasasasas 9030 31wwo weopwoep weerasas 9030 ew31wo ieopwoep bbqqqsas 9030 ew3swo ieeopwoep ccsaqpas 9030 ewiro o2opwoep Expected: aasasasas 9030 31wwo weopwoep weerasas 9031... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Loop through file and replace with sed

Hello all, I need some help please. I got file1 with names. foo bar foo bar foo bar foo bar foo bar and I got file2 with some text some text some text #KEYWORD some text some text some text (3 Replies)
Discussion started by: stinkefisch
3 Replies

3. Red Hat

Not showing ifcfg eth0....redhat enterprise Linux 6.2

Hello, I'm using dell inspiron 14R laptop and I installed Redhat enterprise 6.2 on this. After successfully installation i did not found any network interface on this. when i'm trying to load/add qeth driver . The driver is not found showing this message. Bash: lsmod | grep qeth Bash:... (4 Replies)
Discussion started by: dearsumon
4 Replies

4. Shell Programming and Scripting

Speeding up search and replace in a for loop

Hello, I am using sed in a for loop to replace text in a 100MB file. I have about 55,000 entries to convert in a csv file with two entries per line. The following script works to search file.txt for the first field from conversion.csv and then replace it with the second field. While it works fine,... (15 Replies)
Discussion started by: pbluescript
15 Replies

5. Shell Programming and Scripting

search replace with loop and variable

Hi, could anyone help me with this, tried several times but still not getting it right or having enough grounding to do it outside of javascript: Using awk or sed or bash: need to go through a text file using a for next loop, replacing substrings in the file that consist of a potentially multi... (3 Replies)
Discussion started by: wind
3 Replies

6. Shell Programming and Scripting

Replace file name extension in loop

Hi! I have this shell script that I need to finish. Currently I need to fix one line to make it work. I need to change a file extension. See this code, is pretty simple. #!/bin/sh # Extensions OLD_EXT=.flv NEW_EXT=.mp4 COUNT_FILES=$(ls -l *$OLD_EXT | grep ^- | wc -l) if ; then ... (8 Replies)
Discussion started by: pulsorock
8 Replies

7. Shell Programming and Scripting

Replace data of one column with data on other file corresponding to transaction ID matched

Hi All, I have two files one of which having some mobile numbers and corresponding value whose sample content as follows: 9058629605,8.0 9122828964,30.0 And in second file complete details of all mobile numbers and sample content as follows and delimeter used is comma(,): ... (8 Replies)
Discussion started by: poweroflinux
8 Replies

8. UNIX for Dummies Questions & Answers

replace stdout line in loop

Hi, as part of a script i have a loop that goes though a set of files. This loop can take some time, so i'd like to show the user that something is happening by showing them the filename being worked on. I could create a long list of file names in the terminal using echo or print... (0 Replies)
Discussion started by: nickrick
0 Replies

9. Red Hat

ifcfg-eth0 and eth1 is N/A

when i finish installed rhel 4.4 to my hp dl585 box, ifcfg-eth0 and ifcfg-eth1 is not available.when i ifconfig -a,i can not see the eth0 and eth1, only lo0 and si0. Unlike to the other server i found eth0 and eth1 files in /etc/sysconfig/network-scripts/. what will i do? (2 Replies)
Discussion started by: kenshinhimura
2 Replies

10. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies
Login or Register to Ask a Question