Question

I am trying to create a Telnet Server using Python on Ubuntu 12.04. In order to be able to execute commands as a different user, I need to use the su command, which then prompts for the password. Now, I know that the prompt is sent to the STDERR stream, but I have no idea which stream I am supposed to send the password to. If I try to send it via STDIN, I get the error: su: must be run from a terminal. How do I proceed?

Was it helpful?

Solution

If you really want to use system's su program, you will need to create a terminal pair, see man 7 pty, in python that's pty.openpty call that returns you a pair of file descriptors, one for you and one for su. Then you have to fork, in the child process change stdin/out/err to slave fd and exec su. In the parent process you send data to and receive data from master fd. Linux kernel connects those together.

Alternatively you could perhaps emulate su instead?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top