'Python password reset Pexpect script execution have no effects afterword's, it throws no errors during execution

import pexpect
import pxssh
import sys
child1 = pexpect.spawn('su root -c "passwd mansingh"')
print(child1.before)

#creation of log file
child1.logfile = open("./pwalog","w")

child1.expect('Password:')
print(child1.before)


# su password
child1.sendline('XXXXX')
print(child1.before)


child1.expect('password:')
print(child1.before)

#user mansingh password 
child1.sendline('New@12#345')
print(child1.before)

# retype password
child1.expect('password:')
print(child1.before)

child1.sendline('New@12#345')
print(child1.before)


Solution 1:[1]

No need pexpect and pxssh module.

use paramiko module or use this script.

#!/usr/bin/python3
import sys
import subprocess as sp
user, password = sys.argv[1:]
password = password.encode() + b'\n'
subk = dict(stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.PIPE)
pwd_change = sp.Popen(['passwd', user], **subk)
for i in range(2):
    pwd_change.stdin.write(password)

Check whether password changed or not:
sudo chage -l Mansingh | head -1

Solution 2:[2]

I am closing this question as the issue turns out to be an authentication token error not specifically related to the script itself hence I am closing this question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mansingh Chauhan