Community discussions

MikroTik App
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Fri Sep 03, 2021 4:20 pm

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
You do not have the required permissions to view the files attached to this post.
Last edited by cillininfo on Sat Sep 04, 2021 5:17 pm, edited 2 times in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Fri Sep 03, 2021 5:16 pm

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?
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Fri Sep 03, 2021 5:34 pm

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
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Fri Sep 03, 2021 6:01 pm

What is your goal?
You can export all IP from the Mikrotik DHCP on the router.
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Fri Sep 03, 2021 6:10 pm

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
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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

Fri Sep 03, 2021 7:33 pm

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.
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Sat Sep 04, 2021 5:19 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Sat Sep 04, 2021 5:25 pm

I can satisfy your need,
but the price is to satisfy our curiosity: Why you need that? What is the final scope?
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Sat Sep 04, 2021 6:10 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Sat Sep 04, 2021 6:11 pm

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.
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Sat Sep 04, 2021 6:23 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Sat Sep 04, 2021 6:40 pm

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.
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Sun Sep 05, 2021 6:05 pm

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"
        }
    }
}
Thank you very much,
This method can indeed solve my troubles, I want to ask again, is there a way to directly enter the command in the new terminal to complete; or enter the command in the new terminal to save your script to "dhcp-client / advanced / Script"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Sun Sep 05, 2021 7:43 pm

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.
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Mon Sep 06, 2021 11:48 am

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Mon Sep 06, 2021 1:33 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Mon Sep 06, 2021 1:41 pm

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
Last edited by rextended on Mon Sep 06, 2021 3:34 pm, edited 2 times in total.
 
cillininfo
just joined
Topic Author
Posts: 16
Joined: Thu Apr 23, 2020 11:09 am

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

Mon Sep 06, 2021 3:31 pm

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}"
}


Very good friend, thank you very much for solving my troubles
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Mon Sep 06, 2021 3:32 pm

Very thanks

Who is online

Users browsing this forum: No registered users and 15 guests