How could this be done in ROS?That said... You could use ssh for you solution with sms.
/ip ssh export-host-key export-host-key key-file-prefix=admin.
/user ssh-keys private import user=admin private-key-file=admin_rsa.
/system ssh 192.168.88.2
/system ssh 192.168.88.2 "/ip address print"
/system ssh 192.168.88.2 "/tool sms send usb1 +34XXXXXXXXXXXXX message="test message""
:global i ([/system ssh-exec address=192.168.100.10 user=podarok66 command=":put [/system routerboard get model]" as-value ] -> "output")
:local y "123"; /system ssh-exec address=192.168.100.10 user=podarok66 command=":global cif $y"
Yes, both examples work correctly.Will ssh-exec work?
/system ssh 192.168.1.2
/:global i ([/system ssh-exec address=192.168.1.2 user=admin command=":put [/system routerboard get model]" as-value ] -> "output")
OK, great.I did it again, it works. Moreover, I imported both keys to the client and to the server so that it was possible to enter from either side. It is not clear what the error was. But this time I created both certificates on a different router. It is not clear yet, maybe there is an error in ROS. I'll test on different routers, let's see what's the matter.
/ip ssh set always-allow-password-login=yes
/ip/ssh> print
forwarding-enabled: no
always-allow-password-login: yes
strong-crypto: no
allow-none-crypto: no
host-key-size: 2048
Now, on the server side, once you activate the SSH access through RSA Keys, if you want to access also with password manually you must activate the option: "always-allow-password-login: yes" otherwise you may get an error.
/ip ssh set always-allow-password-login=no
Bug, the variable is not created.
Why? The team itself must correctly work out the transmission of spaces. This glitch grew, not my mistake. I've been thinking for a long time why if I do:You need some escaped quoting here...
Bug??? Or syntax error....
/system ssh-exec address=192.168.1.2 user=admin command=":global x \"$y\""]
no matter the syntax, it's common sense for programming...
if "command" is a string, and is resolved by adding the value of "y", how does it become?
":global x mather father"
and obviously is one error, because you omit the " ".
:local y [/system identity get name]
[/system ssh-exec address=192.168.1.2 user=admin command=":global x $y"]
everything works correctly, and if I do:
:local y [/system routerboard get model]
[/system ssh-exec address=192.168.1.2 user=admin command=":global x $y"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global x \"[/system routerboard get model]\""]
The other device do not have on memory any variable, the command is just a simple string passed to the other device.
:local strvar thisisonestring
[/system ssh-exec address=192.168.1.2 user=admin command=":global x [/system routerboard get model]"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global x \"$[/system routerboard get model]\""]
Please avoid this attitude...Yes, since REX wrote it, it works that way.
??? NO...You're making fun of me
I don't understand what you mean, honestly...your example is too exaggerated.
Please avoid this attitude...
I know the documentation is poor
and what I know comes from my own experience.
You're making fun of me
??? NO...
your example is too exaggerated.
I don't understand what you mean, honestly...
[/system ssh-exec address=192.168.1.2 user=admin command=":global x [/system routerboard get model]"]
# for:
[/system ssh-exec 192.168.1.2 ":global x [/system routerboard get model]"]
# "user=admin" I understand that it can be omitted because the server and client router have the same user.
:global myFunc do={:log warning test}
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc \"$myFunc\"; [$myFunc]"]
:global myFunc [:parse "log warning test"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc \"$myFunc\"; [$myFunc]"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc \"[:parse \":log warning test\"]\"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc [:parse \":log warning test\"]; [$myFunc]"]
Thank you for your trust, but don't overdo it...Where is our REX ? He knows everything. Let's wait for his opinion.
:global ip2bin do={
:local number [:tonum [:toip $1]]
:local ret ""
:for i from=0 to=31 step=1 do={
:set ret "$(($number >> $i) & 1)$ret"
:if ([:tostr $i]~"^(7|15|23)\$") do={:set ret "$2$ret"}
}
:return $ret
}
/system script> export # RouterOS 6.48.6 # /system script add name=script1 source=":global ip2bin do={\r\ \n :local number [:tonum [:toip \$1]]\r\ \n :local ret \"\"\r\ \n :for i from=0 to=31 step=1 do={\r\ \n :set ret \"\$((\$number >> \$i) & 1)\$ret\"\r\ \n :if ([:tostr \$i]~\"^(7|15|23)\\\$\") do={:set ret \"\$2\$ret\"}\r\ \n }\r\ \n :return \$ret\r\ \n}"at this point the "command" is = to script "source", just add at the end the command for run the function:
[/system ssh-exec address=192.168.1.2 user=admin command=":global ip2bin do={\r\
\n :local number [:tonum [:toip \$1]]\r\
\n :local ret \"\"\r\
\n :for i from=0 to=31 step=1 do={\r\
\n :set ret \"\$((\$number >> \$i) & 1)\$ret\"\r\
\n :if ([:tostr \$i]~\"^(7|15|23)\\\$\") do={:set ret \"\$2\$ret\"}\r\
\n }\r\
\n :return \$ret\r\
\n}; :global result [\$ip2bin 127.0.0.1]"
:global myFunc do={:log warning test}
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc do={:log warning test}; [\$myFunc]"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc do={:log warning test}; [\$myFunc]"]
[…] why do not use the /system script export function for escape all correctly […]?
[/system ssh-exec address=192.168.1.2 user=admin command=":global ip2bin do={\r\
\n :local number [:tonum [:toip \$1]]\r\
\n :local ret \"\"\r\
\n :for i from=0 to=31 step=1 do={\r\
\n :set ret \"\$((\$number >> \$i) & 1)\$ret\"\r\
\n :if ([:tostr \$i]~\"^(7|15|23)\\\$\") do={:set ret \"\$2\$ret\"}\r\
\n }\r\
\n :return \$ret\r\
\n}; :global result [\$ip2bin 127.0.0.1]"]
[/system ssh-exec address=192.168.0.1 user=admin command=":global ip2bin do={;\ :local number [:tonum [:toip \$1]];\ :local ret \"\";\ :for i from=0 to=31 step=1 do={;\ :set ret \"\$((\$number >> \$i) & 1)\$ret\";\ :if ([:tostr \$i]~\"^(7|15|23)\\\$\") do={:set ret \"\$2\$ret\"};\ };\ :return \$ret;\ }; :global result [\$ip2bin 127.0.0.1]"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc [:parse \":log error test\"]; \$myFunc"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc [:parse \":log error test\"]; [\$myFunc]"]
It's not your fault, I just forgot to write that...I didn't understand what was needed to apply "(just a minor fix)" ...
# $1 - router IP
# $2 - username
# $3 - (usbX/lteY)
# $4 - phonenumber
# $5 - SMS-message
:global SSHsend do={
do {:set $1 [:toip $1]} on-error={:return "Error syntax address IP SSH-Server"}
:if (!any $2) do={:return "Error username"}
:if (!any $3) do={:return "Error SMS interface number or type"}
:if (!any $4) do={:global ADMINPHONE; :set $4 $ADMINPHONE}
:if (!any $5) do={:set $5 "test"}
do {
[/system ssh-exec address=$1 user=$2 command="/tool sms send port=\"$3\" phone-number=\"$4\" message=\"$5\""]
} on-error={:return "Error send message from SSH-server"}
:log warning "SMS $5 to a subscriber $4 via modem $3 router ssh-server IP $1 is sent"; :return "Done"
}
:local sshAns [$SSHsend 192.168.1.2 admin lte2 89107777777 "Hello from router!"]
:if ($sshAns!="Done") do={:log error $sshAns}
with me having to send a "CONTINUE" message every 5 gigabytes, every day (an unlimited amount of times) to get another 5GB of data.
:global SSHsend
:local address XXX.XXX.XXX.XXX
:local user admin
:local port usb2
:local phoneNumber +XYYYYYYYYYY
[$SSHsend $address $user $port $phoneNumber "CONTINUE"]
Sertik do not lost time, is the usual add spam later NameSurname
:global ip2bin do={
:local number [:tonum [:toip $1]]
:local ret ""
:for i from=0 to=31 step=1 do={
:set ret "$(($number >> $i) & 1)$ret"
:if ([:tostr $i]~"^(7|15|23)\$") do={:set ret "$2$ret"}
}
:return $ret
}
:global SSHfunc do={
# $1 - SSH-server address
# $2 - username
# $3 - script name with function text on SSH client
:local ReplaceChars do={:local ret ""
:local char ""
:for i from=0 to=([:len $1] - 1) do={
:set char [:pick $1 $i ($i + 1)]
:if ($char = "\r") do={:set $char "\3B\5C"}
# :if ($char = "\n") do={:set $char ""}
:if ($char = "\$") do={:set $char "\5C\24"}
:if ($char = "\"") do={:set $char "\5C\22"}
:if ($char = "}") do={:set $char " }"}
:set $ret "$ret$char"
}
:if ([:pick $ret ([:len $ret]-1) [:len $ret]]="\5C") do={:set $ret [:pick $ret 0 ([:len $ret]-1)]}
:return $ret
}
: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 [/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]
:put $F
} on-error={:return "Error reading a script into a variable script larger than 4096 bytes"}
:set F [$ReplaceChars $F]
:put $F
:do {
[/system ssh-exec address=$1 user=$2 command="$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 and set function"; :return "Done"
}
:local sshAns [$SSHfunc 192.168.1.2 admin ip2bin]
:if ($sshAns!="Done") do={:log error $sshAns}
/system script
add dont-require-permissions=no name=ip2bin owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
source=":global ip2bin do={\r\
\n :local number [:tonum [:toip \$1]]\r\
\n :local ret \"\"\r\
\n :for i from=0 to=31 step=1 do={\r\
\n :set ret \"\$((\$number >> \$i) & 1)\$ret\"\r\
\n :if ([:tostr \$i]~\"^(7|15|23)\\\$\") do={:set ret \"\$2\$ret\"}\r\
\n }\r\
\n :return \$ret\r\
\n}"
:global scr2sshcmd do={
:local remote [:toip $1] ; :local usr [:tostr $2]
:local scr [:tostr $3] ; :local ext [:tostr $4]
/system script
:if ([:typeof $remote] = "nil") do={:return "IP wrong or not defined"}
:if ([:len $usr] < 1) do={:return "user not defined"}
:if ([:len [find where name=$scr]] != 1) do={:return "script not found or not defined"}
:if ($ext~"(\a|\b|\f|\n|\r|\t|\v)") do={:return "invalid characters on command extension"}
:local source [get $scr source]
:local cmd ""
:for idx from=0 to=([:len $source] - 1) do={
:local char [:pick $source $idx]
:if ($char~"(\a|\b|\f|\n|\r|\v)") do={:set char ";"}
:if ($char = "\t") do={:set char " "}
:set cmd "$cmd$char"
}
:return [/system ssh-exec address=$remote user=$usr command="$cmd;$ext;:return \"DONE\""]
}
:put [$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]")]I do not implement ssh-exec error management.
: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") do={:set char ";"}
:if ($char = "\b") do={:set char ";"}
:if ($char = "\f") do={:set char ";"}
:if ($char = "\t") do={:set char " "}
:if ($char = "\v") do={:set char ";"}
:if ($char = "\n") do={:set char ";"}
:if ($char = "\r") 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
:set F [$ReplaceChars $F]
:log error $F
} on-error={:return "Error reading a script into a variable script larger than 4096 bytes"}
: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 ip2bin]
:if ($sshAns!="Done") do={:log error $sshAns}
:global scr2sshcmd do={
:local remote [:toip $1] ; :local usr [:tostr $2]
:local scr [:tostr $3] ; :local ext [:tostr $4]
/system script
:if ([:typeof $remote] = "nil") do={:return "IP wrong or not defined"}
:if ([:len $usr] < 1) do={:return "user not defined"}
:if ([:len [find where name=$scr]] != 1) do={:return "script not found or not defined"}
:if ($ext~"(\a|\b|\f|\n|\r|\t|\v)") do={:return "invalid characters on command extension"}
:local source [get $scr source]
:local cmd ""
:for idx from=0 to=([:len $source] - 1) do={
:local char [:pick $source $idx]
:if ($char~"(\a|\b|\f|\n|\r|\v)") do={:set char ";"}
:if ($char = "\t") do={:set char " "}
:set cmd "$cmd$char"
}
:return [/system ssh-exec address=$remote user=$usr command="$cmd;$ext;:return \"DONE\""]
}
:put [$scr2sshcmd 192.168.1.2 admin (":global test [\$ip2bin 127.0.0.1]")]
:if ($char = "\a") do={:set char ";"}
:if ($char = "\b") do={:set char ";"}
:if ($char = "\f") do={:set char ";"}
:if ($char = "\t") do={:set char " "}
:if ($char = "\v") do={:set char ";"}
:if ($char = "\n") do={:set char ";"}
:if ($char = "\r") do={:set char ";"}
:if ($char~"(\a|\b|\f|\n|\r|\v)") do={:set char ";"}
:if ($char = "\t") do={:set char " "}
after install the function, the 4th parameter add some command(s), like on the example (escaped)Didn't get the idea of $4. Where is he ? Do you mean that you can pass function parameters in $4?
:global scr2sshcmd do={
:local remote [:toip $1] ; :local usr [:tostr $2]
:local scr [:tostr $3] ; :local ext [:tostr $4]
/system script
:if ([:typeof $remote] = "nil") do={:return "IP wrong or not defined"}
:if ([:len $usr] < 1) do={:return "user not defined"}
:if ([:len [find where name=$scr]] != 1) do={:return "script not found or not defined"}
:if ($ext~"(\a|\b|\f|\n|\r|\t|\v)") do={:return "invalid characters on command extension"}
:local source [get $scr source]
:local cmd ""
:for idx from=0 to=([:len $source] - 1) do={
:local char [:pick $source $idx]
:if ($char~"(\a|\b|\f|\n|\r|\v)") do={:set char ";"}
:if ($char = "\t") do={:set char " "}
:set cmd "$cmd$char"
}
:return [/system ssh-exec address=$remote user=$usr command="$cmd;$ext;:return \"DONE\""]
}
:put [$scr2sshcmd 192.168.1.2 admin (":global test [\$ip2bin 127.0.0.1]")]
:put [$scr2sshcmd 192.168.1.2 admin Ourscript (":global test [\$ip2bin 127.0.0.1]")]
Check again the function and command line example I post on previous post,
You have read a preliminar version not finished than I post for not lost what I done till that moment.
Check also the "example code" of the command line, have the 4th parameter now.
(because in the preliminary version of the example the 3rd value was missing: the name of the script)
:put [$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]")]
:log warning ([$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]") as-value]->"output")
{
: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"
:global i ([/system ssh-exec address=192.168.1.2 user=admin command=":put [/system routerboard get model]" as-value ] -> "output")
:log info ([/system ssh-exec address=192.168.1.2 user=admin command=":put [/system routerboard get model]" as-value ] -> "output")
: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
so as-value]->"output") is supported /system ssh-execssh-exec do not support as-value)
# $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}
You can do like this:
01111111000000000000000000000001Code: Select all: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
so as-value]->"output") is supported /system ssh-execssh-exec do not support as-value)
: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