Trouble with masking password input in Python - python

I am using Python. I am making a script where the user has to enter the password in the terminal.
I have already found a solution on this website by using the getpass module.
new_password=getpass.getpass(prompt="Type new password: ")
The problem is I get a warning and the password input gets displayed as well.
Warning (from warnings module):
File "C:\Python34\lib\getpass.py", line 101
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.

Use command prompt as admin to run this program.
Reason is because system environment where stdin, stdout and stderr are connected to /dev/tty, or another PTY-compliant device.
The IDLE REPL does not meet this requirement.
And change new_password=getpass.getpass(prompt="Type new password: ") to new_password=getpass.getpass("Type new password: ") if you are using Windows OS or new_password=getpass.getpass("Type new password: ", None) for Linux distributions.
This would help you for sure:
import getpass
pw = getpass.getpass("Enter Your Password Here: ")
if pw == "password":
print("You are Welcome...")
else:
print("Sorry! You're are not allowed.")
As per Python documentation:
getpass.getpass([prompt[, stream]])
Prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to 'Password: '. On Unix, the prompt is written to the file-like object stream. stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows)
Changed in version 2.5: The stream parameter was added.
Note: If you call getpass from within IDLE, the input may be done in the terminal you launched IDLE from rather than the idle window itself.

Using a normal terminal with this code:
import getpass
new_password=getpass.getpass(prompt="Type new password: ")
print(new_password)
Will work alright, but if we try the same with IDLE we'll get the error you've exposed in your question.
Now, if we look at the docs here you'll see this is intended, it says:
Note If you call getpass from within IDLE, the input may be done in
the terminal you launched IDLE from rather than the idle window
itself.

Related

How to hide user input for passwords

I'm trying to write a program which includes a login function. Ideally I want to hide the password input. I've done some searching and found the getpass module yet can't seem to get it work.
from getpass import getpass
password = getpass("Please enter your password: ")
That's what I've got so far and every time it just throws up this error message:
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Please enter your password:
It's probably me being really stupid but I cannot for the life of me figure out what I've done wrong

How to use getpass() to hide a users password as it is entered [duplicate]

This question already has answers here:
"GetPassWarning: Can not control echo on the terminal" when running from IDLE
(4 answers)
Closed 5 years ago.
I am trying to make a small program which checks the strength of the user password. When the user is prompted to enter their password I want the input to show as asterisks or dots instead of the text input. Is there a way of hiding this input.
I have used getpass to try and hide the input without success.
from getpass import getpass
passwd = getpass('Please enter your password')
This gives me the following warning message:
Warning (from warnings module):
File "C:\Program Files (x86)\Python36-32\lib\getpass.py", line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Please enter your password: this_is_my_password
The password is entered on the line but instead of coming up with astrisks it comes up with the original characters.
As the program will check the strength of the password I need to have something that does not save the password as the asterisks but as its original characters.
As well as this is there any way of incorporating a way of showing the user password if they perform an action. I was considering using a button but that is an area in Python I am not confident with so could I program in another way with a specific key.
Sudo code for project.
userPass = hidden(input("Please enter your password "))
if showPassword selected:
show the password
else:
move on with the rest of the program
The warning basically means you are running your program in an environment where Python cannot use the regular interactive IOCTL calls to implement the asterisks-only functionality. Running a program from inside your IDE basically causes the IDE to run the program in a pseudo-terminal. Try running it on the command line instead.
The getpass module contains a getpass function that works just like input(), except that it turns the terminal echo off.
The result is the password entered, which you can use to determine its strength or print as needed, but while the user is entering their password, nothing is printed except the prompt.
>>> from getpass import getpass
>>> passwd = getpass('Please enter your password')
Please enter your password
>>> print passwd
This is my password

How to automate shell interactive commands using Python pexpect module

I am trying to automate the setup of an application by performing SSH to the machine and goto /var/packages folder and execute the script.when the installation starts a set of interactive commands to be send based on the expected output.I found from google that pexpect can achieve this but i am unable to achieve the result that i wish. I am trying following code , can someone guide me how to achieve this as I am beginner to python.Any help would be appreciated. My application setup would look like this
[root#bits packages]# ./SHR_setup.bin -i console
Preparing to install...
Extracting the JRE from the installer archive...
Unpacking the JRE...
Extracting the installation resources from the installer archive...
Configuring the installer for this system's environment...
Launching installer...
===============================================================================
Choose Locale...
----------------
1- Deutsch
->2- English
3- Español
4- Français
5- Italiano
6- Nederlands
7- Português (Brasil)
CHOOSE LOCALE BY NUMBER: 2
I accept the terms of the License Agreement (Y/N): Y
Please hit Enter to continue:
Python Code
from pexpect import pxssh
import pexpect
try:
s = pxssh.pxssh()
hostname = '10.110.40.20'
username = 'admin'
password = 'admin123'
s.login(hostname, username, password)
s.sendline('cd /var/packages') # goto /var/packages folder
child = pexpect.spawn('./SHR_setup.bin -i console') # start the application setup in packages folder
child.expect('CHOOSE LOCALE BY NUMBER') # expect output like this
child.sendline('2')
s.prompt()
print s.before
except pxssh.ExceptionPxssh, e:
print 'pxssh failed on login'
print e
You should change
s.sendline('cd /var/packages')
child = pexpect.spawn('./SHR_setup.bin -i console')
to
s.sendline('cd /var/packages')
s.sendline('./SHR_setup.bin -i console')
spawn is supposed to run a program on the local host, not on the remote host.
You're on the right track with using the s.before log for debugging.
The app you're interacting with appears to be more screen-oriented than line-oriented, which can pose some difficulties, including ANSI escape sequences for color and position. Consider running child.expect('Something else'), some string which does reliably show up in before, then do a brief sleep(), then just "blindly" send "2" or "y" or whatever, pausing briefly between sends.

How to pass arguments to linux command prompt using python script

Demo.py
import os
f = os.popen('passwd')
output:
Enter Existing Password: (This one i want program to enter )
I am running this python program which prompts me to Enter existing password. But i want to send that existing password through the program.
is there any way i could pass that value to command prompt via program.
We can achieve this by using python's expect module. I have written the sample code:
#!/usr/bin/python
import pexpect
import sys
child = pexpect.spawn('passwd')
child.logfile=sys.stdout
child.expect("UNIX password:")
child.sendline('guest')
child.expect('Enter new UNIX password:')
child.sendline('guest')
child.expect('Retype new UNIX password:')
child.sendline('guest')
You can also check more details of expect module at http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect-module-in-python

Can i get console input without echo in python?

Can I get console input without echo in python?
Use getpass:
>>> from getpass import getpass
>>> getpass()
Password:
'secret'
There is also another solution (at least on unix systems, I don't know if this is working on Windows). Simply switch off the console output and use raw_input:
os.system("stty -echo")
password = raw_input('Enter Password:')
os.system("stty echo")
print "\n"
Maybe the 'console' module is your only bet (it's kinda 'fork' of the curses module for Unix), however I haven't seen anything related to terminal echo disabling in its homepage, you might try to dig into it by yourself.

Categories

Resources