Community discussions

MikroTik App
 
Cablenut9
Long time Member
Long time Member
Topic Author
Posts: 542
Joined: Fri Jan 08, 2021 5:30 am

Send SMS from different router

Mon Apr 26, 2021 10:31 pm

I have a setup where I have two redundant routers with VRRP, and one has a fiber internet connection and the other has LTE. Is there a way to make a script send an SMS from the LTE device, but have that message be initiated by the other router? I'd like to be able to get text messages when some event happens on the main router but it's not the one that can actually send messages.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Send SMS from different router

Tue Apr 27, 2021 3:11 pm

I guess sending e-mails or telegram messages would be a lot easier... And it does not rely on the other device being available.
That said... You could use ssh for you solution with sms.
 
Cablenut9
Long time Member
Long time Member
Topic Author
Posts: 542
Joined: Fri Jan 08, 2021 5:30 am

Re: Send SMS from different router

Thu Apr 29, 2021 4:25 am

That said... You could use ssh for you solution with sms.
How could this be done in ROS?
 
maaaruuu
just joined
Posts: 4
Joined: Mon Oct 25, 2021 4:59 pm

Re: Send SMS from different router

Sat Nov 12, 2022 12:30 am

This is old question, but just wandering if you ever found out a solution?

I was thinking about exactly the same scenario. In my case I have one LTE MT in my network along with multiple other ones. I would like to take advantage of LTE and be able to send speciffic SMS notifications from any other MT and use this one as sort of a gateway.
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Posts: 702
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Re: Send SMS from different router

Mon Nov 14, 2022 4:15 pm

I haven't played with the json web api yet, But presumably that can somehow trigger a script to run on the lte device, fetch from the fiber device can easily post a json string to the lte device to push a status, presuming there's a useful endpoint for launching scripts available...
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Wed Jan 04, 2023 9:10 pm

Hello, I am in the same situation.

Image

I want to take advantage of the LTE device to be able to send SMS from other computers within my LAN.

In this case I want to send sms from the RB4011 using the "usb1" serial port that is on the AP.

Has anyone managed to do it?
How would it be?
By SSH or other means?

BR & tnx.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Wed Jan 04, 2023 11:13 pm

Well finally, I have done some research and the solution is offered by SSH.

We can execute commands manually by Terminal and with scripts from one MT to another MT by SSH, but to automate the authentication and not have to write it by hand, we do it with RSA type keys.

MT Client (In the diagram it is the RB4011)

We generate the public and private RSA keys which are stored in /files.
/ip ssh export-host-key export-host-key key-file-prefix=admin.
"admin" is the name we give for the "full" user in this example.

We can see that two files have been generated: admin_rsa and admin_rsa.pub.
The "admin_rsa.pub" file must be imported into MT Server (where we want to connect to).
The "admin_rsa" file must be imported in MT Client (System > Users > SSH Private Keys > Import SSH Private Key). Important to select the user who has administrator rights:
/user ssh-keys private import user=admin private-key-file=admin_rsa.

MT Server (hAP ac2 with USB modem)

The file that we have generated in MT Client "admin_rsa.pub" must be imported in MT Server.
First we copy it to /files and then (System > Users > SSH Keys > Import SSH Key) it is important to select the user with administrator rights.


Testing

Once the public key is installed and trusted in MT Server, a PKI SSH session can be created from MT Client:
/system ssh 192.168.88.2
In this case the IP is from MT Server, and we see that it connects correctly without asking us for username and password.
/system ssh 192.168.88.2 "/ip address print"
We get the Server side addressing.
/system ssh 192.168.88.2 "/tool sms send usb1 +34XXXXXXXXXXXXX message="test message""
successfully sends us an SMS

Concluding, we can have the USB modem in a computer and the rest of the devices of the Network can make use of it using connections via SSH authenticated with RSA.

I hope that if someone is in the same situation I can help you.

BR.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sun Jan 08, 2023 7:16 pm

Will ssh-exec work?

For example:

The first is to take in the script, for example, the model of the remote device and assign this value to the variable:
:global i ([/system ssh-exec address=192.168.100.10 user=podarok66 command=":put [/system routerboard get model]" as-value ] -> "output")
The second is to create a variable on the remote device, assigning it a value taken from a local variable on your device:
:local y "123"; /system ssh-exec address=192.168.100.10 user=podarok66 command=":global cif $y"
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Mon Jan 09, 2023 12:17 am

