Register
It is currently Thu Apr 24, 2014 12:13 am

using sed/ awk to grab NTP IP for later use


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Fri Nov 18, 2011 8:09 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
I have been trying to come up with a way to grab the NTP IP address in /etc/ntp.conf and use it in a variable for one of my scripts. Basically, this would grab it and auto populate a reports file with all network-like info, of which the NTP address is an entry.

*Also, for the network that I am working on, it doesn't connect to the Internet, so it doesn't have the usual FQDN's for the 'server' line.

I know sed and awk can grab the entry after "server" in ntp.conf, but there are a lot of words throughout the file with "server", which wouldn't work for me.

So, I was thinking of adding a commented out phrase to the end of the actual line to search for, so it would narrow it down to a unique line in the file:

Code:
server 192.168.1.3 #NTP_IP

...then, somehow, grab just the IP.

Can someone help me out, using the above example "server" entry and trim off the word "server" and "#NTP_IP", along with the spaces between those words?

thanks in advance...


Top
 Profile  
 PostPosted: Fri Nov 18, 2011 8:10 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
also, I should have noted, that this IP will change, depending on which network it is on...so I can't search for the static IP.


Top
 Profile  
 PostPosted: Fri Nov 18, 2011 9:45 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

Code:
sed -n 's/^server \([^#]*\).*/\1/p' /etc/ntp.conf


Top
 Profile  
 PostPosted: Sat Nov 19, 2011 11:56 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
works perfectly! Thanks!


Top
 Profile  
 PostPosted: Sat Nov 19, 2011 3:00 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Well, it works, don't get me wrong, but I am having another issue.

Here is the relevant code from my overall script:

Code:
NTPADDR=`sed -n 's/^server \([^#]*\).*/\1/p' /etc/ntp.conf`

echo "$NTPADDR will be added to net_reports.txt

sed -i 's/TSVR/'$NTPADDR'/g' /opt/net_reports.txt



It properly echoes the correct IP from /etc/ntp.conf to the screen, but does not go in and change the TSVR placemarker in the net_reports.txt file.
It gives this error:
sed: -e expression #1, char 25: unterminated `s' command

any ideas?


Top
 Profile  
 PostPosted: Sat Nov 19, 2011 3:59 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
Hi,

probably there's one or more trailing space(s) in $NTADDR
see it with
Code:
echo "${NTADDR}-"

also you could avoid such behaviour from sed by double quoting the whole command
Code:
sed "s/TSVR/$NTPADDR/g" /opt/net_reports.txt


Top
 Profile  
 PostPosted: Sat Nov 19, 2011 4:39 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
thanks again, it was a trailing space. Using the double quotes fixed it.


Top
 Profile  
 PostPosted: Sun Nov 20, 2011 10:49 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
dagumĀ²it wrote:
Basically, this would grab it and auto populate a reports file with all network-like info, of which the NTP address is an entry.
[...]
So, I was thinking of adding a commented out phrase to the end of the actual line to search for, so it would narrow it down to a unique line in the file:
Code:
server 192.168.1.3 #NTP_IP

...then, somehow, grab just the IP.


Using awk(1) to retrieve the IP address, and ed(1) to edit the file.

Command line version:
Code:
ed -s /opt/net_reports.txt < <(printf %s\\n "g/TSVR/ s//$(awk '$NF ~ /#[[:space:]]*NTP_IP/ { print $2 }' /etc/ntp.conf)/" w)


Script version:
Code:
ed -s /opt/net_reports.txt << EOF
g/TSVR/\
    s//$(awk '$NF ~ /#[[:space:]]*NTP_IP/ { print $2 }' /etc/ntp.conf)/
w
EOF


Unfortunately you can't hold something in ed(1)'s buffer or keep the last one before editing another file.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP