Bash arrays that compare ip addresses.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash arrays that compare ip addresses.
# 15  
Old 12-30-2012
We're talking bash, notabene.
You can do that in one go: ip=( $(netstat...) )will assign array ip. Pls note the new form $(...) for command substitution.

ip+=sth will append string "sth" to variable $ip, so $ getip+=`netstat ...` twice will yield
Code:
$ echo "$getip"
173,194,78,95
173,194,78,100
2,19,63,139173,194,78,95
173,194,78,100
2,19,63,139

# 16  
Old 12-30-2012
Lof of noice, but was the idea to find each ip only once and then to do something ?

Need only ex. awk. Include regexp - no need to use awk - pipe - sed - pipe - awk ...

Code:
getip=$(netstat -antup 2>/dev/null | awk '
/firefox/ { 
        destip=$5
        sub(/:.*$/, "", destip)
        dest[destip]++
        }
END {
        for ( ip in dest ) print ip
        }
'
)


echo "$getip"

for ip in $getip
do
        echo "ip:$ip"
done

# 17  
Old 01-01-2013
Quote:
Originally Posted by Azrael
. . .
As you can see I did have to come back and convert the dots to commas in the end. Thank you for your help!
It is not the contents of $c that bothers bash, but it is the whitelist index containing the dots. So - try using a non-offending index scheme, e.g. removing the dots if that does not lead to ambiguities:
Code:
 if [ "$c" == "${white[${c//./}]}" ]

will work (c containing the ip with dots!):
Code:
+ for c in '"${ip[@]}"'
+ '[' 173.194.41.66 == '' ']'

while
Code:
if [ "$c" == "${white[${c}]}" ]    
./test: line 13: 173.194.41.66: syntax error: invalid arithmetic operator (error token is ".194.41.66")

crashes!
# 18  
Old 01-02-2013
Code:
# associative array bash 4.0 or ksh93
# bash --version
declare -A white   # bash
#typeset -A white # ksh93

for c in 192.168.10.20 192.168.20.20 192.168.10.20
do
        [ "${white[$c]}" = "$c" ] && echo "it's in the white"
        [ "${white[$c]}" != "$c" ] && white[$c]=$c
done

for c in "${!white[@]}"
do
        echo "$c"
done

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