Sponsored Content
Top Forums Shell Programming and Scripting Bash arrays that compare ip addresses. Post 302747759 by Azrael on Saturday 22nd of December 2012 07:09:59 PM
Old 12-22-2012
Sorry, but this is the dichotomy I face in every forum. Posting 200 lines of code and being told that is too much information. Or just putting the problematic code and being told its not enough. I also feared that since some of this will only run on KDE that many would ignore this post, but here goes:

Code:
#!/bin/bash -x

# Interupt and Exit Function
control_c()
{
setterm -cursor on
exit 0
}

# Make sure user is logged in as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

clear
setterm -cursor off

# Define some paranoid stuff
fox="firefox"

# Define whitelist array
declare -a white

while true 
do

  stat=`netstat -antup | awk '{ print $7 }' | grep firefox | sed -ne "s/^[^\/]\+\///p" | awk '!x[$0]++';`
  b=$stat

     if [ "$b" = "$fox" ];
     then

     #timeout 1 ngrep -d any -O -T port http >> log
     getip=`netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//' | awk '!x[$0]++';`
     ip+=$getip
     #ipchk=`iptables -L | egrep '($ip|ALLOW)'`
     #ipck=$ipchk


#       c=0
#       len=${#ip[@]}
#       for (( c = 0; c < $len; c++ ));
        for c in "${ip[@]}"
        do
            if [ $c == "${white[$c]}" ]
            then
                echo -e "${white[$c]} in  whitelist\n"

            else

                echo -e "Not in whitelist"

                     #else
                             # Whitespace specific like python... 
                             sudo -u ph33r kdialog --title "KNetWatch" --warningyesno "Wanna block   
${ip[$c]}"


#echo ${ip[1]}

            #fi
        #done

        #${#ip[@]}

        #echo "${ip[@]}"


        # Log what happened in background
         #echo "" >> log | date >> log | echo "" >> log;  netstat -antpu | awk '!x[$0]++' >> log; echo "" >> log; echo  "" >> log;
                                                                                                                                         
  rc=$?


        if [ "${rc}" == "0" ]; then

           getpid=`netstat -antup | awk '{ print $7 }' | grep firefox | cut -f1 -d"/" | awk '!x[$0]++'`

           # Kill the appiclation
           #pid=$getpid
           #kill -9 $pid 
           sudo -u ph33r kdialog --title "Firefox was killed by KNetWatch" --passivepopup "Examine log file for details" 7

           # Block the ip address
           #ip=$getip
           /sbin/iptables -I OUTPUT -s ${ip[$c]} -j DROP

        elif [ "${rc}" == "1" ]; then

           # Use array as iptables uses the hostname here.

                pass=${ip[$c]}
                white+=$pass

           sudo -u ph33r kdialog --title "KNetWatch" --passivepopup "
${ip[$c]} 
whitelisted." 7

           # clear array to avoid duplicates
           unset ip

        else

            echo "You shudna done that he's just a boy!"

        fi

     fi
   done
fi

sleep 2

done

Much of this is commented out as I've made changes where some is now irrelevant, or to not run every part to make testing easier.

What was said about "awk '!x[$0]++'" removing duplicates is true, but this was only for where netstat was being checked. "unset ip" ip was to clear the current ips listed in the ip array to so that the command that checks netstat would not enter the same ips again and cause duplicates in that array.

Its true this does not need to check floats, but strings. I should have specified that. However, I've never told this code to interpret ips as floats or strings as Bash is not a strong typed language. So I don't know how to set or correct this.

I have also tried to resolve checking the elements of the arrays one at a time with a nested for loop. It seemed to be better logic but I run into problems with no output.

Code:
        for c in "${ip[@]}"
        do
         for d in "${white[@]}"
         do
           if [ "$c" -eq "$d"]
           then
               echo -e "${white[$d]} in array\n"

           else

               echo -e "$c is not in array"

               pass=${ip[$c]}
               white+=$pass

Hopefully that is not an overkill of information.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies

2. Shell Programming and Scripting

Compare arrays in perl

Hello, Let's say that we have the two following arrays @array1= @array2= Is there any easy way to compare these two arrays and print the values that exist in array1 and not in array2 and the values that exist in array2 and not in array1? Regards, Chriss_58 (3 Replies)
Discussion started by: chriss_58
3 Replies

3. Shell Programming and Scripting

Using Bash scripting to compare arrays looking for matches

I have two arrays I need to compare against a third, looking for matches, not differences. I think I'm going to have to convert the arrays to files and grep them, but I'm not too sure if there's a tool to enable me to matches specifically, instead of differences. Thanks in advance! Karl (9 Replies)
Discussion started by: karlp
9 Replies

4. Shell Programming and Scripting

Perl Compare 2 Arrays

Hello, Consider the following 2 arrays: Array1 = qw(Fa0/0 Fa0/1 Fa0/2 Fa0/3); Array1 = qw(Fa0/1 Fa0/2 Fa0/3 Fa0/4); I want to compare the following 2 arrays as follows: Take specific action when elements of Array1 that doesn't exist in Array2 (in my example: Fa0/0). Take another... (4 Replies)
Discussion started by: ahmed_zaher
4 Replies

5. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

6. Shell Programming and Scripting

Compare two arrays

Hi, I am trying to compare two lists that are held in two variables so I believe I need to access the array elements to compare these. I am using ksh 88 and the code I have tried is below: for file in ${origfilelist} do if ]] then print -- "File ${file}... (3 Replies)
Discussion started by: frodo61
3 Replies

7. Shell Programming and Scripting

Compare IP addresses and increment

Basically, I have 2 files with IP address. For example 134.123.3.234 in the first file and 134.123.3.235 in the second. Now I want to create a file with a IP address with IP 134.123.3.236...(max 254). So i have to check files which IP address in previous two files and base on that to create a new... (10 Replies)
Discussion started by: Manu1234567
10 Replies

8. Shell Programming and Scripting

Using Diff to compare 2 arrays

I have two arrays and they look like this: array=(`cat /local/mnt/*sys/*includes|grep -v NEW`) array2=(`cat /tmp/*sys.z |grep -v NEW`) I am trying to compare them but I need to use the diff -u command. I am not sure how to do this. I cannot just do diff -u ${array} ${array2} I cannot... (4 Replies)
Discussion started by: newbie2010
4 Replies

9. Shell Programming and Scripting

Compare two arrays by values [BASH]

I have 2 arrays of values for example A1 ={10 15 3 21} A2 ={10 15 3 22} I need to check which one has greater values. However: A1 ={10 15 3 21} A2 ={10 15 3 21 3} - this one would be greater. A1 ={10 15 5 21} - this one greater A2 ={10 15 3 21} Basically, I want to compare patch... (6 Replies)
Discussion started by: Jutsimitsu
6 Replies

10. UNIX for Beginners Questions & Answers

Compare bash arrays issue

Hello everyone, I need help comparing 2 arrays. the first array is static; the second array is not .. array1=( "macOS Mojave" "iTunes" ) cd /Volumes array2=( * ) # output of array2 macOS Mojave iTunes Mac me The problem occurs when I compare the arrays with the following code - ... (6 Replies)
Discussion started by: trexthurman
6 Replies
All times are GMT -4. The time now is 03:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy