diff options
author | Christian Hesse <mail@eworm.de> | 2020-06-23 13:19:27 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2020-06-23 13:19:27 +0200 |
commit | 38760d05b0c5e771bfa1714c0ebde061d0611983 (patch) | |
tree | c47b7f22428369319996a1cf07d8b3bbcae3bae9 /global-functions | |
parent | 71ad56aaccecb40253e8e697ff36e17b508bb927 (diff) |
global-functions: add $IPCalc
[admin@MikroTik] > $IPCalc 192.168.88.188/20
Address: 192.168.88.188
Netmask: 255.255.240.0
Network: 192.168.80.0/20
HostMin: 192.168.80.1
HostMax: 192.168.95.254
Broadcast: 192.168.95.255
Diffstat (limited to 'global-functions')
-rw-r--r-- | global-functions | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/global-functions b/global-functions index c3cab0c..eaf4532 100644 --- a/global-functions +++ b/global-functions @@ -30,6 +30,7 @@ :global GetMacVendor; :global GetRandomNumber; :global GetRandomSha256; +:global IPCalc; :global LogPrintExit; :global MailServerIsUp; :global MkDir; @@ -335,6 +336,22 @@ :return $FingerPrint; } +# calculate and print netmask, network, min host, max host and broadcast +:set IPCalc do={ + :local Input [ :tostr $1 ]; + :local Address [ :toip [ :pick $Input 0 [ :find $Input "/" ] ] ]; + :local Bits [ :tonum [ :pick $Input ([ :find $Input "/" ] + 1) [ :len $Input ] ] ]; + :local Mask ((255.255.255.255 << (32 - $Bits)) & 255.255.255.255); + + :put ( \ + "Address: " . $Address . "\n\r" . \ + "Netmask: " . $Mask . "\n\r" . \ + "Network: " . ($Address & $Mask) . "/" . $Bits . "\n\r" . \ + "HostMin: " . (($Address & $Mask) | 0.0.0.1) . "\n\r" . \ + "HostMax: " . (($Address | ~$Mask) ^ 0.0.0.1) . "\n\r" . \ + "Broadcast: " . ($Address | ~$Mask)); +} + # log and print with same text, optionally exit :set LogPrintExit do={ :local Severity [ :tostr $1 ]; |