Page 1 of 1

Script input from console ... works!

Posted: Thu May 31, 2018 3:18 pm
by dennis46
I found an instresting undocumented (?) solution to input values from terminal to script.

It looks like this.

=== sample script ===
:local input do={:put $1;:return}
:local login [$input "Enter login"]
:local password [$input "Enter password"]
:put "Login is [$login] and password is [$password]"
=================

=== terminal outeput ===
[admin@MikroTik AP] /ppp secret> /system script run 1
Enter login
value: mike
Enter password
value: jdajshdjadh
Login is [mike] and password is [jdajshdjadh]
[admin@MikroTik AP] /ppp secret>
====================

So if you use :return without any params you get input from terminal.

No mention of this behaviour found at https://wiki.mikrotik.com/wiki/Manual:Scripting and https://wiki.mikrotik.com/wiki/Manual:S ... g-examples ...

Re: Script input from console ... works!

Posted: Mon Jun 04, 2018 12:37 pm
by dasiu
dennis46 - it's a nice lifehack!! Thanks! :)

It's not "Undocumented" and it's probably not a "feature" :). MikroTik does that if you don't fill a required value. Try: /ping - it will ask you for address (as it's a required argument). The same when you want to remove something (/ip address remove), not specifying what - it'll ask you for clarification. And does the same here. Probably noone thought that this "asking for clarification" can be used here that way :D.

Re: Script input from console ... works!

Posted: Wed Jun 06, 2018 3:04 pm
by dennis46
It's not "Undocumented" and it's probably not a "feature" . MikroTik does that if you don't fill a required value.
Ok! Thanks for explanation! :-)

Re: Script input from console ... works!

Posted: Thu Jun 04, 2020 2:41 pm
by dke
This does not work for me in v6.47. I'm not getting prompted for input and this is printed then:
Login is [] and password is []
Is there a working user input in v6.47? I also did not succeed with viewtopic.php?t=87744.

Re: Script input from console ... works!

Posted: Thu Jun 04, 2020 5:11 pm
by msatter
Thanks you so much and I was looking for this script. Put it like this with a starting "{" and a ending "}" in terminal. When using in script and schedule as stated earlier. I have put :terminal style escaped; in to have the text be displayed in a different color.

I played with cuu and el to get the field to enter behind the description but did not manage that.
:terminal
cuu -- move cursor up
el -- erase line
inkey -- read key
style -- set output text style

:local input do={:terminal style escaped; :put $1; :return;}
:local login [$input "Enter login:"]
:local password [$input "Enter password:"]
:put "Login is [$login] and password is [$password]"
Enter login
value: test
Enter password
value: test1
Login is [test] and password is [test1]
Darn even better than expected! :shock:
\... :put [:typeof $login]; :put [:typeof $password];
Enter login
value: 1234
Enter password
value: false
num
bool

Re: Script input from console ... works!

Posted: Thu Aug 24, 2023 9:46 pm
by tabraham
Hi the whole thing works for be but I did not get the part where you said
Put it like this with a starting "{" and a ending "}" in terminal. When using in script and schedule as stated earlier.
.
The script where I use the input (if there is nothing set in the global environment) is also scheduled to run after interval... but it fails. It only runs perfectly if i run from the terminal... Can you please help me out?
Thanks you so much and I was looking for this script. Put it like this with a starting "{" and a ending "}" in terminal. When using in script and schedule as stated earlier. I have put :terminal style escaped; in to have the text be displayed in a different color.

I played with cuu and el to get the field to enter behind the description but did not manage that.
:terminal
cuu -- move cursor up
el -- erase line
inkey -- read key
style -- set output text style

:local input do={:terminal style escaped; :put $1; :return;}
:local login [$input "Enter login:"]
:local password [$input "Enter password:"]
:put "Login is [$login] and password is [$password]"
Enter login
value: test
Enter password
value: test1
Login is [test] and password is [test1]
Darn even better than expected! :shock:
\... :put [:typeof $login]; :put [:typeof $password];
Enter login
value: 1234
Enter password
value: false
num
bool

Re: Script input from console ... works!

Posted: Thu Aug 24, 2023 9:55 pm
by Amm0
There is actually now an /terminal/ask in v7.12beta1.
{
  :local myhelp "You can ask questions now!"
  :local myquestion "What do you want to ask?"
  :local myanswer [/terminal/ask preinput=$myhelp prompt=$myquestion]
  :put $myanswer
}

Re: Script input from console ... works!

Posted: Thu Aug 24, 2023 11:15 pm
by msatter
All variables are :local and as soon the script has ende those are destroyed. To make those :global replace :local with :global.

The {} allows in terminal to run it as one script. Otherwise every line intepreted on it's own and all variables are destroyed after pressing Enter.....unless you use :global.

Re: Script input from console ... works!

Posted: Fri Aug 25, 2023 11:08 pm
by tabraham
Thanks for the ideas but I think I failed to explain properly.

I have a script which takes the input and sets as a global variable before doing further things. The scripts works perfectly when I run it from the terminal. But I want the script to run using scheduler too. When the scheduler runs the script, it should not ask for the user input but rather take in the default value.

FYI: I do not wish for an upgrade to the beta version. My ROS is 7.11

Re: Script input from console ... works!

Posted: Sat Aug 26, 2023 9:30 am
by rextended
I want the script to run using scheduler too.
When the scheduler runs the script, it should not ask for the user input but rather take in the default value.
Create a separate non-interactive version...