Community discussions

MikroTik App
 
samcarney
just joined
Topic Author
Posts: 17
Joined: Thu Sep 04, 2008 9:30 pm

how to handle y/n response in .rsc script

Mon Apr 19, 2010 7:54 pm

So i have a basic script in .rsc file that I drag and drop. Through terminal I type "import scriptx"
When it gets to end of script it ask " Are you sure you want to upgrade y/n"
How can I include "Yes" in the script so the script doesn't halt?

Help is much Appreciated!
Thanks
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: how to handle y/n response in .rsc script

Tue Apr 20, 2010 3:00 am

I'm glad you asked this question!

I just found this out a few weeks ago, and have been working on a simple setup script for a few projects so admins can run the script interactively.

Anyway, so here it is:
# Prompts for y/n

:local prompt "Are you sure? [Y/N]:"
:local options "y,n"

# Array of ascii values and ascii characters
# The ascii values directly correspond to a single ascii character
# asciicharlower and asciicharupper are used for character conversions
:local asciicharlower ("a,b,c,d,e,f,g,h,i,j,k,l," . \
                                  "m,n,o,p,q,r,s,t,u,v,w,x,y,z")
:local asciicharupper ("A,B,C,D,E,F,G,H,I,J,K,L," . \
                                   "M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")
:local asciichar ("0,1,2,3,4,5,6,7,8,9," . \
                         "a,b,c,d,e,f,g,h,i,j,k,l," . \
                         "m,n,o,p,q,r,s,t,u,v,w,x,y,z," . \
                         "A,B,C,D,E,F,G,H,I,J,K,L," . \
                         "M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z," . \
                         ".,/")
:local asciival {48;49;50;51;52;53;54;55;56;57; \
                       97;98;99;100;101;102;103;104;105;106;107;108; \
                       109;110;111;112;113;114;115;116;117;118;119;120;121;122; \
                       65;66;67;68;69;70;71;72;73;74;75;76; \
                       77;78;79;80;81;82;83;84;85;86;87;88;89;90; \
                       46;47}

:local key
:local findindex
:local validoption

:set key 0
:set validoption 0

# Prompt for input
:put ($prompt)

# Wait until valid option is entered
:while ($validoption = 0) do={

   :set key ([:terminal inkey])

# Convert numeric ascii value to ascii character
   :set findindex [:find [:toarray $asciival] $key]
   :if ([:len $findindex] > 0) do={
      :set key [:pick [:toarray $asciichar] $findindex]
   }


# If key is uppercase, convert it to lowercase
   :set findindex [:find [:toarray $asciicharupper] $key]
   :if ([:len $findindex] > 0) do={
      :set key [:pick [:toarray $asciicharlower] $findindex]
   }

# Check if key is a valid option
   :set findindex [:find [:toarray $options] $key]
   :if ([:len $findindex] > 0) do={
      :set validoption 1
   }

# end while validoption = 0
}


# Evaluate input
# At this point, key is converted to lower case
:if ($key = "y") do={
   :put ("You confirmed YES")
}
:if ($key = "n") do={
   :put ("You chose NO")
}
Basically, this prompts for a single character (y or n). If you type Y or N, it get's converted to lowercase. This way, during evaluation of the input, you only have to evaluate once (y or n).

Hope this helps. You could even set a global variable after input, and pass it to another script for execution.
 
samcarney
just joined
Topic Author
Posts: 17
Joined: Thu Sep 04, 2008 9:30 pm

Re: how to handle y/n response in .rsc script

Tue Apr 20, 2010 1:29 pm

wow thats a great script.

It looks as the script prompts user for yes/no.
Currently I just want to answer the yes/no that is presented.

Example .rsc file
> system routerboard upgrade
:delay 2
:put y 
Results
Opening script file test2.rsc

Script file loaded successfullyDo you really want to upgrade firmware? [y/n] 

The .rsc file has system routerboard upgrade command, and it halts me on the question. I want the script to answer yes. Can I use the :put command for the y?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: how to handle y/n response in .rsc script

Tue Apr 20, 2010 1:40 pm

RouterOS Script can't answer to prompt questions.
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: how to handle y/n response in .rsc script

Tue Apr 20, 2010 1:43 pm

You could probably use telnet/ssh connection with something that waits for the prompt, then sends the 'Y' key over the connection.

I haven't done this, but have seen it mentioned a on the forums somewhere.
 
samcarney
just joined
Topic Author
Posts: 17
Joined: Thu Sep 04, 2008 9:30 pm

Re: how to handle y/n response in .rsc script

Tue Apr 20, 2010 3:25 pm

ok thanks everyone for the input, I will just have the guys to hit the y key manually for now.
 
Monzinger
just joined
Posts: 2
Joined: Thu Jun 02, 2016 8:32 am

Re: how to handle y/n response in .rsc script

Thu Jun 02, 2016 8:45 am

In scripts y/n responds you don't need to handle. :D
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: how to handle y/n response in .rsc script

Thu Jun 09, 2016 1:27 pm

Congratulation, you managed to digging 6 year old topic . Prompt was removed when running commands from scripts for a very long time now.
 
duleorlovic
just joined
Posts: 2
Joined: Mon Feb 26, 2018 12:42 pm

Re: how to handle y/n response in .rsc script

Fri Mar 02, 2018 3:48 pm

Prompt was removed when running commands from scripts for a very long time now.
When I run
/import some_script.rsc
I got prompt on my Windbox 6.38.7
[admin@MikroTik] > /import flash/reset.rsc 
Dangerous! Reset anyway? [y/N]: 
n
action cancelled


Script file loaded and executed successfully
and reset.rsc contains
/system reset-configuration
 
iprok
just joined
Posts: 5
Joined: Mon Feb 15, 2016 5:20 pm

Re: how to handle y/n response in .rsc script

Thu Dec 10, 2020 12:46 pm

The same problem is for command:
/tool user-manager database load name=filename.

I can't restore user-manager database in scripts.
 
iprok
just joined
Posts: 5
Joined: Mon Feb 15, 2016 5:20 pm

Re: how to handle y/n response in .rsc script

Thu Dec 10, 2020 1:01 pm

And I resolved it by this snippet of code:
    :execute {/tool user-manager database load name=disk1/UMDB.umb;}
    :delay 1
    :put yes
 
jacauc
just joined
Posts: 17
Joined: Sun Jan 30, 2011 3:49 am

Re: how to handle y/n response in .rsc script

Fri Nov 19, 2021 12:56 pm

And I resolved it by this snippet of code:
    :execute {/tool user-manager database load name=disk1/UMDB.umb;}
    :delay 1
    :put yes
Amazing! Thank you!

I used it to restore a mikrotik backup from script:
    
    :execute {/system backup load name=backups/goodbackup.backup password="";}
    :delay 1
    :put y
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: how to handle y/n response in .rsc script

Fri Nov 19, 2021 5:50 pm

Another time with this idiocy?

The Script (actually) does not require any form of prompting if run OUTSIDE the terminal / CLI / or as you call it.
Any script TESTED or CALLED from the terminal / CLI / etc, ASKS for a prompt, because it runs interactively.

UNDERSTAND OR NOT?

this, removing all and the ; at the end:
# tested on 6.47.10
:execute {/system backup load name=backups/goodbackup.backup password=""}
work on terminal / CLI / etc. without delay or put, is simply asincrounosly executed,
and do not ask confirmation because is executed on internal separated thread, not linked to the current terminal / CLI / etc.

Who is online

Users browsing this forum: No registered users and 24 guests