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>>$prefix))
:local addrspace (~$submask)
:local network ($ip & $submask)
:local broadcast ($ip | $addrspace)
:local first ($network + 1)
:local last ($broadcast - 1)
:put " Source: $source"
:put " IP: $ip"
:put "Subnet Prefix: $prefix"
:put " Subnet Mask: $submask"
:put "Address Space: $addrspace"
:put " Network* IP: $network"
:put "Broadcast* IP: $broadcast"
:put " First* IP: $first"
:put " Last* IP: $last"
}
* = 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 prefix 32
:local addrspace (~$submask)
:local tempsub [:tonum $addrspace]
:while ($tempsub > 0) do={:set tempsub ($tempsub / 2); :set prefix ($prefix - 1)}
:local network ($ip & $submask)
:local broadcast ($ip | $addrspace)
:local first ($network + 1)
:local last ($broadcast - 1)
:put " Source IP: $sourceip"
:put "Source Subnet: $sourcesub"
:put "Subnet Prefix: $prefix"
:put " Subnet Mask: $submask"
:put "Address Space: $addrspace"
:put " Network* IP: $network"
:put "Broadcast* IP: $broadcast"
:put " First* IP: $first"
:put " Last* IP: $last"
}
Convert IP to decimal number: (127.0.0.1 = 2130706433)
Convert decimal number to IP: (2130706433 = 127.0.0.1)
:put (0.0.0.0 + 2130706433)
Convert IP to hexadecimal number
Using this:
viewtopic.php?f=2&t=57665&p=868898#p868898
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)