MLSC
MLSC

Reputation: 5972

Bash script to show range of IP addresses

I want to write a bash script in order to get two IP addresses and shows me whole range between them...

I'm trying but unfortunately no result...

Could you possibly help me?

I found this one but not complete:

addresses=( `< listofnums` )
network=${addresses[0]%.*}
hosts=( ${addresses[@]##*.} )

for (( i=${hosts[0]}; i<255; ++i ))
do
        case "${hosts[@]}" in *"$i"*) ;; *) echo "$network.$i" ;; esac
done

This script would get for example 1.1.1.1 5.4.6.3 and cat me whole range between them.

Upvotes: 0

Views: 698

Answers (1)

Jacobo de Vera
Jacobo de Vera

Reputation: 1933

Use the functions in this answer: https://stackoverflow.com/a/3222521/116957

And then this should give you what you want:

read -p "IP1> " ip1
read -p "IP2> " ip2

ip1n=$(INET_ATON $ip1)
ip2n=$(INET_ATON $ip2)

if [[ $ip2n -lt $ip1n ]]; then
    echo "Wrong range: $ip1 - $ip2"
    exit 1;
fi

for ipn in $(seq $ip1n $ip2n)
do
    INET_NTOA $ipn
done

Upvotes: 1

Related Questions