Need a condition to account for failed nslookups


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a condition to account for failed nslookups
# 1  
Old 02-17-2012
Need a condition to account for failed nslookups

I need some help creating a condition for looking up hosts. I have this master host file that has data in the following columns:
Code:
FQDN     primary IP     secondary IP     third IP

I need the hostnames to feed into another script I use for provisioning users. The FQDN doesn't always work for connection attempts and sometimes the hostname needs a different suffix (.svr.company.net, company.net, company.com, etc). Unfortunately this master host file is not managed by me and I do not have the ability to fix the hostnames origin in the file. However it does provide the IP's which I can look up and that gives me the appropriate FQDN. The other script in which this information is fed will only accept the hostname, not the IP.

So I run a loop to cat the master hosts file, awk for column 2, and nslookup the value at the time of the iteration. The thing I need help with is I need a condition to say "if the IP from column 2 does not resolve, try the one from column 3, and if that doesn't resolve then try the IP from column 4". I was toying with an if/else statement:
Code:
if [ nslookup $(cat line | awk -F " " {'print $2'}) | grep NXDOMAIN ]
     then try column 3
else
     proceed to the next line

I'm not really sure how it should look and I'm waiting on support so I thought I'd try here.
# 2  
Old 02-17-2012
I would use the host command, the output is easier to parse. This is in ksh.

Code:
#!/bin/ksh    
# also works with /bin/bash
while read col1 col2 col3 col4
do
     ok=""
    for i in $col2 $col3 $col4
    do
          host $i >/dev/null
          if [ $? -eq 0 ] ; then
               ok=$i
               break          
          fi
    done
    if [ ! -z "$ok" ] ; then
         echo "we found a good one: $ok"
    else
         echo "no valid values found"
    fi
done < master_hosts

try that for a starter
# 3  
Old 02-23-2012
This is really good - I've been working with it all week (among other things). Thank you! I have it set now to awk the ip address into a file and provide a list of exception hosts (NXDOMAIN) that need to be investigated. The only problem I have now is when "host" spits out two lines - like:
Code:
hostname is an alias for host_name
hostname has address 1.1.1.1

I think if I pipe it into a grep for "address" I should be good.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Further to my query re: failed attempt to change email address on existing account

Neo Thanks for your reply to my original post, entitled "Problem changing the email address associated with my unix.com account". I am unable to reply to you in that thread, as I am unable to log-on to unix.com! From what you said about purging dormant accounts, it is likely that my account... (1 Reply)
Discussion started by: irb
1 Replies

2. Windows & DOS: Issues & Discussions

What happens to your skype account if you close outlook.com email account?

Hello, Does anyone know what happens to your skype account if you close the outlook.com email account which are linked together? As you know they are both owned by Microsoft. Thanks (0 Replies)
Discussion started by: milhan
0 Replies

3. How to Post in the The UNIX and Linux Forums

Simultaneously try to execute commands after connecting to remote account to one account

I have made password less connection to my remote account. and i tried to execute commands at a time. but i am unable to execute the commands. ssh $ACCOUNT_DETAILS@$HOST_DETAILS cd ~/JEE/*/logs/ (1 Reply)
Discussion started by: kishored005
1 Replies

4. Solaris

multiple nslookups

Hi Guys i am creating a script in tcsh to perform a nslookup to a ip address. now i need to modify the script to perform nslookups on multiple ip addresses. the ip addresses are added as variables to the script on the command line. i also need to do a nslookup on a specific dns host. can... (10 Replies)
Discussion started by: brian112
10 Replies

5. UNIX for Dummies Questions & Answers

block user account after failed password

hi guys I have Centos 5.4 The idea is lock the user account for 3 minutes after he has entered his password incorrectly 3 times. I've modified /etc/pam.d/system-auth auth required pam_tally.so onerr=fail per_user deny=3 account required pam_tally.so resetbesides... (3 Replies)
Discussion started by: kopper
3 Replies

6. Linux

Apply disk quota to account(dedicate 3 GB to account).

Hi , I am faceing lot of problem due to "disk space is not enough". senerio is like as, In system has 5 account. a,b,c,d,e say account c if very critical. Due to other user's data, user 'c' is faceing disk space issue. I want to dedicate 3 GB for user 'c'. No user... (1 Reply)
Discussion started by: ashokd009
1 Replies

7. UNIX for Dummies Questions & Answers

Change Account to not lock account if password expires

I have access to 15+ UNIX boxes at work, and I do not consistently log onto all of them over time. When I do try to access one I havent been on in awhile, my account is locked as the password has expired. I need to request to the UNIX SA's that the password expiration is 90 days and that if it... (1 Reply)
Discussion started by: stringzz
1 Replies

8. Shell Programming and Scripting

Bad day !! test condition failed --need a one liner to do --help

Hi all this is simple but bad day for me nothing work out .. Problem is that I wan to check the argument passed to my script and accordignly exit or setup ENV variable I have a script name src_cpcp_preproc.sh i want to pass 2 argumet from command line argumet and check it in the script... (13 Replies)
Discussion started by: jambesh
13 Replies

9. UNIX for Dummies Questions & Answers

Setting an account to be a non-login account automatically?

Is there a way to easily change an account to be a non login account (NP in the shadow) file? I know I can just edit the file but that is not what we want to do. We use access control software and want to provide a way to set an account to be non-login using simple commands that can be mapped... (0 Replies)
Discussion started by: LordJezo
0 Replies
Login or Register to Ask a Question