I would like to ask if there is a way to identify the local ip through a script and automatically add multiple local ip

Every time I install a new machine, I need to check the IP address obtained by dhcp
Then execute the command to add 9 new IP addresses

For example, the obtained ip is : 192.168.0.101/24
no1.png
Then use the following command to add another 9 ip addresses:

:for i from 1 to 9 do { /ip address add address (“192.168.0.”.$i+101) interface ether1}
no2.png
Is there a way to make the script automate this process

If you get an IP 192.168.20.12, how do you know it part of 192.168.20.0/24 or 192.168.20.0/23?
Where do you get this IP?

This ip address is obtained through dhcp or has been preset on the network card

What is your goal?
You can export all IP from the Mikrotik DHCP on the router.

My purpose is to use a script to identify the existing ip address, and then automatically add another 10 ip addresses

I do not understand.
If a client connects to you router with IP 192.168.3.2, what do you like to add where and why?
Give the whole story from A to Z.

:for i from 1 to 9 do { /ip address add address (“192.168.3.”.$i+2) interface ether1}

Is there a way to make the script automate this process

I can satisfy your need,
but the price is to satisfy our curiosity: Why you need that? What is the final scope?

Thank you for your willingness to help me, because each internal network ip address corresponds to a public network ip address, but only the first one can be obtained automatically, and the latter need to be added manually

Ok… understanded.

Leave me some minutes to write the script.

The script must do:

  1. from dhcp client, read the IP obtained,
  2. understand the IP obtained “removing the subnet”
  3. add then 10 consecutive IP to same interface, with same subnet.
  4. when dhcp expire, remove all IP added.

Ok thank you very much

Paste this on dhcp-client / advanced / Script:


/ip address remove [find where comment="autoadded"]
:if ($bound = 1) do={
    :local iface $interface
    :local assignedip [/ip address get [find where interface=$iface] address]
    :local startip [:toip [:pick $assignedip 0 [:find $assignedip "/" -1]]]
    :local subnetip [:pick $assignedip ([:find $assignedip "/" -1] + 1) [:len $assignedip]]
    :for x from=1 to=10 step=1 do={
        :if ([:len [/ip address find where address="$($startip + $x)/$subnetip"]] = 0) do={
            /ip address add comment="autoadded" interface=$iface address="$($startip + $x)/$subnetip"
        }
    }
}

Caveat:
The script ignore on purpose the subnet mask.
If the IP is, for example, 192.168.88.247/24, this procedure add:
192.168.88.248/24
192.168.88.249/24

192.168.88.255/24
192.168.89.0/24
192.168.89.1/24


Remember:
When lease is added, or changed, the script, removing first previous IPs, add the IPs,
when lease is expired (and not renewed), released or removed, remove all IPs added,
when lease is renewed, do NOTHING (keep added IPs).
When the DHCP Client is deleted (removed) or disabled, the IPs are also deleted if the script is present.
On reboot the IPs are keeped, until the DHCP Client do not obtain again one lease.

I’m on office tomorrw, I read this post and do not appear again to me as non-readed.
Rememeber me tomorrow to write the script, or I hope someone on the forum reply to you first.

Thanks, I also hope you can help me, because so far no one has responded to me

is there a way to directly enter the command in the new terminal to complete

please explain, I do not understand this


or enter the command in the new terminal to save your script to “dhcp-client / advanced / Script”

Yes, if it is the only DHCP Client on device, can be pasted the script than which I will add in the next post.
If are presents more dhcp-client (I doubt) must be specified dhcp-client name.

To add the script to all dhcp-client on device, or if only one dhcp-client is present on device, you can paste this on new terminal / mac telnet client:

/ip dhcp-client
{
set [find] script="/ip address remove [find where comment=\"autoadded\"]\r\
    \n:if (\$bound = 1) do={\r\
    \n    :local iface \$interface\r\
    \n    :local assignedip [/ip address get [find where interface=\$iface] address]\r\
    \n    :local startip [:toip [:pick \$assignedip 0 [:find \$assignedip \"/\" -1]]]\r\
    \n    :local subnetip [:pick \$assignedip ([:find \$assignedip \"/\" -1] + 1) [:len \$assignedip]]\r\
    \n    :for x from=1 to=10 step=1 do={\r\
    \n        :if ([:len [/ip address find where address=\"\$(\$startip + \$x)/\$subnetip\"]] = 0) do={\r\
    \n            /ip address add comment=\"autoadded\" interface=\$iface address=\"\$(\$startip + \$x)/\$subnetip\"\r\
    \n        }\r\
    \n    }\r\
    \n}"
}



But if you want add the script to a single dhcp-client on device where are present multiple dhcp-client, you must specify on what dhcp-client you want put the script, specifing the interface

/ip dhcp-client
{
:local iface "ether1"
set [find where interface=$iface] script="/ip address remove [find where comment=\"autoadded\"]\r\
    \n:if (\$bound = 1) do={\r\
    \n    :local iface \$interface\r\
    \n    :local assignedip [/ip address get [find where interface=\$iface] address]\r\
    \n    :local startip [:toip [:pick \$assignedip 0 [:find \$assignedip \"/\" -1]]]\r\
    \n    :local subnetip [:pick \$assignedip ([:find \$assignedip \"/\" -1] + 1) [:len \$assignedip]]\r\
    \n    :for x from=1 to=10 step=1 do={\r\
    \n        :if ([:len [/ip address find where address=\"\$(\$startip + \$x)/\$subnetip\"]] = 0) do={\r\
    \n            /ip address add comment=\"autoadded\" interface=\$iface address=\"\$(\$startip + \$x)/\$subnetip\"\r\
    \n        }\r\
    \n    }\r\
    \n}"
}

EDIT: cosmetic change only: extra space between where and interface

Very good friend, thank you very much for solving my troubles

Very thanks