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

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

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

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.

Thank you both for your help, I have it now