Solved

Help with Shell Script

Posted on 2014-01-07
1
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

622 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