So I Created this function that changes a UNIX users password, and it works just fine but I want to for loop a list of users to change every users password. But the for loop doesn't work in the function, When i assign a variable for the same user in the list it works just not in the loop
User.txt
FakeAccount
FakeUser
Python Function
def change_password(username, new_password):
process = pexpect.spawn("sudo passwd " + username)
process.expect("Enter new UNIX password: ")
process.sendline(new_password)
process.expect("Retype new UNIX password: ")
process.sendline(new_password)
process.close()
For Loop
np = "test"
f = open('User.txt', 'r')
for line in f:
change_password(line.strip("\n\r"), np)
print('done')
f.close()
What i'm trying to do is loop through the file and put that in place for the the username variable, when i run the code there are no errors however when i try and to login into these accounts their passwords stay the same, When i run the python script i am root so that there are not sudo password prompts.
What i think is the problem, from what I've tried
I think the problem if somwhere in the process of for-looping the file because if i run this code it works perfectly
import pexpect
def change_password(username, new_password):
process = pexpect.spawn("sudo passwd " + username)
process.expect("Enter new UNIX password: ")
process.sendline(new_password)
process.expect("Retype new UNIX password: ")
process.sendline(new_password)
process.close()
np = "test"
U = "FakeUser"
change_password(U, np)
It's only when i introduce that for loop when i get an issue, and to my knowledge its grabbing the same username as i typed in my test Example because to test that i tried
f = open('User.txt', 'r')
for line in f:
print("'" + line.strip("\n\r") + "'")
and got the results 'FakeAccount' and 'FakeUser' which means the usernames are right, is it possible that the for loop is going to fast and not sending then through the terminal? or am i missing somthing?