i want to get some IP from my commented lease IP. When i did like this, i get all those INFO, but how to get from this only IP
[admin@site1] > :global c3 "PC4SEARCH"
[admin@site1] >/ip dhcp-server lease print where comment=$c3;
Flags: X - disabled, R - radius, D - dynamic, B - blocked
# ADDRESS MAC-ADDRESS H SE.. R STATUS LAST-SEEN
0 ;;; PC4SEARCH
10.111.116.40 00:1B:44:11:3A:B7 D dh.. bound 5m53s
RouterOS ver 7 or higher
:global c3 "PC4SEARCH"
:foreach i in=[/ip/dhcp-server/lease/find where comment~$c3] do={:put ([/ip/dhcp-server/lease/get $i address])}
RouterOS ver 6
:global c3 "PC4SEARCH"
:foreach i in=[/ip dhcp-server lease find where comment~$c3] do={:put ([/ip dhcp-server lease get $i address])}
Jotne
December 31, 2021, 4:48pm
4
RouterOS v7 understand v6 script so this works for both:
:global c3 "PC4SEARCH"
:foreach i in=[/ip dhcp-server lease find where comment="$c3"] do={:put ([/ip dhcp-server lease get $i address])}
And recommend to change from
~$c3
that means contain (example it will hits on “PC4SEARCHTest”) some to equal some
="$c3"
RouterOS v7 understand v6 script so this works for both:
:global c3 "PC4SEARCH"
:foreach i in=[/ip dhcp-server lease find where comment="$c3"] do={:put ([/ip dhcp-server lease get $i address])}
And recommend to change from
~$c3
that means contain (example it will hits on “PC4SEARCHTest”) some to equal some
="$c3"
this output me IPs, but if i want to put them into variable to use it in firewall how to do it?
Jotne
January 19, 2022, 8:59am
6
Instead if put, use set to put it inn to a variable.
:global c3 "PC4SEARCH"
:local IP
:foreach i in=[/ip dhcp-server lease find where comment="$c3"] do={:set $IPAddress ([/ip dhcp-server lease get $i address])}
Instead if put, use set to put it inn to a variable.
:global c3 "PC4SEARCH"
:local IP
:foreach i in=[/ip dhcp-server lease find where comment="$c3"] do={:set $IPAddress ([/ip dhcp-server lease get $i address])}
when i env print nothing showed, only c3 variable
edit:
this is how it works
:global c3 "PC4SHOPS"
:global IP
:foreach i in=[/ip dhcp-server lease find where host-name="$c3"] do={:set $IP ([/ip dhcp-server lease get $i address])}
but if I wanted to add it to address list it only generate ONE ip, not ALL that find
/ip firewall address-list add address=$IP list=shops
Jotne
January 19, 2022, 9:29am
8
Its how local variable work.
Global variable are “permanent” and works everywhere
Local variable works fine in script, but if you cut an past it to a terminal session, you need to put it in brakets like this:
{
:local c3 "PC4SHOPS"
:local IP
:foreach i in=[/ip dhcp-server lease find where host-name="$c3"] do={:set $IP ([/ip dhcp-server lease get $i address])}
}
Or some better formatting:
{
:local c3 "PC4SHOPS"
:local IP
:foreach i in=[/ip dhcp-server lease find where host-name="$c3"] do={
:set $IP ([/ip dhcp-server lease get $i address])
:put $IP
}
}
Its how local variable work.
Global variable are “permanent” and works everywhere
Local variable works fine in script, but if you cut an past it to a terminal session, you need to put it in brakets like this:
{
:local c3 "PC4SHOPS"
:local IP
:foreach i in=[/ip dhcp-server lease find where host-name="$c3"] do={:set $IP ([/ip dhcp-server lease get $i address])}
}
Or some better formatting:
{
:local c3 "PC4SHOPS"
:local IP
:foreach i in=[/ip dhcp-server lease find where host-name="$c3"] do={
:set $IP ([/ip dhcp-server lease get $i address])
:put $IP
}
}
I am not having problem with formating, its ok, but if I want to put all those to some address list, how to get them all, if i put variable, it added me just one IP
Jotne
January 19, 2022, 11:49am
10
This will add one by one IP to the address list “Blocking”
{
:local c3 "PC4SHOPS"
:local IP
:foreach i in=[/ip dhcp-server lease find where host-name="$c3"] do={
:set $IP ([/ip dhcp-server lease get $i address])
/ip firewall address-list add list="Blocking" address=$IP
}
}
RouterOS ver 7 or higher
:global c3 "PC4SEARCH"
:foreach i in=[/ip/dhcp-server/lease/find where comment~$c3] do={:put ([/ip/dhcp-server/lease/get $i address])}
RouterOS ver 6
:global c3 "PC4SEARCH"
:foreach i in=[/ip dhcp-server lease find where comment~$c3] do={:put ([/ip dhcp-server lease get $i address])}
why (i tried on v6) when i paste it to terminal, nothing is been outpouted to screen?
Jotne
October 6, 2022, 11:59am
13
No need to use global variable if its not intended to use in other script or use it later. (it will work with global)
Remove some /
Try this:
:local c3 "PC4SEARCH"
:foreach i in=[/ip dhcp-server lease find where comment~$c3] do={:put ([/ip dhcp-server lease get $i address])}
PS Pasting to terminal, you need to wrap it in {}
{
:local c3 "PC4SEARCH"
:foreach i in=[/ip dhcp-server lease find where comment~$c3] do={:put ([/ip dhcp-server lease get $i address])}
}
Or
{
:local c3 "PC4SEARCH"
/ip dhcp-server lease
:foreach i in=[find where comment~$c3] do={:put ([get $i address])}
}
Is it possible to put result to file?
I tried with one single line
{{ :local c3 "PC4SEARCH"; :foreach i in=[/ip dhcp-server lease find where host-name~$c3] do={:put ([/ip dhcp-server lease get $i address])} }} file=test.txt
but command don’t work.