By Tom Christiansen and Nathan Torkington
ISBN 1-56592-243-3
First Edition, published August 1998
源代码在线查看: ch15_11.htm
> > Recipe 15.10. Reading Passwords (Perl Cookbook) NAME="DC.title" CONTENT="Perl Cookbook"> NAME="DC.creator" CONTENT="Tom Christiansen & Nathan Torkington"> NAME="DC.publisher" CONTENT="O'Reilly & Associates, Inc."> NAME="DC.date" CONTENT="1999-07-02T01:43:22Z"> NAME="DC.type" CONTENT="Text.Monograph"> NAME="DC.format" CONTENT="text/html" SCHEME="MIME"> NAME="DC.source" CONTENT="1-56592-243-3" SCHEME="ISBN"> NAME="DC.language" CONTENT="en-US"> NAME="generator" CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"> REV="made" HREF="mailto:online-books@oreilly.com" TITLE="Online Books Comments"> REL="up" HREF="ch15_01.htm" TITLE="15. User Interfaces"> REL="prev" HREF="ch15_10.htm" TITLE="15.9. Checking for Waiting Input"> REL="next" HREF="ch15_12.htm" TITLE="15.11. Editing Input"> > BGCOLOR="#FFFFFF"> WIDTH="684" BORDER="0" CELLSPACING="0" CELLPADDING="0" > > ALIGN="LEFT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch15_10.htm" TITLE="15.9. Checking for Waiting Input" > SRC="../gifs/txtpreva.gif" ALT="Previous: 15.9. Checking for Waiting Input" BORDER="0"> > > ALIGN="CENTER" VALIGN="TOP" WIDTH="228" > > FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1" > CLASS="chapter" REL="up" HREF="ch15_01.htm" TITLE="15. User Interfaces" > > > > > ALIGN="RIGHT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch15_12.htm" TITLE="15.11. Editing Input" > SRC="../gifs/txtnexta.gif" ALT="Next: 15.11. Editing Input" BORDER="0"> > > > > > CLASS="sect1" > CLASS="sect1" > CLASS="title" NAME="ch15-42438" >15.10. Reading Passwords > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch15-pgfId-931" >Problem CLASS="indexterm" NAME="ch15-idx-1000005120-0" > > CLASS="indexterm" NAME="ch15-idx-1000005120-1" > > CLASS="indexterm" NAME="ch15-idx-1000005120-2" > > CLASS="indexterm" NAME="ch15-idx-1000005120-3" > > CLASS="indexterm" NAME="ch15-idx-1000005120-4" > > CLASS="indexterm" NAME="ch15-idx-1000005120-5" > > CLASS="indexterm" NAME="ch15-idx-1000005120-6" > > > > CLASS="para" >You want to read input from the keyboard without the keystrokes being echoed on the screen. For instance, you want to read passwords as CLASS="emphasis" >passwd > does, i.e. without displaying the user's password. > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch15-pgfId-937" >Solution > > CLASS="para" >Use the CPAN module Term::ReadKey, set the input mode to CLASS="literal" >noecho >, and then use CLASS="literal" >ReadLine >: > CLASS="programlisting" >use Term::ReadKey; ReadMode('noecho'); $password = ReadLine(0); > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch15-pgfId-951" >Discussion > > CLASS="para" > CLASS="xref" HREF="ch15_11.htm#ch15-37288" TITLE="checkuser" >Example 15.3 > shows how to verify a user's password. If your system uses shadow passwords, only the superuser can get the encrypted form of the password with CLASS="literal" >getpwuid >. Everyone else just gets CLASS="literal" >* > as the password field of the database, which is useless for verifying passwords. > CLASS="example" > CLASS="example" > CLASS="title" NAME="ch15-37288" >Example 15.3: checkuser > > CLASS="programlisting" >#!/usr/bin/perl -w # CLASS="indexterm" NAME="ch15-idx-1000005001-0" > >checkuser - demonstrates reading and checking a user's password use Term::ReadKey; print "Enter your password: "; ReadMode 'noecho'; $password = ReadLine 0; chomp $password; ReadMode 'normal'; print "\n"; ($username, $encrypted) = ( getpwuid $< )[0,1]; if (crypt($password, $encrypted) ne $encrypted) { die "You are not $username\n"; } else { print "Welcome, $username\n"; } CLASS="indexterm" NAME="ch15-idx-1000005122-0" > > CLASS="indexterm" NAME="ch15-idx-1000005122-1" > > CLASS="indexterm" NAME="ch15-idx-1000005122-2" > > CLASS="indexterm" NAME="ch15-idx-1000005122-3" > > CLASS="indexterm" NAME="ch15-idx-1000005122-4" > > CLASS="indexterm" NAME="ch15-idx-1000005122-5" > > CLASS="indexterm" NAME="ch15-idx-1000005122-6" > > > > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch15-pgfId-997" >See Also > > CLASS="para" >The documentation for the Term::ReadKey module from CPAN; the CLASS="olink" HREF="../prog/ch03_022.htm" > CLASS="literal" >crypt > > and CLASS="olink" HREF="../prog/ch03_063.htm" > CLASS="literal" >getpwuid > > functions in CLASS="olink" HREF="../prog/ch03_01.htm" >Chapter 3 > of CLASS="citetitle" HREF="../prog/index.htm" TITLE="Programming Perl" > CLASS="citetitle" >Programming Perl > > and in CLASS="filename" >perlfunc >(1), which demonstrate using the CLASS="filename" >stty > (1) command; your system's CLASS="filename" >crypt >(3) and CLASS="filename" >passwd >(5) manpages (if you have them) > > > CLASS="htmlnav" > > > ALIGN="LEFT" WIDTH="684" TITLE="footer"> WIDTH="684" BORDER="0" CELLSPACING="0" CELLPADDING="0" > > ALIGN="LEFT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch15_10.htm" TITLE="15.9. Checking for Waiting Input" > SRC="../gifs/txtpreva.gif" ALT="Previous: 15.9. Checking for Waiting Input" BORDER="0"> > > ALIGN="CENTER" VALIGN="TOP" WIDTH="228" > CLASS="book" HREF="index.htm" TITLE="Perl Cookbook" > SRC="../gifs/txthome.gif" ALT="Perl Cookbook" BORDER="0"> > > ALIGN="RIGHT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch15_12.htm" TITLE="15.11. Editing Input" > SRC="../gifs/txtnexta.gif" ALT="Next: 15.11. Editing Input" BORDER="0"> > > > > ALIGN="LEFT" VALIGN="TOP" WIDTH="228" >15.9. Checking for Waiting Input > ALIGN="CENTER" VALIGN="TOP" WIDTH="228" > CLASS="index" HREF="index/index.htm" TITLE="Book Index" > SRC="../gifs/index.gif" ALT="Book Index" BORDER="0"> > > ALIGN="RIGHT" VALIGN="TOP" WIDTH="228" >15.11. Editing Input > > > ALIGN="LEFT" WIDTH="684" TITLE="footer"> SIZE="-1" > > >