summaryrefslogtreecommitdiff
path: root/src/validators/ipv4-range-mask
blob: 8fa4abe30dd22590473d882f9e9fc22ee4da4e16 (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
#!/bin/bash

error_exit() {
  echo "Error: $1 is not a valid IPv4 address range or these IPs are not under /$2"
  exit 1
}

# Check if address range is under the same netmask
# -m - mask
# -r - IP range in format x.x.x.x-y.y.y.y
while getopts m:r: flag
do
    case "${flag}" in
        m) mask=${OPTARG};;
        r) range=${OPTARG}
    esac
done

if [[ "${range}" =~ "-" ]]&&[[ ! -z ${mask} ]]; then
  ipaddrcheck --range-prefix-length "${mask}" --is-ipv4-range "${range}"
  if [ $? -gt 0 ]; then
    error_exit ${range} ${mask}
  fi
  exit 0
fi

error_exit ${range} ${mask}