summaryrefslogtreecommitdiff
path: root/src/validators/ipv4-range
blob: 0d707d6c555b6ef9e37159ea3d06b6da4451cb06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash

# snippet from https://stackoverflow.com/questions/10768160/ip-address-converter
ip2dec () {
    local a b c d ip=$@
    IFS=. read -r a b c d <<< "$ip"
    printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
}

# This only works with real bash (<<<) - split IP addresses into array with
# hyphen as delimiter
readarray -d - -t strarr <<< $1

ipaddrcheck --is-ipv4-single ${strarr[0]}
if [ $? -gt 0 ]; then
  exit 1
fi

ipaddrcheck --is-ipv4-single ${strarr[1]}
if [ $? -gt 0 ]; then
  exit 1
fi

start=$(ip2dec ${strarr[0]})
stop=$(ip2dec ${strarr[1]})
if [ $start -ge $stop ]; then
  exit 1
fi

exit 0