Community discussions

MikroTik App
 
dennis46
just joined
Topic Author
Posts: 2
Joined: Thu May 31, 2018 3:10 pm

Script input from console ... works!

Thu May 31, 2018 3:18 pm

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 ...
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Script input from console ... works!

Mon Jun 04, 2018 12:37 pm

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.
 
dennis46
just joined
Topic Author
Posts: 2
Joined: Thu May 31, 2018 3:10 pm

Re: Script input from console ... works!

Wed Jun 06, 2018 3:04 pm

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! :-)
 
dke
newbie
Posts: 47
Joined: Tue Dec 10, 2019 11:30 pm
Location: Austria

Re: Script input from console ... works!

Thu Jun 04, 2020 2:41 pm

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.
 
msatter
Forum Guru
Forum Guru
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Script input from console ... works!

Thu Jun 04, 2020 5:11 pm

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
 
tabraham
newbie
Posts: 27
Joined: Wed Feb 08, 2017 10:18 pm
Location: Germany
Contact:

Re: Script input from console ... works!

Thu Aug 24, 2023 9:46 pm

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
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script input from console ... works!

Thu Aug 24, 2023 9:55 pm

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
}
 
msatter
Forum Guru
Forum Guru
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Script input from console ... works!

Thu Aug 24, 2023 11:15 pm

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.
 
tabraham
newbie
Posts: 27
Joined: Wed Feb 08, 2017 10:18 pm
Location: Germany
Contact:

Re: Script input from console ... works!

Fri Aug 25, 2023 11:08 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script input from console ... works!

Sat Aug 26, 2023 9:30 am

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

Who is online

Users browsing this forum: No registered users and 3 guests