From 8e94814d0708d312293e94e6a35f4b5ad555c263 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 15 Jan 2022 10:53:33 +0800 Subject: zsh: Add ip2dec() and dec2ip() functions --- _zshrc | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to '_zshrc') diff --git a/_zshrc b/_zshrc index 085e6c1..0a27cc7 100644 --- a/_zshrc +++ b/_zshrc @@ -230,6 +230,16 @@ hex2ip() { printf '%d.%d.%d.%d' $(echo "${1#0[xX]}" | sed 's/\(..\)/0x\1 /g') } +## Convert IP between dotted-decimal and decimal notation +# Credit: https://stackoverflow.com/a/24136051 +ip2dec() { + echo "$1" | awk 'BEGIN { RS = "." } { d = d * 256 + $1 } END { print d }' +} +# Credit: https://stackoverflow.com/a/31281331 +dec2ip() { + printf '%d.%d.%d.%d' $(echo "obase=256; $1" | bc) +} + # # Terminal settings # -- cgit v1.2.2