Solved

Help with Shell Script

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

762 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