Community discussions

MikroTik App
 
User avatar
SiB
Forum Guru
Forum Guru
Topic Author
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

How properly ignore value from dhcp get "array, no such item"

Thu Oct 06, 2022 4:46 pm

Hi

My script stops on entry in dhcp who are not exist.
I try check if in dhcp leases exist IP address and have a host-name and get it if possible.
ip dhcp-server lease get [find address=10.5.10.51] host-name
no such item
put [ip dhcp-server lease find address=10.5.10.51 ]

put [:typeof [ip dhcp-server lease find address=10.5.10.51 ]]
array
 
User avatar
SiB
Forum Guru
Forum Guru
Topic Author
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: How properly ignore value from dhcp get "array, no such item"  [SOLVED]

Thu Oct 06, 2022 5:09 pm

# entry who not exist in dhcp leases
put [typeof [ip dhcp-server lease get [find address=10.5.20.1] host-name ]]
no such item

# entry who exist but not have hostname
put [typeof [ip dhcp-server lease get [find address=10.5.20.2] host-name ]]
nil

proper entry
put [typeof [ip dhcp-server lease get [find address=10.5.20.3] host-name ]]
str

--------

len -- return number of elements in value

When element not exist then len=0, when exist len=1
put [len [ip dhcp-server lease find address=10.5.20.1 ]]
0
put [len [ip dhcp-server lease find address=10.5.20.2 ]]
1
put [len [ip dhcp-server lease find address=10.5.20.3 ]]
1

This means final SOLUTION is:
/ip dhcp-server lease ; :if ([:len [find address=10.5.20.3]] =1 and [:typeof [get [find address=10.5.20.3] host-name ]] = "str" ) do={:put [get [find address=10.5.20.3] host-name ] }
swR1core2
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How properly ignore value from dhcp get "array, no such item"

Fri Oct 07, 2022 2:28 am

For title and OP question:
:put ([print as-value where address=10.5.20.3]->0->"host-name")

For the other:
:put ([print as-value where address=10.5.20.3 and [:typeof $"host-name"]="str"]->0->"host-name")

Who is online

Users browsing this forum: JDF, UkRainUa and 30 guests