From 110a876cdcc0f121576275a9297b1f86435c3389 Mon Sep 17 00:00:00 2001 From: Von Random Date: Fri, 10 Feb 2023 00:54:56 +0200 Subject: [PATCH] add a script to test for private ip ranges --- is_private | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 is_private diff --git a/is_private b/is_private new file mode 100755 index 0000000..fcf47da --- /dev/null +++ b/is_private @@ -0,0 +1,27 @@ +#!/usr/bin/env zsh +typeset -ga PRIVATE_RANGES=( + 00001010 # 10.0.0.0/8 + 101011000001 # 172.16.0.0/12 + 1100000010101000 # 192.168.0.0/16 +) + +test_range() { + typeset net addr=$(printf '%.2x' ${(s:.:)1}) + for net in $PRIVATE_RANGES; do + ((16#$addr >> (32 - $#net) == 2#$net)) && return 0 + done +} + +report() { + typeset result='private' addr=$1 state=$2 + ((state)) && result='not private' + printf "%s is %s\n" $addr $result + exit $state +} + +main() { + test_range $1 && report $1 0 + report $1 1 +} + +main $@