简体   繁体   English

如何从Ruby中的命令行读取密码?

[英]How can I read a password from the command line in Ruby?

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. 我有一些需要连接MySQL数据库的Ruby代码执行select。 To connect to the database I need to provide the password among other things. 要连接到数据库,我需要提供密码等。

The Ruby code can display a prompt requesting the password, the user types in the password and hits the Enter key. Ruby代码可以显示请求密码的提示,用户输入密码并按Enter键。 What I need is for the password, as it is typed, to be displayed as a line of asterisks. 我需要的是密码,因为它是键入的,显示为一行星号。

How can I get Ruby to display the typed password as a line of asterisks in the 'dos box'? 如何让Ruby在'dos box'中将键入的密码显示为一行星号?

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. 要回答我自己的问题,并且为了其他任何想知道的人的利益,你需要一个名为HighLine的Ruby宝石。

require 'rubygems'
require 'highline/import'

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

thePassword = get_password()

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. 此库的目标受众是需要编写提示,生成,验证和加密密码的Ruby程序的系统管理员。

Edit: Whoops I failed to notice that you need this for Windows :( 编辑:哎呀我没注意到你需要这个Windows :(

According to the Highline doc, this seems to work. 根据Highline文档,这似乎有效。 Not sure if it will work on Windows. 不确定它是否适用于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:  ******

The following works (lobin.rb) in ruby not jruby 以下作品(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:

Starting from Ruby 2.3 you can use the IO#getpass method as such: 从Ruby 2.3开始,您可以使用IO#getpass方法:

require 'io/console' 

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删除的答案中复制的内容。

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
# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM