Prevent Terminal Echo Using Python (for Passwords)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Prevent Terminal Echo Using Python (for Passwords)
I have a Python program that gets a password from the user in order to log in to an ftp server using an ftplib.FTP() object. The program runs off the command line; and the problem is that the password is in plain view when typed in. Is there any way to 'cloak' the text being typed in, i.e. to prevent the keyboard input from being echoed to the terminal? (Like the behavior in ssh, for example.) I'm thinking maybe the readline module would allow me to do that, but I don't know how specifically. Thanks!
I'm not sure if there is a pure python method but you could just make a system call before and after:
# Disable local echo
stty -echo
# Ask for password here
# Enable local echo
stty echo
Great! I didn't know about that command, but it sure does the trick. And it's much simpler than the curses module I've been wrangling with for the last hour. Thanks a lot!
I turned back to my curses problem after writing the os.system() call into my script, and suddenly realised why everything was going so totally wrong with it. I had made the script executable and was merely running it from the command line (i.e. "$ ./programs/python/test_curses.py"), but I didn't have a "#!/usr/bin/env python" (or any other hash-bang line) at the beginning of the file. And here I wondered why it just suddenly started dumping errors in a non-Python style. Just one of those silly programming mistakes, you know? So I ended up and played around with it for the rest of the evening and I think I have an ok handle on curses now. You're right, it isn't bad at all. If anyone's curious, the result (such as it is right now) is on my website, or more specifically: http://mysite.verizon.net/taylor_ven...ebpublish.html. Thanks again to both of you!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.