开发者

Finding and printing ranges between IP addresses with bash

开发者 https://www.devze.com 2023-03-28 15:24 出处:网络
I have this ip addresses that are in use in a file: 192.168.1.121 192.168.1.141 192.168.1.172 192.168.1.180

I have this ip addresses that are in use in a file:

192.168.1.121 192.168.1.141 192.168.1.172 192.168.1.180 192.168.1.180 192.168.1.181 192.168.1.182 192.168.1.185 192.168.1.190

What I want to do is to be able to print out the ranges between used IP addresses with Bash. So for example by looking at the example 192.168.1.121 is used but I want to be able to print out all I开发者_高级运维P addresses between 121 and 141. Similarly between 141 and 172. I am not worried about printing IP addresses before 121 though.

Is there a way to do this ?

Thanks Kuti.


Yes.

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

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

精彩评论

暂无评论...
验证码 换一张
取 消