# Getpass

**URL:** <https://www.ruby-forum.com/t/getpass/134467>\
**Category:** Ruby\
**Created:** [April 11, 2008, 4:57am UTC](https://www.ruby-forum.com/t/getpass/134467 "2008-04-11T04:57:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 11, 2008, 4:57am UTC](https://www.ruby-forum.com/t/getpass/134467/1 "2008-04-11T04:57:55Z")

</div>

Hi, how do I do getpass() in ruby? Thanks!

---

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 11, 2008, 5:09am UTC](https://www.ruby-forum.com/t/getpass/134467/2 "2008-04-11T05:09:37Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Yang Z. wrote:  
| Hi, how do I do getpass() in ruby? Thanks!

def getpass  
~ “do getpass()”  
end

Please elaborate on what you mean, and want to do.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)

Use data arrays to avoid repetitive control sequences.  
~ - The Elements of Programming Style (Kernighan & Plaugher)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkf+1i0ACgkQbtAgaoJTgL870wCeMdCUU+JnWgaWkq1Xx6tqHraN  
j6kAnjXLAWKYupSM/Z/65oWPvZp+z0AU  
=blU3  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 11, 2008, 6:09am UTC](https://www.ruby-forum.com/t/getpass/134467/3 "2008-04-11T06:09:48Z")

</div>

Thanks for prompting me to provide some context. getpass() is a  
standard POSIX function, described here:

[http://www.opengroup.org/pubs/online/7908799/xsh/getpass.html](http://www.opengroup.org/pubs/online/7908799/xsh/getpass.html)

---

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 11, 2008, 6:17am UTC](https://www.ruby-forum.com/t/getpass/134467/4 "2008-04-11T06:17:24Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Yang Z. wrote:  
| Thanks for prompting me to provide some context. getpass() is a  
| standard POSIX function, described here:  
|  
| [http://www.opengroup.org/pubs/online/7908799/xsh/getpass.html](http://www.opengroup.org/pubs/online/7908799/xsh/getpass.html)

In that case, you’ll need a C extension.

RubyInline is one library that provides that functionality (I assume  
portability to non-UNICES is not really a concern ;).

Here are some presentations to using C with Ruby from Ruby conferences:

[http://rubyhoedown2007.confreaks.com/session08.html](http://rubyhoedown2007.confreaks.com/session08.html)  
[http://rubyconf2007.confreaks.com/d1t1p6\_avoiding\_pitfalls\_in\_c\_extensions.html](http://rubyconf2007.confreaks.com/d1t1p6_avoiding_pitfalls_in_c_extensions.html)

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)

~ - You know you’ve been hacking too long when…  
…you send E-mail and end each line with \n.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkf+5jcACgkQbtAgaoJTgL/0BACdG40D0Q4eI1CgMkXfBmmn92aC  
XYwAnAlRdaxQzIJAnV9Visz4MRhPzvTK  
=2K16  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 13, 2008, 7:10am UTC](https://www.ruby-forum.com/t/getpass/134467/5 "2008-04-13T07:10:58Z")

</div>

On Thu, 10 Apr 2008 23:08:40 -0500, Yang Z. wrote:

> Thanks for prompting me to provide some context. getpass() is a  
> standard POSIX function, described here:
> 
> [http://www.opengroup.org/pubs/online/7908799/xsh/getpass.html](http://www.opengroup.org/pubs/online/7908799/xsh/getpass.html)

getpass() is not a POSIX anything.

From the manpage:

DESCRIPTION  
This function is obsolete. Do not use it.  
…  
CONFORMING TO  
Present in SUSv2, but marked LEGACY. Removed in POSIX.1-2001.

In theory, using Ruby/DL should work, like

require ‘dl/import’  
module LibC  
extend DL::Importable  
dlload ‘libc.so.6’  
extern ‘char \*getpass( const char \* prompt )’  
end

In theory, theory and practice are the same.

/usr/lib/ruby/1.8/dl/import.rb:126:in `symbol': can't find the symbol `getpass’ (RuntimeError)  
from /usr/lib/ruby/1.8/dl/import.rb:145:in `import' from /usr/lib/ruby/1.8/dl/import.rb:61:in `extern’

But this works:

require ‘dl’  
module LibC  
extend self  
LIB=DL.dlopen(‘libc.so.6’)  
SYMS={ :getpass =\> LIB[‘getpass’,‘SS’] }  
def getpass prompt  
r,rs = SYMS[:getpass].call prompt  
return r  
end  
end

Again, don’t use this. Use Highline instead.

–Ken

---

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 11, 2008, 6:26am UTC](https://www.ruby-forum.com/t/getpass/134467/6 "2008-04-11T06:26:10Z")

</div>

Its not the getpass method, but has the same affect using Highline  
[http://highline.rubyforge.org/](http://highline.rubyforge.org/)

% cat password.rb  
require “rubygems”  
require “highline/import”

pass = ask("Enter your password: ") { |q| q.echo = false }  
puts “Your password is [#{pass}]”

pass = ask("Enter your password: ") { |q| q.echo = ‘\*’ }  
puts “Your password is ‘#{pass}’”

% ruby password.rb  
Enter your password:  
Your password is [iloveruby]  
Enter your password: \*\*\*\*\*\*\*\*\*\*\*  
Your password is ‘i love ruby’

enjoy,

-jeremy

# –

Jeremy H. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 13, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/getpass/134467/7 "2008-04-13T20:17:31Z")

</div>

Sorry if I’m doing this wrong, but how do I get off the mailing list?  
I can’t figure it out.

---

<div class="post-metadata">

**Author:** ![Yang\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yang\_Z](https://www.ruby-forum.com/u/Yang_Z)\
**Post date:** [April 13, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/getpass/134467/8 "2008-04-13T22:20:46Z")

</div>

On Sun, Apr 13, 2008 at 1:16 PM, zor [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry if I’m doing this wrong, but how do I get off the mailing list? I  
> can’t figure it out.

From [http://www2.ruby-lang.org/en/20020104.html](http://www2.ruby-lang.org/en/20020104.html) …

“To unsubscribe from a list, send a mail which body is “unsubscribe”  
to the controller address.”

The controller address is ruby-talk-ctl on the [ruby-lang.org](http://ruby-lang.org) domain.

I still haven’t figured out why so many people get this wrong.

Todd
