Forum: Ruby mask gets_chomp ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rebhan, Gilbert (Guest)
on 2007-03-05 15:16
(Received via mailing list)
Hi,

i have a script that uses a password as input.

The password is provided via gets_chomp
in the shell.

Is there a way to mask the password with *
or do i have to write a gui for that ?


Regards, Gilbert
Robert K. (Guest)
on 2007-03-05 15:55
(Received via mailing list)
On 05.03.2007 14:15, Rebhan, Gilbert wrote:
> Hi,
>
> i have a script that uses a password as input.
>
> The password is provided via gets_chomp
> in the shell.
>
> Is there a way to mask the password with *
> or do i have to write a gui for that ?

Maybe you can do that via curses.  Other than that, you could use "stty
-echo" before and "stty echo" after the password input:

14:50:07 [~]: ruby -e 'system("stty", "-echo")
 > puts "enter!"
 > pass=gets.chomp
 > puts "You entered #{"*" * pass.size}"
 > system("stty", "echo")'
enter!
You entered ******
14:50:56 [~]:

Kind regards

  robert
Rebhan, Gilbert (Guest)
on 2007-03-05 16:06
(Received via mailing list)
Hi,

/*
14:50:07 [~]: ruby -e 'system("stty", "-echo")
 > puts "enter!"
 > pass=gets.chomp
 > puts "You entered #{"*" * pass.size}"
 > system("stty", "echo")'
enter!
You entered ******
14:50:56 [~]:
*/


How to adopt that for =

cvsrepos=%w[...]
cvspass=gets.chomp

cvsrepos.each {|x|
puts "Login CVS Repository >> #{x} ..."
IO.popen("#{CVSEXE} -d
:pserver:#{ENV["USERNAME"]}:#{cvspass}@cvshostd:/cvsrepos/#{x} login")
}
puts "Login successful !!"


the password is readable in the cmd shell

Regards, Gilbert
Xavier N. (Guest)
on 2007-03-05 16:17
(Received via mailing list)
On Mar 5, 2007, at 2:15 PM, Rebhan, Gilbert wrote:

> i have a script that uses a password as input.
>
> The password is provided via gets_chomp
> in the shell.
>
> Is there a way to mask the password with *
> or do i have to write a gui for that ?

Just in case, are you aware of HighLine's ask mehod?

   require 'rubygems'
   require 'highline/import'

   pass = ask("Password: ") {|q| q.echo = false}

-- fxn
This topic is locked and can not be replied to.