From: "James F.Hranicky" Date: 2003-08-29T05:25:53+09:00 Subject: Re: Password: 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 On Fri, 29 Aug 2003 04:15:03 +0900 mgarriss wrote: > mgarriss wrote: > > > 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. 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--