I am running Ruby and MySQL on a Windows box.
我正在Windows框上运行Ruby和MySQL。
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 among other things.
我有一些Ruby代码需要连接到MySQL数据库a执行select。要连接到数据库,我需要提供密码和其他东西。
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.
Ruby代码可以显示一个请求密码的提示符,用户输入密码并点击Enter键。我需要的是输入的密码,显示为一行星号。
How can I get Ruby to display the typed password as a line of asterisks in the 'dos box'?
如何让Ruby将输入的密码显示为“dos”框中的星号?
6 个解决方案
#1
50
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.
为了回答我自己的问题,为了其他想知道的人的利益,有一个Ruby gem叫做HighLine,你需要它。
require 'rubygems'
require 'highline/import'
def get_password(prompt="Enter Password")
ask(prompt) {|q| q.echo = false}
end
thePassword = get_password()
#2
24
Poor man's solution:
穷人的解决方案:
system "stty -echo"
# read password
system "stty echo"
Or using http://raa.ruby-lang.org/project/ruby-password/
或者使用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 :(
编辑:哎呀,我没注意到你需要Windows:(
#3
12
According to the Highline doc, this seems to work. Not sure if it will work on Windows.
根据Highline doc的说法,这似乎是可行的。不确定它是否适用于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: ******
#4
3
The following works (lobin.rb) in ruby not jruby
下面是ruby中的工作(lobin.rb),而不是jruby
require 'highline/import'
$userid = ask("Enter your username: ") { |q| q.echo = true }
$passwd = ask("Enter your password: ") { |q| q.echo = "*" }
Output from console:
从控制台输出:
E:\Tools>ruby login.rb
Enter your username: username
Enter your password: ********
Howerver if I run in jruby it fails and gives no opportunity to enter your password.
但是,如果我在jruby中运行,它会失败,并且没有机会输入您的密码。
E:\Tools>jruby login.rb
Enter your username: username
Enter your password:
#5
0
The fancy_gets gem has a password thing that works fine with jruby:
fancy_gets gem有一个适用于jruby的密码:
https://github.com/lorint/fancy_gets
https://github.com/lorint/fancy_gets
Code ends up like:
最终代码:
require 'fancy_gets'
include FancyGets
puts "Password:"
pwd = gets_password
# ...
#6
0
Starting from Ruby 2.3 you can use the IO#getpass
method as such:
从Ruby 2.3开始,您可以使用IO#getpass方法:
STDIN.getpass("Password: ")
http://ruby-doc.org/stdlib-2.3.0/libdoc/io/console/rdoc/IO.html#method-i-getpass
http://ruby-doc.org/stdlib-2.3.0/libdoc/io/console/rdoc/IO.html method-i-getpass
The above is copied from a deleted answer by Zoran Majstorovic.
上面的答案是从被删除的Zoran Majstorovic的答案中复制过来的。
#1
50
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.
为了回答我自己的问题,为了其他想知道的人的利益,有一个Ruby gem叫做HighLine,你需要它。
require 'rubygems'
require 'highline/import'
def get_password(prompt="Enter Password")
ask(prompt) {|q| q.echo = false}
end
thePassword = get_password()
#2
24
Poor man's solution:
穷人的解决方案:
system "stty -echo"
# read password
system "stty echo"
Or using http://raa.ruby-lang.org/project/ruby-password/
或者使用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 :(
编辑:哎呀,我没注意到你需要Windows:(
#3
12
According to the Highline doc, this seems to work. Not sure if it will work on Windows.
根据Highline doc的说法,这似乎是可行的。不确定它是否适用于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: ******
#4
3
The following works (lobin.rb) in ruby not jruby
下面是ruby中的工作(lobin.rb),而不是jruby
require 'highline/import'
$userid = ask("Enter your username: ") { |q| q.echo = true }
$passwd = ask("Enter your password: ") { |q| q.echo = "*" }
Output from console:
从控制台输出:
E:\Tools>ruby login.rb
Enter your username: username
Enter your password: ********
Howerver if I run in jruby it fails and gives no opportunity to enter your password.
但是,如果我在jruby中运行,它会失败,并且没有机会输入您的密码。
E:\Tools>jruby login.rb
Enter your username: username
Enter your password:
#5
0
The fancy_gets gem has a password thing that works fine with jruby:
fancy_gets gem有一个适用于jruby的密码:
https://github.com/lorint/fancy_gets
https://github.com/lorint/fancy_gets
Code ends up like:
最终代码:
require 'fancy_gets'
include FancyGets
puts "Password:"
pwd = gets_password
# ...
#6
0
Starting from Ruby 2.3 you can use the IO#getpass
method as such:
从Ruby 2.3开始,您可以使用IO#getpass方法:
STDIN.getpass("Password: ")
http://ruby-doc.org/stdlib-2.3.0/libdoc/io/console/rdoc/IO.html#method-i-getpass
http://ruby-doc.org/stdlib-2.3.0/libdoc/io/console/rdoc/IO.html method-i-getpass
The above is copied from a deleted answer by Zoran Majstorovic.
上面的答案是从被删除的Zoran Majstorovic的答案中复制过来的。