Send SMS from different router

:put [$scr2sshcmd 192.168.1.2 admin ip2bin (“”)]

:put [$scr2sshcmd $1 $2 $3 $4]

$1: IP (as string, but must be a valid IP)
$2: username (as string with length > 0)
$3: script name (as string with length > 0)
$4: extra instructions on command line (as quoted escaped string between parenthesys)
what return: “DONE” if is done, else nothing.

Well yes, that’s right.

:put [$scr2sshcmd 192.168.1.2 admin ip2bin (“”)]

:put [$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]")]

ok

:log warning ([$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]") as-value]->"output")

not works (does not return “DONE”).

Am I wrong again?

You made up commands that don’t exist.

you set as-value as 5th parameter, and the result is a string “DONE” not one array where you can read inside “output” value.

(as-value is usable only on system functions, and only where is supported and ssh-exec do not support as-value)

EDIT: on BOTH v6 AND until v7.7, FOR SURE.

simply

{
:local result [$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]")]
:log warning "The import is $result"
}
:local result [$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]")]
:log warning "The import is $result"

In this case $result is empty. She returns nothing.

Whereas, for example:

:global i ([/system ssh-exec address=192.168.1.2 user=admin command=":put [/system routerboard get model]" as-value ] -> "output")

:put $i
RouterBOARD M33G

returns the model RB successfully !

But at the same time

:log info ([/system ssh-exec address=192.168.1.2 user=admin command=":put [/system routerboard get model]" as-value ] -> "output")

Nothing is printed to the log. Why is not clear.


You can do like this:

:global i ([/system ssh-exec address=192.168.1.2 user=admin command=":put [\$ip2bin 127.0.0.1]" as-value ] -> "output")
:log warning $i

01111111000000000000000000000001


ssh-exec do not support as-value)

so as-value]->“output”) is supported /system ssh-exec

I have another question. My SSH script transfer function does not work, although everything seems to be correct. Here the task is not to immediately execute the function, but to take the text of the repository script, transfer it and create a script on the SSH server with the same text as the source. Then it can be executed if needed.
And this is not always the case. It transfers quite simple scripts normally.
I understand that this is unpleasant for you, but could you point out my mistake in the text of my script?

# $1 is the address of the remote SSH server
# $2 - username of remote SSH server for SSH connection
# $3 is the name of the source script on the SSH client
# $4 is the name of the executable script created on the SSH server
# $5 - run - if you need to run the passed script immediately, in this case, after execution, it is removed from the repository
# if $5 is not set, the passed script is placed in the repository and is not executed.



:global SSHscript do={

:local ReplaceChars do={
    :local cmd
    :for idx from=0 to=([:len $1] - 1) do={
        :local char [:pick $1 $idx]
        :if ($char~"(\a|\b|\f|\n|\r|\v)") do={:set char ";"}
        :if ($char = "\t") do={:set char " "}
        :set cmd "$cmd$char"
    }
:return $cmd
}

:if ([:len $1]=0) do={:return "Error IP SSH-Server"}
do {:set $1 [:toip $1]} on-error={:return "Error syntax address IP SSH-Server"}
:if (!any $2) do={:return "Error username"}
:if ([:len $3]=0) do={:return "Error Script-sourse name not set"}
:if ([:len $4]=0) do={:return "Error Script SSH name not set"}
:if ([:len [/system script find name=$3]]=0) do={:return "Error Script $3 no find in repository"}
  :global F
    :do {
    :set F [/system script get $3 source]
:log warning $F 
       } on-error={:return "Error reading a script into a variable script larger than 4096 bytes"}
:set F [$ReplaceChars $F]
 :log error $F
 :if ([:len $5]=0) do={
     :do {
     [/system ssh-exec address=$1 user=$2 command="/system script remove \"$4\""]
     :delay 2s;
     [/system ssh-exec address=$1 user=$2 command="/system script add name=\"$4\" source=\"$F\""]
       } on-error={:return "Error transmitting or creating a script on the SSH server"}
      :log warning "Script $3 was transferred to the SSH server $1"; :return "Done"
    }
 :if (([:len $5]!=0) && ($5="run")) do={
     :do {
     [/system ssh-exec address=$1 user=$2 command="/system script remove \"$4\""]
     :delay 2s;
     [/system ssh-exec address=$1 user=$2 command="/system script add name=\"$4\" source=\"$F\"; /system script run \"$4\"; /system script remove \"$4\""]
       } on-error={:return "Error transmitting or creating a script on the SSH server"}
   :log warning "Script $3 was transferred to the SSH server $1 and executed"; :return "Done"
  } else={:return "Error parametr $5"}
}


:local sshAns [$SSHscript 192.168.1.2 admin ip2bin ScriptSSH]
:if ($sshAns!="Done") do={:log error $sshAns}

It is not clear how to pass the string so that it can be inserted as a script into the SSH server router repository …
Maybe it’s not possible?

I’m not going to waste my time with people who foolishly keep contradicting me.
It’s not that I’m never wrong, but when something is certain I don’t waste time explaining it over and over again.
“as-value” does not exist for either v6 or v7.7

Spending all the time refuting what I write is not a collaborative attitude,
go to hell.

In vain you are. I didn’t mean to offend you in any way and I don’t understand why you offend me. I do not contradict you, but the design works.

:global i ([/system ssh-exec address=192.168.1.2 user=admin command=":put [\$ip2bin 127.0.0.1]" as-value ] -> "output")
:log warning $i

01111111000000000000000000000001

You are probably a talented person, but this does not give you the right to send others somewhere … (for example, to hell).

Better help me with the function. I really need your help.
If for some reason it is not convenient for you to correspond here, we can go to the section of private messages that no one will see

Once again I emphasize that I am writing something not to convict you of a mistake, but because it is important for me to understand and understand. I don’t have the knowledge you have, but that doesn’t mean I’m stupid. The doctor also has to be patient, explaining many medical issues to his patients, but I don’t call them fools and don’t send them to hell

He has already made it clear to you, please do not waste more time or make others lose it.

I don’t understand why I offended him.
Moreover, it is written in the official wiki: https://wiki.mikrotik.com/wiki/Manual:System/SSH_client
There’s something else… Oh well. Let him decide what to do.