Will ssh-exec work?
Yes, both examples work correctly.

BR.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 12:56 pm

I tried everything. I even imported both keys to each of the routers.
Anyway, when trying to connect, it asks you to enter a password from the keyboard

when trying to connect to the terminal, it asks for a password:
/system ssh 192.168.1.2
password:

when trying to take data from the server to a variable, it writes an authorization error:
/:global i ([/system ssh-exec address=192.168.1.2 user=admin command=":put [/system routerboard get model]" as-value ] -> "output")
failure: authentication failure

ROS version 6.46.3 on both routers. What is the problem ?
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Mon Jan 09, 2023 1:03 pm

If it asks you for a password, the RSA keys are not configured correctly.

Follow my previous post where I explain how to do it. See if this drawing helps you (follow the order of entering commands):

Note: Both devices have version 7.6

Image

Sorry if I have not translated well into English

MT Servidor -> MT Server
MT Cliente -> MT Client

BR.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 1:38 pm

Thank you very much, I kind of did everything like that. I'll try again.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 2:00 pm

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.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Mon Jan 09, 2023 2:09 pm

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.
OK, great.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Mon Jan 09, 2023 2:27 pm

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=yes

/ip/ssh> print
           forwarding-enabled: no
  always-allow-password-login: yes
                strong-crypto: no
            allow-none-crypto: no
                host-key-size: 2048
BR.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 2:44 pm

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


I thought about it and tried it both on the client side and on the server side - the error persists.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 2:50 pm

Along the way, I found an error in RouterOS: the system does not correctly transmit lines containing spaces.
For example:

:local y "mather&father";

[/system ssh-exec address=192.168.1.2 user=admin command=":global x $y"]

Ok, good.

:local y "mather father";

[/system ssh-exec address=192.168.1.2 user=admin command=":global x $y"]

Bug, the variable is not created. Perhaps this can be circumvented by using :parse
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 2:52 pm

You need some escaped quoting here...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 2:59 pm

Bug, the variable is not created.

Bug??? Or syntax error....

/system ssh-exec address=192.168.1.2 user=admin command=":global x \"$y\""]
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:05 pm

You need some escaped quoting here...
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:

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

the command is not working. It turned out because the model of my board contains a space.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:08 pm

Bug??? Or syntax error....

/system ssh-exec address=192.168.1.2 user=admin command=":global x \"$y\""]


For the first time I see that additional quotation marks and their escaping are needed here ... Where is it written in the description of the Ros syntax ?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:10 pm

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

Re: Send SMS from different router

Mon Jan 09, 2023 3:11 pm

Yes, since REX wrote it, it works that way.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:13 pm

yurself wite this:

:local y "mather father";

why you use quotation marks?
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:15 pm

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

To be honest, I didn't really understand. I'm not making a string out of individual variables. In my example, the string is single and it is up to the parser to pass it correctly.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:17 pm

The other device do not have on memory any variable, the command is just a simple string passed to the other device.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:20 pm

: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]\""]


Rex, I understand what you want to say, but to enclose the construction in additional quotes here ..., it seems to me that the Ros parser could be smarter...
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:26 pm

The other device do not have on memory any variable, the command is just a simple string passed to the other device.

I thought that the string is passed exactly as a command. In the sense that after transmission, it is analyzed by the language of the server device. Therefore, I would like to write:

[/system ssh-exec address=192.168.1.2 user=admin command=":global x [/system routerboard get model]"]

But since quotes and escaping are needed, so be it, the main thing is that it works.
Last edited by Sertik on Mon Jan 09, 2023 3:27 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:27 pm

You're right, it should give error immediately when typing
:local strvar thisisonestring
without the proper quotes...
instead of accepting it anyway just because there is no space (or other reserver characters) in between.

Instead in the case of "command" it does exactly what has been ordered,
it is not creating executable code, but only a line of text, it rightly ignore what is written inside.
If he put in quotes without being asked, you know what a pain in the ass if you have to do other things...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:31 pm

This:
[/system ssh-exec address=192.168.1.2 user=admin command=":global x [/system routerboard get model]"]
create inside remote device the variable x with inside it's own routerboard model



