![]() > /etc/hosts is blocking approximately 16147 domains Using the above (in run_command module): > ret = run_n_as_root("/usr/local/bin/pyscripts/TrackingBlocker.py", ) Return subprocess.call(' '.join(cmd), shell=True) If you want to simplify/modify it to run only certain commands as root, you could simply add this to your script (with the necessary imports noted above + import subprocess): def run_as_root(command, args, expand_path = True): To my hosts file (which is only writable by a superuser). Which then displays the authentification dialog needed to get the privileges to add: 127.0.0.1 ModifyTrackingBlocker add Įxecutes: /usr/local/bin/run-as-root-wrapper /usr/local/bin/pyscripts/TrackingBlocker.py add So if I have a script called TrackingBlocker.py (actual script I use to modify the /etc/hosts file to re-route known tracking domains to 127.0.0.1), when I call "sudo /usr/local/bin/pyscripts/TrackingBlocker.py -prefix /usr/local -name ModifyTrackingBlocker install" (arguments handled via argparse module), it installs "/usr/local/bin/ModifyTrackingBlocker", which is a bash script executing /usr/local/bin/run-as-root-wrapper /usr/local/bin/pyscripts/TrackingBlocker.py Į.g. ![]() Os.symlink(os.path.realpath(sys.argv), exe_install_path) If(not run_command.wrap_to_run_as_root(exe_install_path, sys.argv)): In my actual python script, I have the following function: def install_cmd(args):Įxe_install_path = os.path.join(args.prefix, True_command = os.path.normpath(true_command)į.write(run_as_root_path + " " + true_command + " 0755) True_command = os.path.abspath(true_command) True_command = os.path.realpath(true_command) Run_as_root_path = find_executable("run-as-root-wrapper") #=#ĭef wrap_to_run_as_root(exe_install_path, true_command, expand_path = True): Then I have the following run_command.py module: import osįrom distutils.spawn import find_executable usr/local/bin) so python doesn't need anything special to locate it. I name the script "run-as-root-wrapper" and place it in a very common path (e.g. (2) I can store the scripts with their extension (helpful for syntax highlighting in IDEs), but the executables do not contain them because I like it that way (e.g. (1) If I have different versions, I can easily switch which one is executed by changing the symbolic link and it keeps the bin directory cleaner (e.g. usr/local/bin/pyscripts in /usr/local/bin), and create symbolic links to the executables. $SUDO the way I set up my system is that I keep subfolders inside the bin directories (e.g. $SUDO -e "do shell script \"$*\" with administrator privileges" ![]() If thenĮlif available xmessage notify-send then If ( available gksudo & available kdesudo ) # if both are avilable check whoch desktop is running MESSAGE="Please run $1 as root with sudo or install gksu / kdesudo!" MESSAGE="Please run $1 as root with sudo or install osascript (should be installed by default)" I wrote the following bash-script to execute bash/python scripts with administrator privileges (works on Linux and OS X systems): #!/bin/bash ![]() I know the post is a little old, but I wrote the following as a solution to my problem (running a python script as root on both Linux and OS X).
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |