Community discussions

MikroTik App
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

how to get only IP from print lease

Thu Dec 30, 2021 1:37 pm

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
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 723
Joined: Tue Dec 17, 2019 1:08 pm

Re: how to get only IP from print lease

Thu Dec 30, 2021 1:56 pm

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])}
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Thu Dec 30, 2021 3:57 pm

Thanks!
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: how to get only IP from print lease

Fri Dec 31, 2021 6:48 pm

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" 
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Wed Jan 19, 2022 10:50 am

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

Re: how to get only IP from print lease

Wed Jan 19, 2022 10:59 am

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])}
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Wed Jan 19, 2022 11:10 am

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

Re: how to get only IP from print lease

Wed Jan 19, 2022 11:29 am

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
}
}
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Wed Jan 19, 2022 1:01 pm

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

Re: how to get only IP from print lease

Wed Jan 19, 2022 1:49 pm

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
}
}
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Thu Jan 20, 2022 9:30 am

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
}
}
thanks a lot!!!
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Thu Oct 06, 2022 1:07 pm

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

Re: how to get only IP from print lease  [SOLVED]

Thu Oct 06, 2022 2:59 pm

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])}
}
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Thu Oct 06, 2022 3:02 pm

Thanks a lot!!!!
 
tomislav91
Member
Member
Topic Author
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: how to get only IP from print lease

Thu Oct 06, 2022 3:12 pm

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.

Who is online

Users browsing this forum: tuiespacecorp and 14 guests