This:
[/system ssh-exec address=192.168.1.2 user=admin command=":global x \"$[/system routerboard get model]\""]
create inside remote device the variable x with inside the local/calling routerboard model
(notice the added $ inside, not only the quotes)
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:31 pm

"$" I accidentally missed
Last edited by Sertik on Mon Jan 09, 2023 3:34 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:33 pm

Yes, since REX wrote it, it works that way.
Please avoid this attitude...
I know the documentation is poor
and what I know comes from my own experience.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:35 pm

You're making fun of me
??? NO...

your example is too exaggerated.
I don't understand what you mean, honestly...
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:36 pm

Please avoid this attitude...
I know the documentation is poor
and what I know comes from my own experience.


Thank you very much! It's good that there are experienced friends.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:38 pm

You're making fun of me

??? NO...

your example is too exaggerated.

I don't understand what you mean, honestly...


Probably it's all from the problems of automatic translation
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Mon Jan 09, 2023 3:39 pm

Yes... I understand... ;)
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Mon Jan 09, 2023 3:40 pm

Thanks again for the "lesson with quotes"
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Mon Jan 09, 2023 5:03 pm

We all have learned... Rex is a great programmer.

By the way the command of the topic discussed today, can work like this too, easier, shorter.
[/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.
BR.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 8:48 am

My question then is it possible to transfer a ready - made function to another device and execute it there ?
Something like that:

:global myFunc do={:log warning test}
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc \"$myFunc\"; [$myFunc]"]

A global variable on the remote router is created and

/system script environment shows $myFunc
(code), but

:put [:typeof $myFunc]
str

The option with :parse does not help either:
:global myFunc [:parse "log warning test"]
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc \"$myFunc\"; [$myFunc]"]

Is it possible to work around this problem and pass the contents of the function not as a string, but as code?
Or did the developers of Microtik specifically block this possibility ?
Last edited by Sertik on Tue Jan 10, 2023 8:58 am, edited 1 time in total.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Tue Jan 10, 2023 8:55 am

I don't know, you'll have to try it out.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 9:08 am

You need to try something like:
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc \"[:parse \":log warning test\"]\"] 

Where is our REX ? He knows everything. Let's wait for his opinion.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 9:18 am

This is how the code is correctly transmitted, and is recorded in the myFunc router server as code, but it is not yet launched from the client router. On the router server, the created myFunc function is visible in the environment, contains the correct code and works if it is called there.

[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc [:parse \":log warning test\"]; [$myFunc]"] 
 
jameskane1
just joined
Posts: 1
Joined: Mon Oct 17, 2022 2:05 pm
Location: Australia
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 9:21 am

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

Re: Send SMS from different router

Tue Jan 10, 2023 10:15 am

Where is our REX ? He knows everything. Let's wait for his opinion.
Thank you for your trust, but don't overdo it...

My "strong" is not knowing the language, but using what we already have, exploiting it to achieve the final goal, when possible.

A moment while I elaborate the answer.

I'll write the answer in a few minutes.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 11:28 am

Something REX thought for a long time. Maybe for security reasons Microtik ?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 11:59 am

no, no, just because I'm at work and I'm looking for a free moment
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 12:09 pm

Of course, good.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 12:27 pm

For the example I want consider a function with complex syntax, like:
: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
}
How to pass by "ssh-exec commad" to another device?
All the lines must be manually escaped for make all one valid string...
But why do not use the /system script export function for escape all correctly (just minor fix)?

Create a new script, with inside the function and export that:

export code

/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]"
Last edited by rextended on Tue Jan 10, 2023 12:38 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 12:37 pm

For transfer
:global myFunc do={:log warning test}

must be like ($ escaped)
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc do={:log warning test}; [\$myFunc]"]
You can not pass the "compiled" script from router A to router B...
As already wroted, you can pass just one command line like what you can do with the keyboard on ssh terminal.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 12:41 pm

Yes, if that works, then great! But if you write, then checked. I can't check right now, I'll write back in a while.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 12:42 pm

[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc do={:log warning test}; [\$myFunc]"]

Now I see, I forgot to escape the \$
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 1:36 pm

[…] why do not use the /system script export function for escape all correctly […]?
;)
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 4:59 pm

[/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]"]
The syntax is correct, but the function is not created on the remote machine.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 5:33 pm

OH... you read «(just minor fix)»?

I forgot to replace \r with ; and \n with nothing.

revised code

