Bash arrays that compare ip addresses.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash arrays that compare ip addresses.
# 8  
Old 12-24-2012
Working perfectly for me:
Code:
# netstat -antup | awk '/firefox/ {sub (/:.*$/, "", $5); gsub ("\.", ",", $5); print $5}'
68,232,35,121
173,194,70,139
108,161,188,213

and then the loop/condition works well.
Perhaps your awk version? Which one do you have? Give gsub ("\134\056", ",", $5)a shot ...

Last edited by RudiC; 12-24-2012 at 03:16 AM..
# 9  
Old 12-25-2012
Code:
$ awk --version
GNU Awk 4.0.1

This works from the command-line:

Code:
# netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//' | sed "s/\./\\\\./g" | awk '!x[$0]++'
173\.194\.64\.95
74\.125\.227\.158

However, when I run the code with /bin/bash -x the there are no backslashes to escape the dots and I still get "syntax error: invalid arithmetic operator". I also tried sending this straight to the $ip array in case the formatting was lost when passing from the $getip variable. Again I had the same results....
# 10  
Old 12-27-2012
If using sed anyhow, try this to replace dots by commas:
Code:
| sed -e 's/:[^:]*$//;s/\./,/g'

# 11  
Old 12-28-2012
Don't ask me how or why, but this seemed to get around the problem of the dots in the ip addresses coming back as numbers instead of strings:
Code:
#!/bin/bash -x  fox="firefox"  declare -a white  while true  do  sleep 2    stat=`netstat -antup | awk '{ print $7 }' | grep firefox | sed -ne "s/^[^\/]\+\///p" | awk '!x[$0]++';`   b=$stat       if [ "$b" = "$fox" ];      then       ip+=`netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//' | awk '!x[$0]++';`          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"             # clear array to avoid duplicates            unset ip             fi          done done  fi  done

The only problem now seems to be that all the ip addresses from this line are being thrown into the first element of the array instead of individual elements for each ip:
Code:
ip+=`netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//' | awk '!x[$0]++';`

I confirmed this by just echoing the first element:
Code:
echo -e "${ip[0]}\n"

Anyone know how to split these into individual elements? I tried using the space between them as a delimiter with cut but it didn't seem to work.

Last edited by Azrael; 12-29-2012 at 04:30 PM..
# 12  
Old 12-29-2012
Your ip+=... contruct is not an array assignment. In bash, try ip=( $(. . .) ) to create an array.
This User Gave Thanks to RudiC For This Post:
# 13  
Old 12-29-2012
Um... I believe that array+= does assign values to an array as I have successfully used it in other projects before. However, I did get this worked out. I just had to use tr to set the space in between the ip's as a delimiter making them each separate values:

Code:
getip=`netstat -antup | grep firefox | awk '{ print $5 }' | sed -e 's/:[^:]*$//;s/\./,/g;' | awk '!x[$0]++';`
ip=(`echo $getip | tr " " "\n"`)

As you can see I did have to come back and convert the dots to commas in the end. Thank you for your help!
# 14  
Old 12-29-2012
+= sounds more like makefile than BASH syntax.
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