-1

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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    Can you specify how the function doesn't work? Also, `pass` is a reserved keyword, I think it's invalid syntax to try to use it as a variable. – zxxc Sep 28 '18 at 20:48
  • 3
    Because of the invalid `pass` variable you should be getting a syntax error before you even get to the loop. – Barmar Sep 28 '18 at 20:51
  • It would be helpful if you could provide a working example and some test data. – June Skeeter Sep 28 '18 at 21:24
  • @Barmar I changed the variable im sorry that was an old test i put in there, now its up to date with the proper variables but its not changed the password – lavarockman Sep 28 '18 at 21:26
  • @zxxc i changed that i apologize, but it is now changed to the proper variable and im having issues with, some testing i did was to see if the line from the file is being typed right, for example i added a quotation mark at the beginning and end to see if it has any spaces or anything. – lavarockman Sep 28 '18 at 21:29
  • What do you see if you do `print(repr(username), repr(new_password))` in the function? – Barmar Sep 28 '18 at 21:38
  • @Barmar which function the defining one or the one in the for loop? – lavarockman Sep 28 '18 at 21:46
  • I only see one function, `def change_password`. There's no function in the for loop. – Barmar Sep 28 '18 at 21:47
  • Using `repr()` is better than your technique of printing with quotes around it. – Barmar Sep 28 '18 at 21:49
  • @Barmar it gives me ("'FakeAccount'", "'test'") ("'FakeUser'", "'test'") – lavarockman Sep 28 '18 at 21:51
  • Looks like you're using python 2.7. Can you try with Python 3.x? – Barmar Sep 28 '18 at 21:57
  • @Barmar i used python 3 and got 'FakeAccount' 'root' 'FakeUser' 'root' – lavarockman Sep 28 '18 at 22:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180965/discussion-between-lavarockman-and-barmar). – lavarockman Sep 28 '18 at 22:08

2 Answers2

1

On first sight, it seems to me that the problem is the file opening. Try with this:

np = "test"
with open('User.txt', 'r', encoding='UTF-8') as f:
    lines = [l for l in f.read().split("\n") if l]

for l in lines:
    change_password(l, np)
print('done')
Scrooge McDuck
  • 372
  • 2
  • 14
0

So after many differnt tests and changing of things it works perfect now, the issue was i was needing to run it in python3 as well as giving it some time to process the commands, when i put a time.sleep(5) in the function is ran smoothly after that.