Page 1 of 1

How to extract network variables?

Posted: Tue Feb 28, 2023 12:10 pm
by MitchelBraybh
In Linux, utilizing the commands "ipcalc" or "sipcalc" followed by an IP address and subnet mask allows me to extract helpful variables using "awk".

On RouterOS, I aim to assign the variable "10.0.88.0/24" to "network" and create variables for the minimum and maximum usable hosts, as well as the network address with a value "n" added to it.

As of now, I only know how to extract the network address by removing the subnet mask using the command: ":put [:pick $network 0 [:find $network "/"]]"

Is there a pre-existing script that accomplishes these tasks?

Re: How to extract network variables?

Posted: Tue Feb 28, 2023 1:01 pm
by rextended
Some of my "tools"
viewtopic.php?t=177551#p953746

For example for 10.31.42.56/16 (no matter the ".0.0" at the end)
{
: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"
}

out code

       Source: 10.31.42.56/16
           IP: 10.31.42.56
Subnet Prefix: 16
  Subnet Mask: 255.255.0.0
Address Space: 0.0.255.255
    Total IPs: 65536
  Network* IP: 10.31.0.0
Broadcast* IP: 10.31.255.255
    First* IP: 10.31.0.1
     Last* IP: 10.31.255.254
  Usable* IPs: 65534
* = 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.

And for 10.0.88.0/24

out code

       Source: 10.0.88.0/24
           IP: 10.0.88.0
Subnet Prefix: 24
  Subnet Mask: 255.255.255.0
Address Space: 0.0.0.255
    Total IPs: 256
  Network* IP: 10.0.88.0
Broadcast* IP: 10.0.88.255
    First* IP: 10.0.88.1
     Last* IP: 10.0.88.254
  Usable* IPs: 254

Re: How to extract network variables?

Posted: Fri Mar 03, 2023 6:59 am
by LansoirThemtq
I am not aware of any pre-existing script that accomplishes these specific tasks. However, you can use RouterOS scripting language to achieve this. Here is an example script that extracts the minimum and maximum usable hosts, as well as the network address with a value "n" added to it:
# Assign the network and subnet mask
:local network "10.0.88.0/24"

# Extract the network address
:local network_address [:pick $network 0 [:find $network "/"]]

# Calculate the subnet mask
:local subnet_mask [:pick $network ([:find $network "/"] + 1) 2]

# Calculate the number of available hosts
:local available_hosts (2 ** (32 - $subnet_mask)) - 2

# Calculate the minimum usable host
:local min_host [:toip [:tonum $network_address] + 1]

# Calculate the maximum usable host
:local max_host [:toip [:tonum $network_address + $available_hosts] - 1]

# Calculate the network address with a value "n" added to it
:local network_n_address [:toip [:tonum $network_address + $n]]

# Print the results
:put ("Network: " . $network)
:put ("Minimum usable host: " . $min_host)
:put ("Maximum usable host: " . $max_host)
:put ("Network address with n added: " . $network_n_address)

You can modify this script to suit your specific needs.

Re: How to extract network variables?

Posted: Fri Mar 03, 2023 11:49 am
by rextended
I am not aware of any pre-existing script that accomplishes these specific tasks.
So before answering, you don't read other people's posts...

Re: How to extract network variables?

Posted: Fri Mar 03, 2023 1:23 pm
by rextended
Is the trademark of ChatGPT invent instructions that do no exist...
:local available_hosts (2 ** (32 - $subnet_mask)) - 2
Do you know where you have to put that thing's answers?