[/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]"]
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 5:43 pm

Wow! It works ! I didn't think of that. Thank you very much!
I didn't understand what was needed to apply "(just a minor fix)" ...
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 5:51 pm

explain, Rex, why it works
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc [:parse \":log error test\"]; \$myFunc"]  
and does not work
[/system ssh-exec address=192.168.1.2 user=admin command=":global myFunc [:parse \":log error test\"]; [\$myFunc]"]  
I do not understand.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 6:04 pm

For me work on both cases, correct syntax on 6.48.6
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Tue Jan 10, 2023 6:05 pm

I didn't understand what was needed to apply "(just a minor fix)" ...
It's not your fault, I just forgot to write that...
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Tue Jan 10, 2023 8:36 pm

Yes, now it works for me too. I'm sorry, maybe there was a mistake somewhere.

Thank you for your time and help.

Now we have found out that it is possible not only to run the finished script on a remote router, but also to transfer the code there and execute it. Some practical things can be built on this, for example, copyright protection.

It is a pity that it is impossible to save the code in /ip firewall layer7, for example, and then restore it from there as code. :)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Wed Jan 11, 2023 1:42 am

 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Wed Jan 11, 2023 8:03 am

Yes, I read and even wrote some messages there. There we came to the conclusion that it is possible to save the function code in Layer7, but it is not possible to restore it from there as a working code, it is restored as a string type, and so far this problem has not been solved.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Wed Jan 11, 2023 1:44 pm

A small function for sending an SMS message from the router-SSH-client through the modem of the router-SSH-server:
# $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}
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Wed Jan 11, 2023 8:36 pm

with me having to send a "CONTINUE" message every 5 gigabytes, every day (an unlimited amount of times) to get another 5GB of data.

If you need to send messages through a remote router, you can use the SSHsend function:

:global SSHsend
:local address XXX.XXX.XXX.XXX
:local user admin
:local port usb2
:local phoneNumber +XYYYYYYYYYY

[$SSHsend $address $user $port $phoneNumber "CONTINUE"]
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Wed Jan 11, 2023 9:15 pm

Sertik do not lost time, is the usual add-spam-later NameSurname
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Thu Jan 12, 2023 8:10 am

Sertik do not lost time, is the usual add spam later NameSurname

I don't understand you, REX, what do you mean ?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Thu Jan 12, 2023 10:51 am

You reply to one post user, with the username NameSurname, with nonsense post...
are "bot" for spam.

Add later after some weeks on the signature spam links.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Thu Jan 12, 2023 3:08 pm

Ok, understand.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sat Jan 14, 2023 2:16 pm

Hello, Rex !
I ask for your help.

I'm trying to write a function that would create a function from the text of an SSH client script on an SSH server. Everything seems to be correct and a string is created that is identical to your escaped version of the ip2bin function, but the global variable with the code is not created on the remote SSH server. What's my mistake? I can't find it at all...
: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
}
this code is in the ip2bin script of the same name.

And this is my function:
: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}

I understand what can be done differently - transfer the source script to the SSH server and execute it - the same function definition will occur. But we are not looking for easy ways - we want to do exactly the first option. Although in the second option, you will also have to change the same characters in the transmitted string.
Last edited by Sertik on Sat Jan 14, 2023 5:33 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sat Jan 14, 2023 5:16 pm

Too much escape will kill you...

/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\""]
}

example code

:put [$scr2sshcmd 192.168.1.2 admin ip2bin (":global test [\$ip2bin 127.0.0.1]")]
I do not implement ssh-exec error management.
Last edited by rextended on Sat Jan 14, 2023 7:02 pm, edited 1 time in total.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sat Jan 14, 2023 6:10 pm

Rex, thank you so much! I inserted your symbol replacement function into mine, everything seems to be the same as yours, I can’t understand why it doesn’t work. Sorry for taking up your time.

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

Re: Send SMS from different router

Sat Jan 14, 2023 6:26 pm

: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]")]

Didn't get the idea of ​​$4. Where is he ? Do you mean that you can pass function parameters in $4?
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sat Jan 14, 2023 6:32 pm

I am writing such a library and in logic it is almost ready. What do you think, maybe you already have such developments and I am reinventing the wheel? If you think that the idea is not bad, maybe you will agree to look at my code? For a programmer like you, this is probably an easy task.

