LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2010, 05:36 PM   #1
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Rep: Reputation: 15
Read from Input File and Append to Multiple Files


Sorry for the long message, I wanted to be as clear as possible. I have to add hundreds of new servers to hundreds of configuration files in Nagios. Here is a sample structure:
------------------------------------------------------------------------
servers.txt has:

SERVER3
SERVER4
SERVER5
------------------------------------------------------------------------
service-template.txt has:

define service{
use generic-service
host_name $1
service_description Description1
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!sshd
}
######################################################################
define service{
use generic-service
host_name $1
service_description Description2
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!ntpd
}
######################################################################
define service{
use generic-service
host_name $1
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!xinetd
}
------------------------------------------------------------------------
service-servers-sshd.cfg has:
########################### service to check - SSHD

define service{
use generic-service
host_name SERVER1
service_description Description1
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!sshd
}

define service{
use generic-service
host_name SERVER2
service_description Description1
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!sshd
}

service-servers-ntpd.cfg has:
########################### service to check - NTPD

define service{
use generic-service
host_name SERVER1
service_description Description2
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!ntpd
}

define service{
use generic-service
host_name SERVER2
service_description Description2
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!ntpd
}

service-servers-xinetd.cfg has:
########################### service to check - XINETD

define service{
use generic-service
host_name SERVER1
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!xinetd
}

define service{
use generic-service
host_name SERVER2
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_procs!1:1!xinetd
}
------------------------------------------------------------------------
Now I need to add SERVER3, SERVER4 and SERVER5 to each of the service-servers-*.cfg files, following the same structure as the above. Blank line between the "defined services". I knew how to do this when I was adding ALL servers to a single configuration file. I would then merge that file to my final CFG file. Now, my issue is all those service files are separate and I want to append the new servers at the end of each file.

Hope I made it clear, cause right now I am really confused on how to do this!
 
Old 11-22-2010, 08:16 PM   #2
barriehie
Member
 
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
What languages are available for completing this task?
 
Old 11-22-2010, 08:23 PM   #3
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
Well, I usually use bash but perl will do too.
 
Old 11-22-2010, 08:47 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
How about something like:
Code:
#!/bin/bash

declare SSHD NTPD XINETD

SSHD=0
NTPD=1
XINETD=2

IFS='#' entries+=($(awk 'BEGIN{RS="#+\n"}NR==FNR{split($0,servers);next}{tmp="xinetd";if(/sshd/)tmp="sshd";if(/ntpd/)tmp="ntpd";for(x in servers)printf "\n%s",gensub(/\$1/,servers[x],"1");printf "#"}' servers.txt service-template.txt))

while read -r FILE
do
	case $FILE in
	    *sshd.cfg)echo -n ${entries[SSHD]} >> $FILE;;
	    *ntpd.cfg)echo -n ${entries[NTPD]} >> $FILE;;
	    *xinetd.cfg)echo -n ${entries[XINETD]} >> $FILE;;
	    *)echo "Not a required file to update";;
	esac
done< <(find <path to files> -type f -name '*cfg')
 
1 members found this post helpful.
Old 11-22-2010, 09:00 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
You could read in the server names, in a loop, and set a variable. Then create the config file using a HERE document, which will replace variable with it's value, when creating the config file:
Here is an example:
Code:
:> configfile
for server in SERV1, SERV2, SERV3; do cat <<END >>configfile 
use generic-server
host_name $server
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
END
done
This produces this "configfile":
Code:
use generic-server
host_name SERV1,
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
use generic-server
host_name SERV2,
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
use generic-server
host_name SERV3
service_description Description3
servicegroups virtual-servers,standalone-servers
check_command check_remote_proc!1:1:!xinetd
}
A nice thing about HERE documents, is that it easy to edit the template, which is self contained in the script.
 
Old 11-22-2010, 09:37 PM   #6
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks for the suggestions and quick responses. I will try them tomorrow.
 
Old 11-23-2010, 05:37 PM   #7
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
Thumbs up

Thanks again to you both for helping out. I haven't been able to finish the final script yet but I am getting really close.

I tried both of your approaches. jschiwal's requires a little more work on my end but is somewhat easier to work on. grail's I got much closer to what I was expecting as an end result but required me to go deeper trying to understand the script process.

Either way your solutions were great! I will try finishing it tomorrow and post a follow up if I get stuck again.

Always a pleasure to talk to people like you who really know their game!
 
Old 11-24-2010, 02:02 PM   #8
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
Yep! Got it ironed out and your suggestions were key to my success! Thanks again! Officially SOLVED!
 
Old 11-24-2010, 07:05 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Glad you got it worked out
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read multiple input with bash zulsolar Programming 10 05-09-2010 02:22 AM
Create a Multiple files by reading a input file and changing the contents bhargavkr Linux - Newbie 4 09-17-2009 12:37 AM
Script to append lines to multiple files jimma Linux - General 1 08-22-2009 06:52 AM
append end of multiple files daberkow Linux - Newbie 3 05-01-2009 05:11 PM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 12:58 AM


All times are GMT -5. The time now is 06:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration