Solved

Help with Shell Script

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

752 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