1

I'm trying to create a script that will edit my hosts file to block distracting sites. I'm trying to do so through Python on my mac. I've made the script that edits the host file, but I've only been able to execute it by running

    sudo python change_hosts.py

I'd like to be able to run it by calling it from another script. Is there anyway I can package/wrap the script in a way that it can be run without having to go through terminal every time? Can I give it special permission to edit hosts without causing security problems? I read somewhere about an os admin token, is something like that possible in a python script, so that I could enter the password once on boot or something?

Any help appreciated, thanks in advance

jackmac92
  • 75
  • 1
  • 8
  • 1
    sudo is `substitute user do` and I think if you call from script executed as root user to another script, this last script would have to be executed as root user too. So, call the first script with sudo may be enough.. – aabilio Sep 30 '14 at 01:55
  • Have you read [this](http://stackoverflow.com/questions/17950780/calling-python-script-from-another-script)? If you still can't get it to work, you likely have permissions set wrong somewhere. – Chase Ries Sep 30 '14 at 06:56

1 Answers1

0

I do not have a Mac, so I don't know if this will work, but try suid-python, which is a C program written by Steven Elliot. You have to compile it yourself, but the instructions are good. Afterwards you will have an executable that will run, and honor, Python suid scripts.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237