Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-21-2012
Registered User
 
Join Date: Feb 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
nslookup hanging

Hey folks. Long time lurker, first time poster. I'm a bit of a newbie at "coding" (obviously, scripting is a teensy bit different than coding) and I've run into a problem that I just can't seem to get around.

I'm going through a list of servers to check their name, IP, reverse-NSLOOKUP name and ping status (whether pinging the server was successful). The thing is, every time I try to run through a list of servers, I get hung up on servers that aren't found in the domain.

I want to catch those servers and make sure I know they're not in the domain, but I don't want it to lock up the program that's running.

Here's my code:


Code:
for node_name in `cat /var/opt/OV/share/utils/node_check/log/myname/check_these_servers.txt`
do
  #Check for full name and IP address
  nslookup $node_name > /tmp/nslookup.out
  if [ $? = 0 ]
  then
    full_name=`cat /tmp/nslookup.out | grep  "Name:" | awk {'print $2'}`
    ip_address=`cat /tmp/nslookup.out | grep  "Address:" | awk {'print $2'}`
  else
    full_name="NONE"
    ip_address="NONE"
    reverse_name="NONE"
    ping_status="NONE"
    echo $node_name,$full_name,$ip_address,$reverse_name,$ping_status >> /var/opt/OV/share/utils/node_check/log/myname/nnm_check.csv
    # The "continue" here will be put back in when the script is working
    # continue
  fi
  #Do a reverse NSLOOKUP on IP address for full name
  nslookup $ip_address > /tmp/nslookup.out
  if [ $? = 0 ]
  then
    reverse_name=`cat /tmp/nslookup.out | grep  "Name:" | awk {'print $2'}`
  else
    reverse_name="NONE"
  fi
  #Check for successful ping
  ping $full_name -n 5 > /dev/null
  if [ $? = 0 ]
  then
    ping_status="YES"
  else
    ping_status="NO"
  fi
  #Print results
  echo $node_name,$full_name,$ip_address,$reverse_name,$ping_status >> /var/opt/OV/share/utils/node_check/log/myname/nnm_check.csv
done

Let's say I run a list of servers by using only the first part of their names (i.e. instead of "node_name1.domain-name.org", I use "node_name1"). I want NSLOOKUP to see if those servers are in the domain and for part of the resulting output to be the full name of that server instead of just the beginning part of it.

So I have a list that includes:

Quote:
node_name1
node_name2
node_name3
node_name4
...etc.
Let's also say that node_name3 is the server NSLOOKUP cannot locate. Here's what the output to my screen looks like:

Quote:
*** hostserver.domain-name.org can't find node_name3: Non-existent domain
And there it stays. The only way for me to stop it is to exit the command line entirely!

Here's what the file output looks like:

Quote:
node_name1,node_name1.domain-name.org,127.0.0.1 [I'm substituting a fake IP here for you guys],NODE_NAME1.domain-name.org,YES
node_name2,node_name2.domain-name.org,127.0.0.2 [I'm substituting a fake IP here for you guys],NODE_NAME2.domain-name.org,YES
In other words, the first two nodes are processed. When node_name3 comes around, the entire program hangs after it fails to find node_name3 in the domain.

Is it because I'm using only part of the hostname that I'm trying to look up? If this is so, why do the first two names work fine? Obviously I could delete the offending node name out of the list but I'd rather not have to do that every time there's a node name that doesn't work.

Anyone have any clues? And yes, I tried "timeout". It didn't work.
Sponsored Links
    #2  
Old 02-21-2012
Chubler_XL's Avatar
Registered User
 
Join Date: Oct 2010
Posts: 2,109
Thanks: 65
Thanked 611 Times in 589 Posts
It's probably because this grep "Address:" | awk {'print $2'}` is matching 2 lines of output


Code:
Server:  dc.domain-name.org
Address:  127.0.0.10
Name:    node_name1.domain-name.org
Address:  127.0.0.22

Calling nslookup 127.0.0.10 127.0.0.22 will probably hang waiting for a DNS response from 127.0.0.22 when it probably isn't a DNS server

You probably want the last occurance of Address: try:

Code:
ip_address=`awk '/Address:/{ I=$2} END{ print I}' /tmp/nslookup.out`

Note this also removes unneeded calls to cat and grep.

You should also test for blank full_name before using ping:


Code:
  [ -z "$full_name" ] && continue
  ping $full_name -n 5 > /dev/null

The Following 3 Users Say Thank You to Chubler_XL For This Useful Post:
Bearwhale (02-22-2012), fpmurphy (02-22-2012), jim mcnamara (02-21-2012)
Sponsored Links
    #3  
Old 02-22-2012
Registered User
 
Join Date: Feb 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks, Chubler_XL! That was what was causing the problem!

I ran the check as you suggested, just in case.

Cheers!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Nslookup!!! Please look up!!! anishkumarv Emergency UNIX and Linux Support !! Help Me!! 4 05-05-2011 01:23 AM
nslookup: am I doing it right? Abdulelah Shell Programming and Scripting 8 01-05-2011 02:13 PM
nslookup strangness purest IP Networking 3 12-13-2007 07:42 AM
DNS client nslookup xnightcrawl UNIX for Advanced & Expert Users 3 03-24-2006 02:29 PM
nslookup problem xnightcrawl UNIX for Advanced & Expert Users 8 03-22-2006 09:07 PM



All times are GMT -4. The time now is 11:33 AM.