Solved

Help with Shell Script

Posted on 2014-01-07
1
273 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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.

810 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