Reputation: 1093
I want to validate root password which is entered by user script which is used to install the driver which requires root.
if I try to run that installer as a sudo
or superuser privilege
it gives below Error:
ERROR: installer must be run as root
I am accepting the password with the help of getpass()
method at the beginning of a script and will use that password in between the execution of it so don't want to make a user wait for such long time.
I found what-is-the-best-way-for-checking-if-the-user-of-a-script-has-root-like-privileg and how-to-verify-a-users-password-for-root-privledges-in-python but this will check validation of sudo password not the root
.
I want to check password is valid for root login.
How Should I validate that user entered the password
is correct for root
using python?
As well as My knowledge is a concern I have to check the validity of root password by performing so su
operation
we can not run the whole script as a root as after installation script is going to run some test as a normal user.
Upvotes: 2
Views: 1663
Reputation: 1093
After reading multiple stuff somehow I figure out that we can authenticate root password via a Python script. Below is My approach to Authenticate the Same.
import subprocess
import getpass
FAIL = 'Password: \r\nsu: Authentication failure'
def validate_pass(passwd):
ret = 0
try:
cmd = '{ sleep 1; echo "%s"; } | script -q -c "su -l root -c ls /root" /dev/null' % passwd
ret = subprocess.check_output(cmd, shell=True)
return ret
except:
return 1
passwd = getpass.getpass(prompt='Password: ', stream=None)
res = validate_pass(passwd).strip()
if FAIL == res:
print(res)
print ("Invalid paasword")
else:
print(res)
print ("Valid paasword")
Upvotes: 1