Community discussions

MikroTik App
 
meconiotronic
newbie
Topic Author
Posts: 30
Joined: Wed Mar 14, 2012 9:50 am

Please i need help for understanding funcions

Wed Apr 12, 2023 12:23 pm

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?
 
User avatar
Sertik
Member
Member
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 12:57 pm

To work from a script, you do not need to enclose the script in a local scope. Remove do { ... }
 
meconiotronic
newbie
Topic Author
Posts: 30
Joined: Wed Mar 14, 2012 9:50 am

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:02 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:06 pm

To work from a script, you do not need to enclose the script in a local scope. Remove do { ... }
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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:08 pm

still does not work.
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.
Last edited by rextended on Wed Apr 12, 2023 1:14 pm, edited 3 times in total.
 
meconiotronic
newbie
Topic Author
Posts: 30
Joined: Wed Mar 14, 2012 9:50 am

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:13 pm

still does not work.
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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:14 pm

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:

not tested code

{
: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

not tested code

{
: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]
}
Last edited by rextended on Wed Apr 12, 2023 1:21 pm, edited 2 times in total.
 
User avatar
Sertik
Member
Member
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:19 pm

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.
Last edited by Sertik on Wed Apr 12, 2023 1:22 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:21 pm

Obviously the script at the end do error because the result on the "/tool fetch" is not one paseable script for execute......
Last edited by rextended on Wed Apr 12, 2023 1:44 pm, edited 1 time in total.
 
meconiotronic
newbie
Topic Author
Posts: 30
Joined: Wed Mar 14, 2012 9:50 am

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:23 pm

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:

not tested code

{
: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

not tested code

{
: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]
}
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"

}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 1:50 pm

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"....

untested code

{
: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:
viewtopic.php?f=2&t=178355&p=878643#p878643
 
meconiotronic
newbie
Topic Author
Posts: 30
Joined: Wed Mar 14, 2012 9:50 am

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 2:03 pm

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"....

untested code

{
: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:
viewtopic.php?f=2&t=178355&p=878643#p878643
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.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3250
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Please i need help for understanding funcions

Wed Apr 12, 2023 9:35 pm

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"

Who is online

Users browsing this forum: No registered users and 13 guests