2023-03-07 10:57:42 +01:00
|
|
|
#!rsc by RouterOS
|
|
|
|
|
# RouterOS script: mod/ipcalc
|
2025-01-02 00:04:06 +01:00
|
|
|
# Copyright (c) 2020-2025 Christian Hesse <mail@eworm.de>
|
2025-01-24 20:46:12 +01:00
|
|
|
# https://rsc.eworm.de/COPYING.md
|
2023-04-04 19:22:44 +02:00
|
|
|
#
|
2025-02-07 17:39:48 +01:00
|
|
|
# requires RouterOS, version=7.15
|
2024-04-04 21:45:02 +02:00
|
|
|
#
|
2023-04-04 19:22:44 +02:00
|
|
|
# ip address calculation
|
2025-01-24 20:46:12 +01:00
|
|
|
# https://rsc.eworm.de/doc/mod/ipcalc.md
|
2023-03-07 10:57:42 +01:00
|
|
|
|
|
|
|
|
:global IPCalc;
|
|
|
|
|
:global IPCalcReturn;
|
|
|
|
|
|
|
|
|
|
# print netmask, network, min host, max host and broadcast
|
2025-05-06 09:45:14 +02:00
|
|
|
:set IPCalc do={ :onerror Err {
|
2023-03-07 10:57:42 +01:00
|
|
|
:local Input [ :tostr $1 ];
|
|
|
|
|
|
2023-04-21 00:12:31 +02:00
|
|
|
:global FormatLine;
|
2023-03-07 10:57:42 +01:00
|
|
|
:global IPCalcReturn;
|
|
|
|
|
|
|
|
|
|
:local Values [ $IPCalcReturn $1 ];
|
|
|
|
|
|
2024-07-11 23:13:50 +02:00
|
|
|
:put [ :tocrlf ( \
|
2023-04-21 00:12:31 +02:00
|
|
|
[ $FormatLine "Address" ($Values->"address") ] . "\n" . \
|
|
|
|
|
[ $FormatLine "Netmask" ($Values->"netmask") ] . "\n" . \
|
|
|
|
|
[ $FormatLine "Network" ($Values->"network") ] . "\n" . \
|
|
|
|
|
[ $FormatLine "HostMin" ($Values->"hostmin") ] . "\n" . \
|
|
|
|
|
[ $FormatLine "HostMax" ($Values->"hostmax") ] . "\n" . \
|
2024-07-11 23:13:50 +02:00
|
|
|
[ $FormatLine "Broadcast" ($Values->"broadcast") ]) ];
|
2025-05-06 09:45:14 +02:00
|
|
|
} do={
|
|
|
|
|
:global ExitError; $ExitError false $0 $Err;
|
2024-12-09 09:10:27 +01:00
|
|
|
} }
|
2023-03-07 10:57:42 +01:00
|
|
|
|
|
|
|
|
# calculate and return netmask, network, min host, max host and broadcast
|
|
|
|
|
:set IPCalcReturn do={
|
|
|
|
|
:local Input [ :tostr $1 ];
|
2025-10-16 10:42:23 +02:00
|
|
|
|
|
|
|
|
:global NetMask4;
|
2025-10-16 17:31:15 +02:00
|
|
|
:global NetMask6;
|
2025-10-16 10:42:23 +02:00
|
|
|
|
2025-10-16 17:31:15 +02:00
|
|
|
:local Address [ :pick $Input 0 [ :find $Input "/" ] ];
|
2023-03-07 10:57:42 +01:00
|
|
|
:local Bits [ :tonum [ :pick $Input ([ :find $Input "/" ] + 1) [ :len $Input ] ] ];
|
2025-10-16 17:31:15 +02:00
|
|
|
:local Mask;
|
|
|
|
|
:local One;
|
|
|
|
|
:if ([ :typeof [ :toip $Address ] ] = "ip") do={
|
|
|
|
|
:set Address [ :toip $Address ];
|
|
|
|
|
:set Mask [ $NetMask4 $Bits ];
|
|
|
|
|
:set One 0.0.0.1;
|
|
|
|
|
} else={
|
|
|
|
|
:set Address [ :toip6 $Address ];
|
|
|
|
|
:set Mask [ $NetMask6 $Bits ];
|
|
|
|
|
:set One ::1;
|
|
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2025-10-16 17:31:15 +02:00
|
|
|
:local Return ({
|
2023-03-07 10:57:42 +01:00
|
|
|
"address"=$Address;
|
|
|
|
|
"netmask"=$Mask;
|
|
|
|
|
"networkaddress"=($Address & $Mask);
|
|
|
|
|
"networkbits"=$Bits;
|
|
|
|
|
"network"=(($Address & $Mask) . "/" . $Bits);
|
2025-10-16 17:31:15 +02:00
|
|
|
"hostmin"=(($Address & $Mask) | $One);
|
|
|
|
|
"hostmax"=(($Address | ~$Mask) ^ $One);
|
2023-03-07 10:57:42 +01:00
|
|
|
"broadcast"=($Address | ~$Mask);
|
2025-10-16 17:31:15 +02:00
|
|
|
});
|
2023-03-07 10:57:42 +01:00
|
|
|
|
|
|
|
|
:return $Return;
|
|
|
|
|
}
|