Obtain some info from one IP, convert subnet mask to prefix, convert prefix to subnet mask, convert IP to num, convert num to IP, convert IP to HEX, convert HEX to IP etc.
If the IP have (do not have) subnet mask different from /32 (255.255.255.255)
other useful info can be obtained from this information.
(MikroTik actually do not support directly /31 addresses)
For example if you have this 10.31.42.56/16
{
:local source 10.31.42.56/16
:local ip [:toip [:pick $source 0 [:find $source "/"]]]
:local prefix [:tonum [:pick $source ([:find $source "/"] + 1) [:len $source]]]
:local submask (255.255.255.255<<(32 - $prefix))
:local addrspace (~$submask)
:local totip ([:tonum $addrspace] + 1)
:local network ($ip & $submask)
:local broadcast ($ip | $addrspace)
:local first (($network + 1) - ($prefix / 31))
:local last (($broadcast - 1) + ($prefix / 31))
:local usable (($last - $network) + ($prefix / 31))
:put " Source: $source"
:put " IP: $ip"
:put "Subnet Prefix: $prefix"
:put " Subnet Mask: $submask"
:put "Address Space: $addrspace"
:put " Total IPs: $totip"
:put " Network* IP: $network"
:put "Broadcast* IP: $broadcast"
:put " First* IP: $first"
:put " Last* IP: $last"
:put " Usable* IPs: $usable"
}
- = Network / Broadcast / First IP and Last IP are valid only when the IP are distribuited on local LAN,
instead for routing only, all IP can be used.
.0 and .255 are perfectly valid IP if are not the network ip or the broadcast address,
but for compatibility with some end devices that have problems with .0 and .255 outside a /24, is better remove all .0 and all .255 from the IP pools assigned from DHCP Server.
For example if you have this IP 10.31.42.56 and subnet 255.255.0.0
{
:local sourceip 10.31.42.56
:local sourcesub 255.255.0.0
:local ip [:toip $sourceip]
:local submask [:toip $sourcesub]
:local addrspace (~$submask)
:local tempsub [:tonum $addrspace]
:local prefix 32
:while ($tempsub > 0) do={:set tempsub ($tempsub / 2); :set prefix ($prefix - 1)}
:local totip ([:tonum $addrspace] + 1)
:local network ($ip & $submask)
:local broadcast ($ip | $addrspace)
:local first (($network + 1) - ($prefix / 31))
:local last (($broadcast - 1) + ($prefix / 31))
:local usable (($last - $network) + ($prefix / 31))
:put " Source IP: $ip"
:put " Source Mask: $submask"
:put "Subnet Prefix: $prefix"
:put "Address Space: $addrspace"
:put " Total IPs: $totip"
:put " Network* IP: $network"
:put "Broadcast* IP: $broadcast"
:put " First* IP: $first"
:put " Last* IP: $last"
:put " Usable* IPs: $usable"
}
Convert IP to decimal number: (127.0.0.1 = 2130706433)
:put [:tonum 127.0.0.1]
Convert decimal number to IP: (2130706433 = 127.0.0.1)
:put (0.0.0.0 + 2130706433)
Convert IP to hexadecimal number
Using this:
http://forum.mikrotik.com/t/how-to-covert-int-to-hex-type-value-and-save-it-in-a-string/52654/6
The IP can be converted first to decimal, then to hexadecimal.
:put [$num2hex [:tonum 127.0.0.1]]
Convert hexadecimal number to IP
:put (0.0.0.0 + 0x7F000001)