The UNIX and Linux Forums  

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


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check ssh connection to remote host praveenbvarrier Shell Programming and Scripting 6 10-06-2008 09:46 AM
Duplicate crontab entries vikashtulsiyan SUN Solaris 2 05-06-2008 02:07 AM
Bash Script to check Remote Host Connection zulfikarmd UNIX for Dummies Questions & Answers 5 04-16-2008 03:53 AM
Filtering multiple entries in a file suman_jakkula Shell Programming and Scripting 2 08-22-2006 02:02 AM
deleting double entries in a log file opusforum Shell Programming and Scripting 5 09-17-2002 12:02 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-21-2008
Registered User
 

Join Date: Jan 2008
Posts: 3
Check host file for duplicate entries

I need a KSH script that will check a host file for duplicate IP's and/or host names and report out the errors. Anyone out there have one they would like to share?

Something like:

Hostname blahblah appears X times
IP Address xxx.xxx.xxx.xxx appears X times

TIA
Reply With Quote
Forum Sponsor
  #2  
Old 01-21-2008
Registered User
 

Join Date: Jan 2008
Location: London e Milano
Posts: 71
Hacking a previous post, something along the lines of

for i in `cat /etc/hosts | cut -d"." -f1,2 | uniq`
do
num=`cat /etc/hosts | grep $i | wc -l`
if (( $num > 1 ))
then
echo $i
fi
done

which will show you the IP address that have duplicates I think, just needs polishing off with a ''wc' command ?

- someone please confirm - digging up my script memories!

-------------------------------------------------------------------
~ stevie
Solaris SCSA (v8)
Microsoft MCP
Ubuntu & Fedora on-the-way
Reply With Quote
  #3  
Old 01-21-2008
Registered User
 

Join Date: Jan 2008
Posts: 3
hmmmm, maybe

uniq only picks up repeats when they are consecutive lines (I think). I need the script to check the whole file for a repeat....

<---scratching head
Reply With Quote
  #4  
Old 01-21-2008
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,590
Code:
getent hosts | awk '{ ip[$1]++  
        for ( host=2; host <= NF; host++ ) { 
            hosts[$host] ++ 
        }
    }
    END { 
        for ( add in ip ) { 
            print add, ip[add], "times" 
        } 
        for ( h in hosts ) { 
            print h, hosts[h], "times" 
        }
    }'
Obviously you can add a check for only >1 occurrences if you like.
Reply With Quote
  #5  
Old 01-21-2008
Registered User
 

Join Date: Jan 2008
Posts: 3
Thank you both for your help, I have it now

Last edited by ThreeDot; 01-22-2008 at 10:43 AM. Reason: question answered
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0