Community discussions

 
marrold
Member
Member
Topic Author
Posts: 406
Joined: Wed Sep 04, 2013 10:45 am

Scripting - Asking user for input.

Mon Aug 04, 2014 6:01 pm

Hi all,

I'm making an 'install script' to install another script on routerboards. Currently it's setup as a function, and the user can pass arguments to the function.

Is there any way for a script to ask for user input via the console? E.G

$Please enter WAN interface-
$_
I'm a SIP / VoIP engineer. Feel free to ask questions...
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Scripting - Asking user for input.

Mon Aug 04, 2014 6:05 pm

There's the "/terminal inkey" command, which asks the user for a single key, and returns the key code of the pressed key, which you can then decode into an ASCII character or a keyboard key.

It's as cumbersome to work with as it sounds... You need to invoke it as many times as the number of characters you need, or more precisely, keep invoking it until a new line is entered (character code 13). Oh, and you better support backspace (character code 8 ) too. If you manage to elegantly abstract away such stuff, you may as well create a function for that... One which wouldn't need prompts upon its install ;) . But at that point, I think we can agree that's overkill.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
marrold
Member
Member
Topic Author
Posts: 406
Joined: Wed Sep 04, 2013 10:45 am

Re: Scripting - Asking user for input.

Mon Aug 04, 2014 6:19 pm

boen_robot, this is the second time you've assisted with my scripting this week.

Thanks, it's much appreciated.
I'm a SIP / VoIP engineer. Feel free to ask questions...
 
A9691
just joined
Posts: 13
Joined: Sat May 14, 2016 10:58 am

Re: Scripting - Asking user for input.

Thu Mar 01, 2018 10:19 pm

Try out the following:
# function to prompt user to enter a value
:global read do={:return}

# storing the value entered by the user
:local userinput [$read]
It is interesting, that if the user enters 192.168.10.0/24, [:typeof $userinput] will be ip-prefix, not str.
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1790
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: Scripting - Asking user for input.

Thu Mar 01, 2018 11:09 pm

You may want to have a look at this ones too:
viewtopic.php?t=41042
and
viewtopic.php?t=38683#p284764
 
DiVersant
just joined
Posts: 1
Joined: Wed Mar 07, 2018 12:08 pm

Re: Scripting - Asking user for input.

Mon Jun 04, 2018 11:21 am

I'm trying to make a initial configuration script and need to parse user input to set router name, WAN ip and GW, local ip and GW and so on...
When I'm trying to use
# function to prompt user to enter a value
:global read do={:return}

# storing the value entered by the user
:local userinput [$read]
put $read
showing me ";(eval (eval /return))" as answer :(
What I'm doing wrong?
FW from 6.39 up to 6.42.3.
 
cantanko
newbie
Posts: 28
Joined: Mon Apr 05, 2010 12:53 am

Re: Scripting - Asking user for input.

Wed Jun 20, 2018 4:40 pm

You need to :put $userinput, not :put $read :)
 
User avatar
dasiu
Trainer
Trainer
Posts: 232
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Scripting - Asking user for input.

Wed Jun 20, 2018 5:39 pm

Or use:
:put [$read] - to run the script (as for user input) and immediately put it on the screen without "storing it" in a variable :).

Who is online

Users browsing this forum: No registered users and 10 guests