Community discussions

MikroTik App
 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

ip-scan

Tue Jan 16, 2024 4:08 pm

Is there a way to save the output of ip-scan executed from a script?
 
optio
Long time Member
Long time Member
Posts: 675
Joined: Mon Dec 26, 2022 2:57 pm

Re: ip-scan

Tue Jan 16, 2024 7:23 pm

Add additional command argument as-value and output then can be passed to variable or other command argument. It will not be same output as printed on CLI, as-value returns key-value array, but that can be formatted for some file output if needed.
 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

Re: ip-scan

Wed Jan 17, 2024 12:28 am

Thank you but I don't know how to work with as-value.

I tried researching, but the explanations are of limited use.
 
optio
Long time Member
Long time Member
Posts: 675
Joined: Mon Dec 26, 2022 2:57 pm

Re: ip-scan

Wed Jan 17, 2024 7:32 pm

Example:
:local filename "scan.txt"
:local records ""

:foreach scan in=[/tool/ip-scan duration=10s as-value] do={
  :local record "IP: $($scan->"address"), MAC: $($scan->"mac-address")"
  :put $record
  :set records "$records$record\n"
}

:if ([:len records] > 0) do={
  /file
  :if ([:tobool [find where name=$filename]]) do={
    remove $filename
  }
  add contents="IP scan records:\n$records" name=$filename
}
this will iterrate key-value array from command
/tool/ip-scan duration=10s as-value
result (modify this depending on your needs), print records in CLI and if records are populated (if ip-scan command did found some addresses) will save to (overwrite) file named in filename variable (scan.txt) with content header "IP scan records:\n" (you can change this in add contents=... command).
Key-value array from this ip-scan result can contain values in keys:
address
,
dns
,
mac-address
,
netbios
and
snmp
, you can modify record string variable in format depending on your needs and what you want to extract for each scan record (example uses only values from address and mac-address keys).

Who is online

Users browsing this forum: Bing [Bot], Jotne and 10 guests