I am trying to perform a recursive distribution of permissions for all users for the / myFolder / path directory:

big_chmod = 'sudo chmod -R 777 /myFolder/path' print subprocess.Popen(big_chmod, shell=True, stdout=subprocess.PIPE).stdout.read() 

I get the error:

sudo: no tty present and no askpass program specified

Please tell me why this command does not work, and what is the best way to execute it from Python code?

  • And if you do not accidentally ask for a password from the console with your hands to start sudo? at least you need her not to ask him. help.ubuntu.ru/wiki/… - Mike
  • @Mike, but I now think that it is possible to carry out the password input somehow (through popen arguments, or in some other cool way). Because it’s bad if sudo doesn't ask for a password! - neo

1 answer 1

In order not to spoil the security of the system, you can use stdin=PIPE , the script will ask for the password, you will enter it interactively.

 big_chmod = 'sudo chmod -R 777 /myFolder/path' print subprocess.Popen(big_chmod, shell=True, stdin=PIPE, stdout=subprocess.PIPE).stdout.read() 

Of course, this is only a special case for a piece of your code.

UPD

If automation is necessary for you, I offer such INSECURE way:

 big_chmod = 'sudo chmod -R 777 /myFolder/path' password = 'PASSWORD' p = Popen(['sudo', '-S'] + big_chmod, stdin=PIPE, stderr=PIPE, universal_newlines=True) sudo_prompt = p.communicate(password + '\n') 

However, I would advise you not to start on this slippery slope, but, for example, set up access rights for your specific user and his specific actions in sudoers .

  • Thanks for the proposed solution! However, interactive password entry will not work. Everything should be automated. - neo
  • @neo updated the answer - approximatenumber