Solved

Help with Shell Script

Posted on 2014-01-07
1
270 Views
Last Modified: 2014-01-22
Hello,

I am looking for help writing a *nix shell script, specifically to execute on OpenWRT environments.

The summary of the process is as follows:

1) The script would query/resolve a DNS (i.e: foobar.example.com) and get the IP address. If multiple IP's are returned, just the first one should be good enough.

eg:
>nslookup foobar.example.com 8.8.8.8
Server:    8.8.8.8
Address 1: 8.8.8.8 google-public-dns-a.google.com

Name:      foobar.example.com
Address 1: 3.3.3.3  n003-000-000-000.static.ge.com
Address 2: 2.2.2.2

Open in new window


2) the script would then overwrite a config file with the new resolved IP. Config file: server.conf

server.conf
============

foo = bar
bar = foo
some_other_config = 1
server_ip = 5.6.7.8
more_config = true

Open in new window


it should find the line which says server_ip = x.x.x.x, and replce it it server_ip = 3.3.3.3

3) Optionally - it would be nice if we could read the exiting server.conf file and make sure the IP is indeed different and needs to be updated. If yes, then go ahead with the replacement, if not, leave the server.conf alone.

I know this can be achieved by the use of sed and/or awk, but I've had my share of issues doing it, as this is a new area for me.

Any help would be much appreciated.

Thank you!
0
Comment
Question by:shaf81
1 Comment
 
LVL 19

Accepted Solution

by:
simon3270 earned 300 total points
ID: 39761748
To get the IP address:
SERVER=foobar.example.com
IP=$(nslookup $SERVER 8.8.8.8 | awk '/^Name:/{r=1}/^Address:/{if (r == 1){print $2;exit}}')

Open in new window


To replace the existing IP address:
CONF=server.conf
sed -i 's/^server_ip.*/server_ip = '"${IP}/" $CONF

Open in new window


To do this only if the address has changed:
oldip=$(awk '$1 ~ /^server_ip$/{print $3;exit}' $CONF)

if [ "$oldip" != "" ]
then
 if [ "$oldip" != "$IP" ]
 then
   echo Replacing IP
   sed -i 's/^server_ip.*/server_ip = '"${IP}/" $CONF
 fi
fi

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now