Community discussions

MikroTik App
 
mvalsasna
just joined
Topic Author
Posts: 18
Joined: Tue Sep 30, 2014 2:10 pm

capture output of ssh remote command

Tue Jan 03, 2017 11:50 am

Hi

I'm trying to connect from one router to another to get wifi signal-strength
:global GoldenUser; :global GoldenIP
:local myMac E4:8D:8C:26:DC:03
do {
    :local out [[ /system ssh user=$GoldenUser $GoldenIP ":put [/interface wireless registration-table get [ find mac-address=$myMac ]  signal-strength ]" ]]
    :put "out:$out:"
} on-error={
    :put "ERR"
}
but when I run the script it seems that the remote command is run but, it does not generate an error, but its output is not captured.
looks like /system ssh sends the remote command output to stderr instead of stdout, so it is printed to console but not captured by the script
/system script> run test         
-78dBm@6Mbps

Welcome back!
out::
any hints?

thanks

MAtteo
 
User avatar
Sinan
Trainer
Trainer
Posts: 129
Joined: Tue Sep 01, 2015 7:00 am

Re: capture output of ssh remote command

Wed Jan 04, 2017 10:52 pm

Try to put the result in a file then get it by fetch tool
 
mvalsasna
just joined
Topic Author
Posts: 18
Joined: Tue Sep 30, 2014 2:10 pm

Re: capture output of ssh remote command

Tue Jan 10, 2017 4:09 pm

not sure if there is a feature to redirect the output to a file (afterall, routeros is Linux-based).
can you try this..
/system script> run test file=someoutput.txt
and see if that is what you need?
parameter is not supported:
[toor@PEAMI-GW] /system script> run test file=someoutput.txt
expected end of command (line 1 column 10)
version: 6.36.4 (bugfix)
 
User avatar
PaulsMT
MikroTik Support
MikroTik Support
Posts: 282
Joined: Tue Feb 10, 2015 3:21 pm

Re: capture output of ssh remote command

Tue Jan 10, 2017 4:53 pm

SSH command output can be saved with "output-to-file" option.

Any other script/command output you can save with :execute command:

For example - save output of your script:

The script:
/system script add name=ScriptExample source={
/system clock print
}

Execute script, and save output:
{:local a [/system script get ScriptExample source]; execute script=$a file=OutputOfScript}

Or simply save output of any command:
:execute script={/system clock print} file=OutputOfCommand
 
mvalsasna
just joined
Topic Author
Posts: 18
Joined: Tue Sep 30, 2014 2:10 pm

Re: capture output of ssh remote command

Tue Jan 10, 2017 7:12 pm

SSH command output can be saved with "output-to-file" option.


1) true, thanks (meanwhile I had found it out too)

2) but there is another quirk: the file content is not immediately available, you need to insert a delay between creating the file and reading it (sic):
:global GoldenUser; :global GoldenIP;
:local filename outfile
:local myMac E4:8D:8C:26:DC:03
#:local myMac 04:F0:21:1D:77:70
do {
/file remove out
} on-error={
    :put "file already deleted"
}
do {
    /system ssh user=$GoldenUser $GoldenIP ":put [/interface wireless registration-table get [ find mac-address=$myMac ]  signal-strength ]" output-to-file=$filename
} on-error={
    :put "ERR"
}
:delay 1
:local result 
:set result [ /file get [ find name=$filename ] contents ]
:put "res:$result:$filename"
(without :delay the script fails!)

3) this script runs fine from the console, but not if launched remotely via SSH:

from console:
[root@router] /system script> run test         
file already deleted

Welcome back!
res:-85dBm@6Mbps
:outfile
launching from SSH, the outbound ssh connection seems to fail (out file is not created)
$ ssh root@ROUTER_IP "/system script run test"
root@ROUTER_IP's password: 
file already deleted
ERR
res:-85dBm@6Mbps
:outfile
any hint on why this happens/how to fix it?

5) also if I run the script trough :execute it fails to connect via ssh
[root@router] /system script> {:local a [/system script get test  source]; :execute script=$a file=OutputOfScript} 
[root@router] /system script> :put [ /file get OutputOfScript.txt  contents ]                                      
file already deleted
ERR
no such item
it seems that there is something really hard about using outbound SSH in a script, or possibly this could be the result of some security measure

Who is online

Users browsing this forum: No registered users and 56 guests