How to hide user input for passwords - python

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

Related

How can i pass a input variable to subprocess.popen

import subprocess
import sys
password_d=raw_input("Input password")
prog = subprocess.Popen(['runas', '/user:KP\svc','cmd.exe'],universal_newlines=True,stdin=subprocess.PIPE,stdout=sys.stdout)
prog.stdin.write(password_d)
prog.communicate()[0]
This is my code. I am trying to login as another user through runas.exe and provide the password through already stored input object password_d which is taken from the user.
When i try to do this it says the password is incorrect. How can i achieve this in python and how can i print what is being fed as input. any leads would really be helpful.

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

User permission for only execute

I have a python script on raspberryi pi 3. I want to make it only executable for x user without having root permission. It can not be readable and writable. How can I do that? I gave only x(execute) permission to the file for x user. But when I execute the script, it wants root password.
If the user has access to the script, he can modify the content himself. However, just for the sake of the answer or method, we can do something like this:
You can restrict the access to the script by getting the username of the person on the operating the system:
import getpass
if getpass.getuser() in ['user1','user2'] # allowed user list:
main() # main function
else:
print("You are not authorised to run this script")

Trouble with masking password input in 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.

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

Categories

Resources