8

i defined a function that switch my proxy settings every now and then, problem is that i want it to run in a loop without manual intervention. But when i execute the program in sudo it gets called the first time en runs smoothly, second time it asks me for my sudo password. Here is the bit of code:

def ProxySetting(Proxy):
    print "ProxyStetting(Proxy)"
    call("networksetup -setwebproxy 'Wi-Fi' %s" "on" % Proxy, shell = True)
    call("networksetup -setsecurewebproxy 'Wi-Fi' %s" "on" % Proxy, shell = True)
    call("networksetup -setftpproxy 'Wi-Fi' %s" "on" %Proxy , shell=True)

I could use threading but am sure there is a way of doing it that wont cause problems. How can i hard code my sudo password so that it runs at the beginning of the function?

TB1
  • 269
  • 2
  • 5
  • 10
  • It's better to create a service using **systemd** and add the permissions on the service configuration file. – Mikael Jun 21 '17 at 19:40
  • I don't know if I understand your problem correctly, but you might want to have a look at my answer https://stackoverflow.com/a/44540122/7738328 which talks about using `sudo` in a `subprocess`. But, as mentioned above, it is better if you can completely avoid using `sudo` for command execution. – JohanL Jun 21 '17 at 19:50
  • Simply put this code won't run unless I type my password. I want to automate that script meaning never have to manually type that password. I will try Mikael solution, but would rather not have to add permissions to every file that requires a root command. – TB1 Jun 21 '17 at 20:19
  • But what you probably want even less, is to have your `sudo` password in clear text in your Python script. – JohanL Jun 21 '17 at 20:33
  • Probably adding your file to sudoers will help. – GIZ Jun 21 '17 at 21:36
  • You guys are definitely correct, hard coding my password sounds stupid. but this code is only going to run on my raspberry pi nothing of real value on it. – TB1 Jun 22 '17 at 06:17
  • Also instead of hardcoding it i will put it as raw_input() in the starting of the script. – TB1 Jun 22 '17 at 06:51
  • Does this answer your question? [running a command as a super user from a python script](https://stackoverflow.com/questions/567542/running-a-command-as-a-super-user-from-a-python-script) – miken32 Aug 31 '21 at 15:43

2 Answers2

16

Here you can execute a command sudo without interactive prompt asking you to type your password :

from subprocess import call    

pwd='my password'
cmd='ls'

call('echo {} | sudo -S {}'.format(pwd, cmd), shell=True)
glegoux
  • 3,505
  • 15
  • 32
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Jun 22 '17 at 00:19
  • 3
    It should be noted that storing your password in plaintext in your code is a security issue and generally bad practice. – Josh Correia May 29 '19 at 13:52
3

Another method of passing your password to a shell command through python that wouldn't involve it showing up in any command history or ps output is:

p = subprocess.Popen(['sudo', self.resubscribe_script], stdin=subprocess.PIPE)
p.communicate('{}\n'.format(self.sudo_password))

Note that using communicate will only allow one input to be given to stdin; there are other methods for getting a reusable input.

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53