tags:

views:

1803

answers:

3

Hi,

I am running ruby and MySQL on a Windows box.

I have some Ruby code that needs to connect to a MySQL database a perform a select. To connect to the database I need to provide the password (amongst other things). My question is this: How can I get ruby to display the types password as a line of asterisks in the 'dos box'.

In other words, the ruby code can display a prompt requesting the password, the user types in the password and hits the enter key. What I need is for the password, as it is typed, to be displayed as a line of asterisks.

Many thanks.

+1  A: 

Poor man's solution:

system "stty -echo"
# read password
system "stty echo"

Or using http://raa.ruby-lang.org/project/ruby-password/

The target audience for this library is system administrators who need to write Ruby programs that prompt for, generate, verify and encrypt passwords.

Edit: Whoops I failed to notice that you need this for Windows :(

jk
+8  A: 

To answer my own question - and for the benefit of anyone else who would like to know:

There is a ruby gem called highline that you need.

require 'rubygems'
require 'highline/import'

def get_password(prompt="Enter Password")
   ask(prompt) {|q| q.echo = false}
end

thePassword = get_password()

Works a treat!

Simon Knights
+1  A: 

According to the Highline doc, this seems to work. Not sure if it will work on Windows.

#!/usr/local/bin/ruby
require 'rubygems'
require 'highline/import'

username = ask("Enter your username:  ") { |q| q.echo = true }
password = ask("Enter your password:  ") { |q| q.echo = "*" }

Here's the output on the console:

$ ruby highline.rb 
Enter your username:  doug
Enter your password:  ******
Eric Monti