Find the answer to your Linux question:
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' ...
  1. #1
    Just Joined! nexusroot's Avatar
    Join Date
    Jan 2010
    Location
    -84.706059324915, -62.4843750666430
    Posts
    2

    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:
    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
    heres the portion of the script im trying to work with :
    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 :
    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
    Not sure if theres a better way or not, if there is let me know!

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    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
    Unless I have misunderstood your requirements, this script shows you IPs that have more than one occurence in test.ip.

  3. #3
    Just Joined! nexusroot's Avatar
    Join Date
    Jan 2010
    Location
    -84.706059324915, -62.4843750666430
    Posts
    2
    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:
    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
    thanks for the suggestions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...