LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-13-2012, 10:47 PM   #1
call_krushna
Member
 
Registered: Aug 2007
Location: India
Distribution: Ubuntu
Posts: 173

Rep: Reputation: 1
problem with sed ,adding ip in a file


Hi Team,

I have written as script to automate nagios installation.
During the installation,I need to add my nagios server ip to
/etc/xinetd/nrpe file .

The /etc/xinetd/nrpe look like as follown

#less /etc/xinetd/nrpe

# default: on
# description: NRPE (Nagios Remote Plugin Executor)
service nrpe
{
flags = REUSE
socket_type = stream
port = 5666
wait = no
user = nagios
group = nagios
server = /usr/local/nagios/bin/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure += USERID
disable = no
only_from = 127.0.0.1
}
##############################################################
Here I want to add ip next to 127.0.0.1 10.0.0.120

after adding my nrpe.cfg shoul look like as follows


##############################################################
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
service nrpe
{
flags = REUSE
socket_type = stream
port = 5666
wait = no
user = nagios
group = nagios
server = /usr/local/nagios/bin/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure += USERID
disable = no
only_from = 127.0.0.1 10.0.0.120
}

How to automate it in my script

Any help is highly appreciable .
 
Old 06-13-2012, 10:49 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Need to see the script ...
 
Old 06-13-2012, 10:56 PM   #3
call_krushna
Member
 
Registered: Aug 2007
Location: India
Distribution: Ubuntu
Posts: 173

Original Poster
Rep: Reputation: 1
Here is my script

#################################################################################################### #########################
#!/bin/bash
#Purpose :- To install nagios client
#Author :- Krushna
useradd nagios
apt-get -y install xinetd
MYDIR=/mnt/`date +%F`
mkdir $MYDIR
cd $MYDIR
wget http://nchc.dl.sourceforge.net/proje...-1.4.15.tar.gz
tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure
make
make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec
cd $MYDIR
wget http://prdownloads.sourceforge.net/s...pe-2.13.tar.gz

tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
OS=`cat /etc/issue |awk '{print $2}'`
if [ "$OS" = 12.04 ]; then
apt-get install libcurl3-openssl-dev
ARCH=`uname -m`
if [ "$ARCH" = x86_64 ]; then
./configure --with-ssl-lib=/usr/lib/x86_64-linux-gnu/
else
./configure --with-ssl-lib=/usr/lib/i386-linux-gnu/
fi
else
apt-get install openssl-dev

./configure --enable-ssl
fi
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
sed -i -e /nrpe/d /etc/services
echo "nrpe 5666/tcp #NRPE " >> /etc/services
echo "Edit the /etc/xinetd.d/nrpe file and add the IP address of the monitoring server to the only_from directive."
echo "only_from = 127.0.0.1 <nagios_ip_address>"

#################################################################################################### ####################

As of now , I am doing it manully. Is it possible to automate it .
 
Old 06-14-2012, 10:56 AM   #4
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Question Could you be looking for something like this?

Hi call_krushna!

I'm not 100% sure about what you want, but maybe it is as easy as this:

Code:
sed -i /etc/xinetd/nrpe -e 's/^\(only_from = 127.0.0.1\)$/\1 10.0.0.120/'
This "command" is going to modify the file /etc/xinetd/nrpe by looking at each line of the file for exact matches of "only_from = 127.0.0.1". EACH AND EVERY such line is replaced by "only_from = 127.0.0.1 10.0.0.120". As long as "only_from = 127.0.0.1" is only once in your file it should perform well.

Code:
sed -e "s/a/b/" replaces the first "a" on every line with a "b"

with a being:
^ -> line start
\( -> begin of group 1
only_from = 127.0.0.1 -> line contend to replace
\) -> end of group 1
$ -> end of line

and b being:
\1 -> put first group here
 10.0.0.120 -> your new ip
the "-i FILENAME" switch makes sed to do a "in place" operation. Without this switch sed is a typical "filter"


I know this is kind of late but I hope this might still be usefull.


Regards,
Heraton
 
Old 06-14-2012, 09:39 PM   #5
call_krushna
Member
 
Registered: Aug 2007
Location: India
Distribution: Ubuntu
Posts: 173

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Heraton View Post
Hi call_krushna!

I'm not 100% sure about what you want, but maybe it is as easy as this:

Code:
sed -i /etc/xinetd/nrpe -e 's/^\(only_from = 127.0.0.1\)$/\1 10.0.0.120/'
This "command" is going to modify the file /etc/xinetd/nrpe by looking at each line of the file for exact matches of "only_from = 127.0.0.1". EACH AND EVERY such line is replaced by "only_from = 127.0.0.1 10.0.0.120". As long as "only_from = 127.0.0.1" is only once in your file it should perform well.

