Password:

M

mgarriss

Any easy and elegant way to do the 'no output to the screen' thing for
entering passwords. I don't want to have to use ncurses. Also, can I
force stdin to be a tty like 'su' does?

Michael
 
M

mgarriss

mgarriss said:
Any easy and elegant way to do the 'no output to the screen' thing for
entering passwords. I don't want to have to use ncurses. Also, can I
force stdin to be a tty like 'su' does?

Michael


I should mention that I'm on a GNU/Linux but it'd be nice to have a
cross-platform solution to this.
 
J

James F.Hranicky

This is a multi-part message in MIME format.

--Multipart_Thu__28_Aug_2003_16:25:45_-0400_00ab1d00
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

I should mention that I'm on a GNU/Linux but it'd be nice to have a
cross-platform solution to this.

If the termios module works on windows ( I don't know if it does ), the
attached should work. Either way it should work on Unix (and requires
termios, obviously).

It defines noecho() and getpass() for IO, and getpass() uses noecho().

Jim
--Multipart_Thu__28_Aug_2003_16:25:45_-0400_00ab1d00
Content-Type: text/plain;
name="termios-noecho.rb.txt"
Content-Disposition: attachment;
filename="termios-noecho.rb.txt"
Content-Transfer-Encoding: 7bit

require 'termios'

class IO
def noecho
if ! block_given?
raise "Block required for noecho()"
else
begin
term = Termios::getattr(self)
term.c_lflag &= ~Termios::ECHO
Termios::setattr(self, Termios::TCSANOW, term)
yield self
ensure
term.c_lflag |= Termios::ECHO
Termios::setattr(self, Termios::TCSANOW, term)
end
end
end

def getpass(prompt)
stderr_sync = STDERR.sync
STDERR.sync = 1

STDERR.print(prompt)
pw = nil

noecho { |s|
pw = s.gets.chomp
}
STDERR.puts
STDERR.sync = 0 unless stderr_sync
return pw
end
end

--Multipart_Thu__28_Aug_2003_16:25:45_-0400_00ab1d00--
 
A

ahoward

I should mention that I'm on a GNU/Linux but it'd be nice to have a
cross-platform solution to this.

~ > cat ./password.rb
require 'password' # get from the RAA

def password
password = Password.get('Password')
if block_given? # block form is safer!
begin
yield password
ensure
password.replace ''
end
else
# you should delete/replace this so strace can't show it!
return password
end
end

password do |pswd|
puts pswd
end

puts password()

~ > ruby ./password.rb
Password:
foobar
Password:
barfoo


-a
====================================
| Ara Howard
| NOAA Forecast Systems Laboratory
| Information and Technology Services
| Data Systems Group
| R/FST 325 Broadway
| Boulder, CO 80305-3328
| Email: (e-mail address removed)
| Phone: 303-497-7238
| Fax: 303-497-7259
| ~ > ruby -e 'p(%.\x2d\x29..intern)'
====================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,122
Messages
2,570,715
Members
47,282
Latest member
hopkins1988

Latest Threads

Top