Results 1 to 3 of 3
Basically im going to have a variable with a list of IP address'. I need a very simple script to compare and tell me if any of those IP address' ...
- 01-17-2010 #1
Bash script to compare multiple variables
Basically im going to have a variable with a list of IP address'. I need a very simple script to compare and tell me if any of those IP address' are the same. Generally the list will be less then 5 IP's but it could be more, and i just need to know if theres duplicates. I haven't been able to come up with a good clean way to work this one out. if anyone has any suggestions that would be great!
an example of my test.ip file:
heres the portion of the script im trying to work with :Code:user1 tty01 Jan 16 03:16 user2 ttyp0 Jan 16 19:19 10.10.69.22 user3 ttyp1 Jan 16 20:31 10.10.69.241 user4 ttyp1 Jan 16 20:31 10.10.69.22
Code:count="0" ttyp_ip=`cat test.ip |awk '/ttyp/{print $6}'` for each in ${ttyp_ip} do echo "ttyp ip = ${each}" count=`expr ${count} + "1"` echo "new count = ${count}" eval ttyp_ip_${!count}="${each}" echo "unique ip variable is ttyp_ip_${count} which = $ttyp_ip_${!count}" done #somehow compare all ttyp_ip_counts here...
EDIT:
ehh i guess i figured out how to do it this way :
Not sure if theres a better way or not, if there is let me know!Code:count="0" rand=$RANDOM ttyp_ip=`cat test.ip |awk '/ttyp/{print $6}'` for each in ${ttyp_ip} do # echo "ttyp ip = ${each}" count=`expr ${count} + "1"` # echo "new count = ${count}" eval ttyp_ip_${!count}="${each}" # echo "unique ip variable is ttyp_ip_${count} which = $ttyp_ip_${!count}" prune_ip=`echo $ttyp_ip_${!count} |tr -d '.'` if [ -e /tmp/$prune_ip.$rand ] then echo "FAILURE - MULTIPLE LOGINS FROM SAME IP $each" else touch /tmp/$prune_ip.$rand fi done rm /tmp/*.$rand
- 01-17-2010 #2Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
Unless I have misunderstood your requirements, this script shows you IPs that have more than one occurence in test.ip.Code:ttyp_ip=`cat test.ip |awk '/ttyp/{print $6}'` for each in ${ttyp_ip} do nb=$(grep -c $each test.ip) [ $nb -gt 1 ] && echo "$each has $nb occurences." done
- 01-17-2010 #3
Ah there was a better way. I like that way, the only thing i don't like about it is if it see's the IP address 3 times it's going to echo a failure message 3 times. My way only does once. Considering this is going to be ran across 500 systems trying to display the least amount of junk as possible is key.
i guess i could do something like this:
thanks for the suggestions!Code:nb=$(grep -c $each test.ip) [ $nb -gt 1 -a ! -e /tmp/$each ] && echo "FAILURE - MULTIPLE LOGINS FROM SAME IP $each" touch /tmp/$each


Reply With Quote