If you really want to use the system su program, you will need to create a pair of terminals, see man 7 pty , in python, that call pty.openpty , which returns you a couple of file descriptors, one for you and one for su. Then you have fork, in the child process change stdin / out / err to slave fd and exec su. In the parent process, you send data and receive data from master fd. The Linux kernel connects them together.
Alternatively, could you emulate su instead?
source share