4

Sorry, I am new to python and I need to make a python script, to run some terminal commands. Normal commands are ok. But, the problem is with sudo commands. For example: I need to run:

sudo -i

So, I tried the following code.

import os
os.system("sudo -i")

This code is executing the command, but in terminal, it is asking for password. So, is it possible to add the password with the command in the script, so that, it won't ask for passwords? For example: if my password is "MYPASS12", how can I add it in the code, so it won't make problem.

Tazwar Utshas
  • 921
  • 2
  • 17
  • 30

1 Answers1

5

-S flag makes sudo read from STDIN

echo mypassword | sudo -S command

But it is better to check if your script is run by root rather than echoing the password.

Faris
  • 875
  • 1
  • 9
  • 18