Bash arrays that compare ip addresses.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash arrays that compare ip addresses.
# 1  
Old 12-21-2012
Bash arrays that compare ip addresses.

I've been trying to have an array of ip addresses go through a loop one at a time. Then compare if the current element is in another array of ip addresses. I've traced my error with /bin/bash -x

Code:
+ for c in '"${ip[@]}"'
./netk5: line 65: 50.17.231.23 23.64.146.110 23.64.159.139 107.14.36.129 31.13.74.7 173.194.77.95: syntax error: invalid arithmetic operator (error token is ".17.231.23 23.64.146.110 23.64.159.139 107.14.36.129 31.13.74.7 173.194.77.95")

From what I found on Google it seems Bash isn't great at comparing floating point numbers. I saw some suggestions on piping through bc first, but I'm unfamiliar with how that would work here. Perhaps there's also a simpler way.

Here is the code I'm using:

Code:
 #!/bin/bash -x

fox="firefox"

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
     
     getip=`netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//' | awk '!x[$0]++';`
     ip+=$getip

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

            else

                echo -e "Not in array"

                pass=${ip[$c]}
                white+=$pass
           
           # clear array to avoid duplicates
             unset ip

        fi

   done
fi

sleep 2

done

Right now it looks to see if an ip Firefox is connected to is in the "white" array. You may have to refresh your browser to get an ip.

UPDATE: It doesn't seem to be an issue with bc. I tried using the following to get the first ip's and the error persists:
Code:
timeout 1 bc | netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//' | awk '!x[$0]++'


Last edited by Azrael; 12-21-2012 at 06:48 PM.. Reason: New information
# 2  
Old 12-22-2012
It doesn't need to compare floats, it needs to compare strings. As I can't see line 65 (the one with the syntax error) in your code, pls. post entire script and/or log of execution.
# 3  
Old 12-22-2012
Could you tell us what you are trying to do. Not how you want to do it. You have some greatly over-complex code to do something simple - it seems to me.

an awk one-liner and two inputs can identify or remove duplicates, for example.
# 4  
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.
# 5  
Old 12-22-2012
Post contents of this variable:
Code:
getip=`netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//' | awk '!x[$0]++';`
echo $getip

# 6  
Old 12-23-2012
Quote:
Originally Posted by Azrael
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.
. . .
Unfortunately the error msg you posted did not relate to the problematic code snippet you gave. So - where to start looking?
Anyhow, seems like bash doesn't really like the dots in the ip numbers, even when comparing strings, in the if [ "$c" == ...]. Try replacing them with a non-bash-irritating separator, which is not that easy to find ( I used a comma):
Code:
 . . .
     getip=( $(netstat -antup | awk '/firefox/ {sub (/:.*$/, "", $5); gsub ("\.", ",", $5); print $5}') )
        for c in ${getip[@]}
        do  something
        done

This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-23-2012
Sorry if that remark came off sarcastic. It was meant as a sigh...

An error message was provided, but I can see how that might not have been enough information.

It appears that the code below still produced an error when used with mine:

Code:
getip=( $(netstat -antup | awk '/firefox/ {sub (/:.*$/, "", $5); gsub ("\.", ",", $5); print $5}') )

Error:
Code:
awk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'
+ ip+=,,,,,,,,,,,,
+ for c in '"${ip[@]}"'
./netk6: line 66: ,,,,,,,,,,,,: syntax error: operand expected (error token is ",,,,,,,,,,,,")

However, this rang a bell in my head that escaping the . characters may resolve this problem.

I found a link on how to do this with ip addresses here:

Code:
http://stackoverflow.com/questions/12427892/auto-escaping-an-ip-address-in-bash-alias-argv

Perhaps I'll give this a shot after I've had adequate sleep in a couple hours.

Thank you for your suggestion.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question