Community discussions

MikroTik App
 
phdoerfler
just joined
Topic Author
Posts: 18
Joined: Sat Sep 04, 2021 3:40 pm

How to use the output of print in scripts?

Fri Oct 01, 2021 10:57 pm

I'm making my routers send me an e-mail and I'd like to include things like the output of
/system routerboard print
or
/ip address print
:
/ip address print
Flags: X - disabled, I - invalid, D - dynamic
 #   ADDRESS            NETWORK         INTERFACE
 0 D 192.168.1.72/24    192.168.1.0     bridgeLocal
 
However, trying to concatenate that to a string always results in an empty string. I suppose
print
only outputs to the terminal or a file. I've also tried saving the
print
output to a file but don't know how to read it again. With
fetch
? Surely there gotta be a better way, but how?

Consulting https://wiki.mikrotik.com/wiki/Manual:S ... and_Tricks wasn't fruitful either.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to use the output of print in scripts?

Sat Oct 02, 2021 1:24 am

The "print" should only be used in exceptional cases.
Mainly it is to print the text on the terminal, instead of using it on the script.
:put "blah blah blah $[:tostr [/system routerboard print as-value]] blah blah blah"

:put "blah blah blah $[:tostr [/ip address print as-value]] blah blah blah"


# better get exactly what you want

:put "blah blah blah $[/system routerboard get current-firmware] blah blah blah"

:put "blah blah blah $[/ip address get [find where interface="ether1"] address] blah blah blah"


About 2nd question:
[/file get namefile.ext contents]
or simply add the files as attachments to e-mail...
Last edited by rextended on Sat Oct 02, 2021 11:07 am, edited 1 time in total.
 
phdoerfler
just joined
Topic Author
Posts: 18
Joined: Sat Sep 04, 2021 3:40 pm

Re: How to use the output of print in scripts?

Sat Oct 02, 2021 10:56 am

Thanks for your input!

I actually really like the output of print. It makes nice tables, properly aligns things, it's great. What's so bad about using it? In my case I am actually interested in all or most of the data shown in print's output.

Also, I don't get your code examples. They don't seem to work, just like my experiments. I always get an empty string:
> :put "blah blah blah $[/system routerboard print as-value] blah blah blah"
 blah blah blah
 
Or:
[admin@routername] /interface wireless> :global meep ("ho $[/ip address print]" . "hey")
Flags: X - disabled, I - invalid, D - dynamic 
 #   ADDRESS            NETWORK         INTERFACE                                                                                             
 0 D 192.168.1.72/24    192.168.1.0     bridgeLocal                                                                                           
[admin@routername] /interface wireless> :put $meep
ho hey
About the file method, that seems to work, nice! However, when used in the terminal, it seems to mess up the line endings:
[admin@routername] /interface wireless> /ip address print file=tmp   
[admin@routername] /interface wireless> :put [/file get tmp contents]
# oct/ 2/2021  8:45:20 by RouterOS 6.48.4
                                         # software id = DVR5-JWC2
                                                                  #
                                                                   Flags: X - disabled, I - invalid, D - dynamic 
                                                                                                                  #   ADDRESS            NETWORK
         INTERFACE                              
                                                 0 D 192.168.1.72/24    192.168.1.0     bridgeLocal                            
About the attachment approach: Good idea! That didn't even occur to me, haha. Unfortunately I can't get that to work. Sending e-mails without an attachment works fine, but as soon as I specify file=something I don't receive the e-mail. The last status reads a promising
/tool e-mail print
       address: mail.smtp2go.com
          port: 2525
     start-tls: yes
          from: routername <a.b@example.com>
          user: someuser
      password: nothingtoseehere
   last-status: succeeded
  last-address: 123.123.123.123
  
Just like it does when I successfully send e-mails with no attachments. Perhaps my mail server drops the mail because of the attachment and an overly enthusiastic rspamd…

Edit: Seems there was/is some greylisting involved. The first e-mails with attachments are arriving. I don't like this added level of insecurity and also the attached file doesn't get shown in-line.
Also it has this annoying header with the comments. That's also an issue when I use the file content in the body. Plus, this makes the whole neat aligned output useless because of the non fixed space font in my mail client. So, on to sending html e-mails then, I guess.

Is there really no good way of using the output of print in a script?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to use the output of print in scripts?

Sat Oct 02, 2021 11:10 am

Let me explain better, for my point of view: I hate print in a script.
MikroTik send e-mail on text format, and for do nice table, is unadequate.


On the example is missing to-string, because the as-value return an array, I have fixed the previious post with this:
:put "blah blah blah $[:tostr [/system routerboard print as-value]] blah blah blah"

:put "blah blah blah $[:tostr [/ip address print as-value]] blah blah blah"
 
phdoerfler
just joined
Topic Author
Posts: 18
Joined: Sat Sep 04, 2021 3:40 pm

Re: How to use the output of print in scripts?

Sat Oct 02, 2021 12:58 pm

The examples are working now. However, the output is nowhere near as nice as what the regular print does. Unfortunately it seems that "as-value" is mandatory in order to get a string. Without it it just returns the empty string and prints to the terminal (or a file).

The file approach would be ok if there was some way to remove the first three lines:
# oct/ 2/2021  9: 5: 2 by RouterOS 6.48.4
# software id = DVR5-JWC2
#
I found no way of doing so. There doesn't seem to be a way to split a string into multiple lines. In general there seems to be barely any string handling functionality in routeros script. That's unfortunate. Seems like I have to write a ton of boiler plate code just to mimic print's behaviour.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to use the output of print in scripts?

Sat Oct 02, 2021 1:17 pm

Ok.... if you insist too much, I'm forced to give you the soluction,
but I repeat: I hate the use of print commands on script...

Do not remove the delays.
:execute file=sysroupri.txt script="/system routerboard print"
:delay 1s
:put [/file get sysroupri.txt contents]

:execute file=ipaddrpri.txt script="/ip address print"
:delay 1s
:put [/file get ipaddrpri.txt contents]
 
User avatar
Larsa
Forum Guru
Forum Guru
Posts: 1041
Joined: Sat Aug 29, 2015 7:40 pm
Location: The North Pole, Santa's Workshop

Re: How to use the output of print in scripts?

Sun Oct 03, 2021 11:47 am

The "print" should only be used in exceptional cases.
Imho there is a general design flaw in the ros script engine implementation that won't let you catch stdout and stderr from whatever command you want. It's a major drawback when it comes to managing script errors or as in this case catching output from the print command. But don't get depressed, we'll just have to wait for v7.9 so no problem ;)

Who is online

Users browsing this forum: cosinguyen93 and 20 guests