I want to run the shell command 'su - testuser -c "id"' and get the output. In the console it asks the password after that. My intention is to run it in a python script where it logs into antoher user (neither the source nor the destination user has root rights). The problem is, that the password should be entered non-interactive, so that I can just start the script and see the output without entering the password. So I can start the python script and it automatically runs the command without waiting for the password and gives me the output.
I tried it using the pexpect package:
child = pexpect.spawn('su - testuser -c "id"')
child.expect_exact('Password:')
child.sendline('mypassword')
print(child.before) # Prints the output of the "id" command to the console
The problem is that the code doesn't function. The output is like a random string instead of the id and so on. How can I do that?