Community discussions

MikroTik App
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

How to get multiple data using "value-name"

Fri May 17, 2019 10:58 pm

This script does print comment, address and host-name from all dhcp lease
/ip dhcp-server lease
:foreach i in=[find] do={
: put "$[get value-name=comment $i] $[get value-name=address $i] $[get value-name=host-name $i]"
}
Can this be shorten in any way?

I was looking for some like the example below
value-name=(comment|address|host-name)
value-name=comment,address,host-name
value-name="comment address host-name"
But this does not work.
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: How to get multiple data using "value-name"

Sun May 19, 2019 2:08 am

Solved
:do {/ip dhcp-server lease
 :foreach i in=[find] do={
 :put ("comment=" . [get $i value-name=comment] . ", address=" . [get $i value-name=address] . ", host-name=" . [get $i value-name=host-name]);
 }
} on-error={}
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: How to get multiple data using "value-name"

Sun May 19, 2019 8:10 am

This was even more code than what I have used. Gol was to do it more simple. (shorten)
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: How to get multiple data using "value-name"

Mon May 20, 2019 3:20 pm

How about this?
:foreach i in=[ print as-value ] do={
  :put (($i->"comment") . " " . ($i->"address") . " " . ($i->"host-name"));
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: How to get multiple data using "value-name"

Mon May 20, 2019 3:31 pm

Interesting and shorter.

Trying to understand how it works ;)

To make sure it works in all location i would have used full command like this:
:foreach i in=[ /ip dhcp-server lease print as-value ] do={
  :put (($i->"comment") . " " . ($i->"address") . " " . ($i->"host-name"));
}
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: How to get multiple data using "value-name"

Mon May 20, 2019 4:23 pm

To make sure it works in all location i would have used full command like this:
Usually I do. I started from your code, but did not notice the first line was appended to your written text. :lol:
 
aliboy
newbie
Posts: 35
Joined: Thu Aug 01, 2019 9:09 pm

Re: How to get multiple data using "value-name"

Sun May 02, 2021 1:11 am

How can I make the results save to a file? My command prints the results on the terminal and creates the file but the file is empty.

:foreach i in=[ /ip/hotspot/user print file=aaaaaaaaaaa detail as-value where comment=admin ] do={:put (($i->"name") . " " . ($i->"password"));}
 
aliboy
newbie
Posts: 35
Joined: Thu Aug 01, 2019 9:09 pm

Re: How to get multiple data using "value-name"

Sun May 02, 2021 2:32 am

I would like the results to be saved on the file also aside from just showing on the terminal.

:foreach i in=[ /ip/hotspot/user print file=aaaaaaaaaaa detail as-value where comment=admin ] do={:put (("username: " . $i->"name") ." " . "password: " . ($i->"password"));}

username: 649c8 password: 26927
username: dexsm password: 46789
username: 4btjb password: 93555
username: ua67a password: 97954
username: t5w6n password: 65495
username: rr2wm password: 25265
username: kwmd3 password: 73223
username: 9yexa password: 43636
username: bnkxv password: 84966
username: xrmpb password: 88595
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to get multiple data using "value-name"

Mon May 03, 2021 12:33 am

I would like the results to be saved on the file also aside from just showing on the terminal.

:foreach i in=[ /ip/hotspot/user print file=aaaaaaaaaaa detail as-value where comment=admin ] do={:put (("username: " . $i->"name") ." " . "password: " . ($i->"password"));}

username: 649c8 password: 26927
username: dexsm password: 46789
username: 4btjb password: 93555
username: ua67a password: 97954
username: t5w6n password: 65495
username: rr2wm password: 25265
username: kwmd3 password: 73223
username: 9yexa password: 43636
username: bnkxv password: 84966
username: xrmpb password: 88595
DO NOT POST EVERYWHERE.

viewtopic.php?f=13&t=174941&p=855803#p855803
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to get multiple data using "value-name"

Mon May 03, 2021 12:41 am

This script does print comment, address and host-name from all dhcp lease
/ip dhcp-server lease
:foreach i in=[find] do={
: put "$[get value-name=comment $i] $[get value-name=address $i] $[get value-name=host-name $i]"
}
Can this be shorten in any way?

Short version from your example
/ip dhcp-s l
:fore i in=[f] do={:pu "$[g $i co] $[g $i address] $[g $i h]"}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to get multiple data using "value-name"

Mon May 03, 2021 1:00 am

How about this?
/ip dhcp-server lease
:foreach i in=[ print as-value ] do={
  :put (($i->"comment") . " " . ($i->"address") . " " . ($i->"host-name"));
}


Short version from your example
/ip dhcp-s l
:fore i in=[p as] do={:pu "$($i->"comment") $($i->"address") $($i->"host-name")"}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to get multiple data using "value-name"

Mon May 03, 2021 1:03 am

Joke apart

get value-name=comment $i
is equal to
get $i comment

and

"$(...) $(...) $(...)"
is better than
([...] . " " . [...] . " " . [...])

/ip dhcp-server lease
:foreach i in=[print as-value] do={:put "$($i->"comment") $($i->"address") $($i->"host-name")"}

Who is online

Users browsing this forum: No registered users and 15 guests