D
D
DjimiDjimi2020-05-28 19:33:30
linux
DjimiDjimi, 2020-05-28 19:33:30

How to compare an IP address with a given mask?

How to compare in bash ip with a mask in my case so that ip has the address 192.168.10.*?
For example, the $ip variable is 192.168.10.5, not 127.0.0.1.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir Korotenko, 2020-05-28
@firedragon

put ipcalc will help
but in your case
192.168.10.0/24

D
Dmitry, 2020-05-28
@q2digger

For debian there used to be such a utility www.pc-tools.net/unix/grepcidr
Maybe it will help.

A
Alexey Kharchenko, 2020-05-28
@AVX

Well, something like this, PATTERN for grep / egrep think out for your own conditions

[[email protected] ~]$ ip=192.168.10.5 
[[email protected] ~]$ mask=192.168.10.[0-9]
[[email protected] ~]$ echo $ip | egrep $mask
192.168.10.5

A
Alexey, 2020-05-28
@AlexeyKolodchenko

ip=192.168.10.5
fourth_octet=`echo $ip | awk -F "." '{print $4}'
echo $fourth_octet
so you get the fourth octet of the IP address, you can then compare it to anything...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question