Creating the SSH-exec labruary (list of planned features)

1. Receiving and transmitting data by OID
2. READ THE LIST OF REMOTE SCRIPTS, FUNCTIONS, SCHEDULER TASKS
3. Execute remote MT script
4. Perform remote MT function
5. Send command to remote MT
6. Read the value of the variable on the remote MT
7. Create a variable of the original type on the remote MT and pass a value to it
8. Pass the function and execute (if necessary) on the remote MT
9. Transfer the script and execute (if necessary) on the remote MT
10. Create a Scheduler task on the remote MT (with the launch of the script of the remote MT, from the text of the script with the transferred name of the MT client)

When creating scripts and tasks of the Scheduler on a remote MT, create comments to them (if necessary)


+ passing a function to execution with parameters
+ passing the script for execution with parameters
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sat Jan 14, 2023 6:45 pm

Is better you start with something that work, instead to try to fix your code...
Is why I rewrite from 0 the function instead to try to fix your.
Last edited by rextended on Sat Jan 14, 2023 7:08 pm, edited 2 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sat Jan 14, 2023 6:56 pm

You can use all the Snippets on my signature links and all the other script on forum,
also this topic is full of usable code.

I promise you nothing, it can't be like a job.
If you write something it is not sure that I see it and that I correct it,
especially if it's written hard to read, but you never know.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sat Jan 14, 2023 6:58 pm

if you had copied correctly you would not have written on ReplaceChars
        :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 ";"}
but only
 
        :if ($char~"(\a|\b|\f|\n|\r|\v)") do={:set char ";"}
        :if ($char = "\t") do={:set char " "}

Didn't get the idea of ​​$4. Where is he ? Do you mean that you can pass function parameters in $4?
after install the function, the 4th parameter add some command(s), like on the example (escaped)
:global test [$ip2bin 127.0.0.1]
it use instantly the transferred (from source) ip2bin function to add one global variable (on remote machine), called test, with inside the binary value of 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)
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sat Jan 14, 2023 8:46 pm

Thanks Rex. For me, scripting is not a job, but a hobby. I'm a doctor.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sat Jan 14, 2023 8:52 pm

: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]")]

I'm sorry, again I don't see $4 in the function call, three parameters are used: address, user and command:

address 192.168.1.2
user admin
command (":global test [\$ip2bin 127.0.0.1]")

So ? Where is $4?

Should be:

:put [$scr2sshcmd 192.168.1.2 admin Ourscript (":global test [\$ip2bin 127.0.0.1]")]
Last edited by Sertik on Sat Jan 14, 2023 8:57 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sat Jan 14, 2023 8:54 pm

Is better you read all before posting something...

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

Re: Send SMS from different router

Sat Jan 14, 2023 8:59 pm

So I read it, but apparently I didn’t understand because of my bad English
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sat Jan 14, 2023 9:07 pm

:put [$scr2sshcmd 192.168.1.2 admin ip2bin ("<escaped instructions>")]

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

Re: Send SMS from different router

Sat Jan 14, 2023 9:27 pm

Well yes, that's right.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sat Jan 14, 2023 9:44 pm

:put [$scr2sshcmd 192.168.1.2 admin ip2bin ("<escaped instructions>")]
: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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sat Jan 14, 2023 10:55 pm

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"
}
Last edited by rextended on Sun Jan 15, 2023 8:48 pm, edited 3 times in total.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sun Jan 15, 2023 12:45 pm

: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
Last edited by Sertik on Sun Jan 15, 2023 1:17 pm, edited 3 times in total.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sun Jan 15, 2023 1:04 pm

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}
Last edited by Sertik on Sun Jan 15, 2023 8:38 pm, edited 1 time in total.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sun Jan 15, 2023 1:47 pm

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?
Last edited by Sertik on Sun Jan 15, 2023 8:57 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Send SMS from different router

Sun Jan 15, 2023 8:45 pm

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

Re: Send SMS from different router

Sun Jan 15, 2023 9:02 pm

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

Re: Send SMS from different router

Sun Jan 15, 2023 9:11 pm

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
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Send SMS from different router

Sun Jan 15, 2023 9:16 pm

He has already made it clear to you, please do not waste more time or make others lose it.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Send SMS from different router

Sun Jan 15, 2023 9:21 pm

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.

Who is online

Users browsing this forum: No registered users and 26 guests