Compare IP Address from nslookup and file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare IP Address from nslookup and file
# 8  
Old 08-18-2011
try this and let me know if it works. I have eliminated useless use of cat and hopefully made it simpler.


Code:
#!/usr/bin/ksh
awk -F";" '{print $1}' serverlist|while read line
do
curr_ip=`nslookup $line|grep Address | sed -n '2p' | cut -d: -f2|sed 's/^[ \t]*//'`
old_ip=`awk -F";" /"$line"/'{print $2}' serverlist`
if [[ "$curr_ip" = "$old_ip" ]]
then
echo "equal"
fi
done

This User Gave Thanks to dude2cool For This Post:
# 9  
Old 08-18-2011
Yes, I agree with dude2cool --- make it simpler, get rid of all those do's and indentation makes it easier to read and to follow your logic flow ...

Code:
#!/usr/bin/ksh

for server in `cat serverlist`
do
  ser_name=`echo $server | cut -f1 -d";"`
  old_ip=`echo $server | cut -f2 -d";"`
  curr_ip=`/usr/bin/nslookup $ser_name | grep Address | sed -n '2p' | cut -d: -f2`
  if [ $curr_ip == $old_ip ]
    then
      echo "IP is SAME"
    else
      echo "IP is DIFFERENT"
    fi
done

This User Gave Thanks to toro95037 For This Post:
# 10  
Old 08-19-2011
That worked...but I need to send a single email at the end of the check if the IP is different. I used the below code but it sends multiple email since it is in loop.

Code:
#!/usr/bin/ksh
for server in `cat serverlist`
do
        ser_name=`echo $server | cut -f2 -d";"`
        old_ip=`echo $server | cut -f1 -d";"`
        curr_ip=`nslookup $ser_name | grep Address | sed -n '2p' | cut -d: -f2`
        if [ $curr_ip == $old_ip ]
        then
                echo "IP is SAME,$ser_name"
        else
                echo "IP is DIFFERENT,$ser_name" >> log.txt
                mailx -s "$ser_name" viku2006@gmail.com < log.txt
        fi
done


Last edited by Franklin52; 08-19-2011 at 03:16 PM.. Reason: Please use code tags for data and code samples, thank you
# 11  
Old 08-19-2011
At least three subshells can easily be eliminated:
Code:
IFS=';'
while read ser_name old_ip
do
    curr_ip=$(dig $ser_name +short | sed -n '1p' )
    if [[ "$curr_ip" == "$old_ip" ]]
    then
       echo "IP is SAME, $ser_name"
    else
       echo "IP is DIFFERENT,$ser_name" >> log.txt
    fi
done < serverlist

# 12  
Old 08-24-2011
hi,

I didnot yet find a solution for sending a single emails when IP is different..

my current code

#!/usr/bin/ksh
for server in `cat serverlist`
do
ser_name=`echo $server | cut -f2 -d";"`
old_ip=`echo $server | cut -f1 -d";"`
curr_ip=`nslookup $ser_name | grep Address | sed -n '2p' | cut -d: -f2`
if [ $curr_ip == $old_ip ]
then
echo "IP is SAME,$ser_name"
else
echo "IP is DIFFERENT,$ser_name" >> log.txt
mailx -s "$ser_name" viku2006@gmail.com < log.txt
fi
done
# 13  
Old 08-24-2011
put the mailx command after done.
Code:
grep -q DIFFERENT log.txt && mailx -s "IP DIFFERENT" < log.txt "

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk nslookup

Hi, I have a rather simple awk statement to perform an nslookup on a file (list of IPs). If the lookup yields a result, then it sends it to a file. If no result, there is no output. I've been trying to use !NR or !NF with an if statement to make the output state "NA" if there is no result,... (5 Replies)
Discussion started by: umang2382
5 Replies

2. Solaris

nslookup command

hi, I am new in Solaris 10 , i am installing a software in solaris 10, while performing pre requisite nslookup command should return without error, and the IP address and host should match those listed in the etc/hosts file, but nslookup command show the below error , can anybody tell me how i... (4 Replies)
Discussion started by: zeeshan047
4 Replies

3. Emergency UNIX and Linux Support

Nslookup!!! Please look up!!!

Hi all Nslookup command returns!!!! Code: nslookup 10.91.44.29 Server: 127.0.0.1 Address: 127.0.0.1#53 29.44.91.in-addr.arpa name = station1.example.com Code: nslookup station1.example.com Server: 127.0.0.1 Address: 127.0.0.1#53 Name: ... (4 Replies)
Discussion started by: anishkumarv
4 Replies

4. Shell Programming and Scripting

nslookup: am I doing it right?

Hi guys, I'm back to get some help again :) I have a file that has a list of IP addresses, and I use nslookup in my script to resolve hostnames of those IPs. the problem I'm facing is that I not all the hostnames got resolved! and I don't know why. sometimes the same duplicate IP returns a... (8 Replies)
Discussion started by: Abdulelah
8 Replies

5. Shell Programming and Scripting

Fast algorithm to compare an IP address against a list of IP sections?

I have two files: file1: 41.138.128.0 41.138.159.255 location 41.138.160.0 41.138.191.255 location 41.138.192.0 41.138.207.255 location 41.138.208.0 41.138.223.255 location 41.138.224.0 41.138.239.255 location 41.138.240.0 41.138.255.255 location 41.138.32.0 ... (7 Replies)
Discussion started by: kevintse
7 Replies

6. IP Networking

nslookup command?

Hai guys, In which way does the command,"nslookup", will be helpful? Please give me examples. Thanks. (5 Replies)
Discussion started by: Felicia23
5 Replies

7. Shell Programming and Scripting

script to compare two files of mac address

Hi I need to write a bash shell script. I have two separate text files. One file contains a list of MAC addresses taken from a network scan, the other contains a list of MAC addresses for our currently-managed devices. How can I compare these two files, and output a list of addresses that have... (6 Replies)
Discussion started by: borderblaster
6 Replies

8. Shell Programming and Scripting

NSLOOKUP script

Hi Anyone know of a quick and simple way to script an NSLOOKUP's from a list/text file? Thanks, Gav (1 Reply)
Discussion started by: gavcollins
1 Replies

9. UNIX for Dummies Questions & Answers

nslookup on linux ???

Hi I usually use solaris but Im working right now with a Linux box and I cannot find the nslookup command. Does Linux use this command or have an other one to do dns querys?? Thanks and sorry for such a dumb question.. :) (2 Replies)
Discussion started by: piltrafa
2 Replies

10. UNIX for Advanced & Expert Users

named and nslookup

Guys , Am using Linux - Red Hat 6.1 . My DNS and nslookup command was working fine and ppl. can resolve through this server . but i changed something in /etc/named.conf , that stopped the DNS . The named proccess is working , but nslookup command is not , i get this error message : ... (3 Replies)
Discussion started by: tamemi
3 Replies
Login or Register to Ask a Question