Please i need help for understanding funcions

Hi, i have some trouble for understanding how correctly works a routeros scripting funcion.

I have this small piece of code that i want to implement in some of my existing scripts.

do { 
global aIP "10.50.0.191"

global aID "Test"

global logftp "ftp/$aID_ftp.log"

global UPL do={/tool fetch upload=yes url="sftp://$aIP/$1" src-path="$2" user="admin-ssh" keep-result=no;}

execute file=$logftp script=[$UPL dude_rsa.pub server.pub]

}

I dont understand why if i paste this on a terminal it work perfectly:
status: finished
uploaded: 0KiB
total: 0KiB
duration: 1s

If i paste this on routeros script and try to run it i give this error:

system/script/run “test func”
failure: invalid URL

Somebody can help me please?

To work from a script, you do not need to enclose the script in a local scope. Remove do { … }

I have removed it as you suggestion, nothing has changed unfortunately still does not work.

Do not provide false advice, is the “do” that is useless on both terminal or script,
but using { } on script or on terminal do not change nothing.

You omitted the rest of the script, I see from my magic sphere that that’s just a sliver of the script, not all of it.

put [system/script/get "test func" source]
global aIP "10.50.0.191"

global aID "Test"

global logftp "ftp/$aID_ftp.log"

global UPL do={/tool fetch upload=yes url="sftp://$aIP/$1" src-path="$2" user="admin-ssh" keep-result=no;}

execute file=$logftp script=[$UPL "dude_rsa.pub" "server.pub"]

Nothing else.

Ok, so is just write bad…

You forgot the “:” everywhere,
you forgot to declare global variables (used) inside the script,
you used the “;” unnecessarily
“do” uselessly say nothing to… do

Correct script:
{
:local aIP “10.50.0.191”
:local aID “Test”
:local logftp “ftp/$aID_ftp.log”

:local UPL do={ /tool fetch upload=yes url=“sftp://$1/$2” src-path=“$3” user=“admin-ssh” keep-result=no }

:execute file=$logftp script=[$UPL $aIP dude_rsa.pub server.pub]
}
(uselessly) alternative version
{
:global aIP “10.50.0.191”
:local aID “Test”
:local logftp “ftp/$aID_ftp.log”

:local UPL do={ :global aIP ; /tool fetch upload=yes url=“sftp://$aIP/$1” src-path=“$2” user=“admin-ssh” keep-result=no }

:execute file=$logftp script=[$UPL dude_rsa.pub server.pub]
}

Now it will work.
:local UPL do={ :global aIP ; /tool fetch upload=yes url=“sftp://$aIP/$1” src-path=“$2” user=“admin-ssh” keep-result=no }

And even the Mikrotik Scripts manual does not welcome wrapping global variables in local scopes.

Obviously the script at the end do error because the result on the “/tool fetch” is not one paseable script for execute…

Hi, now is working but the created ftp log file is empty, i would use it for search if inside this file there is the word "finished":
local logres [/file get [find name="$logftp.txt"] contents]
:if ($logres~"finished") do={

put "SSH key sended correctly"

} else {


:error "Error sending SSH key"

}

It’s obvious, the result of the fetch is not one “script” that you can provide to execute…

the rapid soluction for this case is this, but is not longer “a function”…
{
:local aIP “10.50.0.191”
:local aID “Test”
:local logftp “ftp/$aID_ftp.log”

:local UPL do={ }

:execute file=$logftp script=“/tool fetch upload=yes url="sftp://$aIP/dude_rsa.pub" src-path="server.pub" user="admin-ssh" keep-result=no”
}
One example to manage fetch results:
http://forum.mikrotik.com/t/fetch-capable-of-following-redirects/151723/7

Meanwhile, thank you very much for your support, it would have been useful to have a function to call within the script to be able to reuse it in several parts of the program, but if there is no way I will use your method, in the end I just want the script to be reliable.

I can’t test it. But wouldn’t this work with parameters?


:global UPL do={
:local logftp "ftp/$aID_ftp.log"
:execute file=$logftp script="/tool fetch upload=yes url=\"sftp://$aIP/dude_rsa.pub\" src-path=\"server.pub\" user=\"admin-ssh\" keep-result=no"
}

$UPL aIP="10.50.0.191" aID="Test"