Code:
sed -e "s/a/b/" replaces the first "a" on every line with a "b"

with a being:
^ -> line start
\( -> begin of group 1
only_from = 127.0.0.1 -> line contend to replace
\) -> end of group 1
$ -> end of line

and b being:
\1 -> put first group here
 10.0.0.120 -> your new ip
the "-i FILENAME" switch makes sed to do a "in place" operation. Without this switch sed is a typical "filter"


I know this is kind of late but I hope this might still be usefull.


Regards,
Heraton
Hi Heraton,

Thanks for reply .You are right ,I just want add the ip next 127.0.0.1 with single space .
But the command is not working .

#sed -i /etc/xinetd.d/nrpe -e 's/^\(only_from = 127.0.0.1\)$/\1 10.0.0.120/'

There is no output .Nothing happens to /etc/xinetd.d/nrpe file .
 
Old 06-15-2012, 11:23 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
PLEASE use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

The long unbroken lines in the above scripts are forcing my screen to side-scroll. Please edit them to enclose them in code tags so that the thread is more readable. Thanks!

What is this?
Code:
#sed -i /etc/xinetd.d/nrpe -e 's/^\(only_from = 127.0.0.1\)$/\1 10.0.0.120/'
Is that supposed to be the input filename? sed doesn't work that way. The input file always goes at the end of the command. You only need to use "-i" alone for in-place editing, with an optional file suffix argument for creating a backup file (i.e. using "-i .bkup" will also create a "filename.bkup" with the original file content).

In any case, the above command is much more complex than necessary. All you really have to do is search for a unique substring in the line, match the endpoint, and append the new text at that point.

Code:
sed -i.bkup '/only_from/ s/$/ 10.0.0.120/' /etc/xinetd.d/nrpe
Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
 
Old 06-15-2012, 01:17 PM   #7
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Question Fascinating...

Dear call_krushna, dear David the H.!


When I tried to write a reply at work, I was unable to send because of a forgotten password. So some of my stuff is already said by David the H.

My first concern was in deed the use of code tags. When you reply there is the "Go advanced" button. In the editor you get to see there is a "#" button which creates the little helpful code tags. Otherwise you can use David the H.'s way of inserting them directly

I believe the problem is that your posting is not an EXACTLY match of the line in your file due to automated formatting done by LQ. That is why we asked you to post the file once more with code tags. Unless we know the exact content of your file we can not find out, what went wrong with the reg ex. I would assume there is one or more spaces or tabs that are causing trouble.

Quote:
Originally Posted by David the H.
What is this?
Code:
#sed -i /etc/xinetd.d/nrpe -e 's/^\(only_from = 127.0.0.1\)$/\1 10.0.0.120/'
Is that supposed to be the input filename? sed doesn't work that way.

Please be assured that I tested my code before posting (mint 12 box: GNU sed-Version 4.2.1). I copy-pasted call_krushnas posting into a file and tested against this file. Everything went well.

Just to be sure I redid my test on a second linux box (suse 10.3: forgot version). Works well too. So I have to admit I am quite a bit confused by that above statement from a senior member.

I know David the H. already made a good proposition, but I want to share the version I made at work anyway. The reason it is still some more complex is that it is appending the new ip after "127.0.0.1" instead of before, although that should not be a matter in your configuration file.
Code:
sed -i /etc/xinetd/nrpe -e 's/\(only_from.*=.*127.0.0.1\)/\1 10.0.0.120/'
I removed the matching for line start and end of line "^" and "$". The ".*" means no or any amount of characters. This reg ex matches EVERY line containing "only_from" followed by "=" and "127.0.0.1".

See if that works for you.

Regards
Heraton


Update:
from man sed
Quote:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
Okay, I get it. The file name is expected to be at the end of the command. But as there was no filename sed seems to have only used the "suffix" as the file name. As I supplied the complete path as suffix it did work anyway. The better way would have been:
Code:
#sed -i -e 's/^\(only_from = 127.0.0.1\)$/\1 10.0.0.120/'  /etc/xinetd.d/nrpe
Heraton

Last edited by Heraton; 06-15-2012 at 01:25 PM. Reason: Update on my confusion about sed
 
  


Reply



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
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
Adding new tag to an xml file using sed nano2 Linux - General 6 11-23-2011 05:47 AM
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
Adding the character "#" in a file using sed kushalkoolwal Programming 4 07-11-2008 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:20 AM.

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
Open Source Consulting | Domain Registration