4

I have a Python app that I'm working on that needs to access the hosts file to append a few lines. Everything worked on my test file, but when I told the program to actually modify my hosts file in /etc/hosts I get IOError 13. From what I understand, my app doesn't have root privileges.

My question is, how can I circumnavigate this issue? Is there a way to prompt the user for their password? Would the process be any different if I was running the app on a Windows machine?

Here's the code in question:

f = open("/etc/hosts", "a")
f.write("Hello Hosts File!")

Also, I plan on using py2app and py2exe for the final product. Would they handle the root privilege issue for me?

Parker
  • 7,943
  • 7
  • 60
  • 91

2 Answers2

6

If you are on the sudoers list, you can start your progamm with sudo:

 sudo python append_to_host.py

sudo runs your python interpreter with root privileges. The first time you do it, you will be asked for your password, later calls will not ask you if your last sudo call is not to long ago.

Being on the sudoers list (in most cases /etc/sudoers) says that the admin trusts you. If you call sudo you are not asked for the root password, but yours. You have to prove that the right user sits at the terminal.

More about sudo on http://en.wikipedia.org/wiki/Sudo

If you want to remote controll this you can use the -S command line switch or use http://www.noah.org/wiki/pexpect

rocksportrocker
  • 6,639
  • 2
  • 28
  • 46
  • Thanks, but ideally I'd like to avoid having to require the user to run the script as sudo. Is there a way to do it for them in the background if I have their password? – Parker Sep 23 '11 at 03:14
  • I did some more research on 'sudo -S' and it seems like that's what I can use to pass the user supplied password onto the system. Thanks! – Parker Sep 23 '11 at 06:50
6

The easiest way to handle this is to write out your changes to a temp file, then run a program to overwrite the protected file. Like so:

with open('/etc/hosts', 'rt') as f:
    s = f.read() + '\n' + '127.0.0.1\t\t\thome_sweet_home\n'
    with open('/tmp/etc_hosts.tmp', 'wt') as outf:
        outf.write(s)

os.system('sudo mv /tmp/etc_hosts.tmp /etc/hosts')

When your Python program runs sudo, the sudo program will prompt the user for his/her password. If you want this to be GUI based you can run a GUI sudo, such as "gksu".

On Windows, the hosts file is buried a couple subdirectories under \Windows. You can use the same general trick, but Windows doesn't have the sudo command. Here is a discussion of equivalents:

https://superuser.com/questions/42537/is-there-any-sudo-command-for-windows

Community
  • 1
  • 1
steveha
  • 67,444
  • 18
  • 86
  • 112
  • Thanks, this is close to what I need. The script is graphical though, so is there any way to pass a string to the system for authentication? I'd like to avoid having the user type into a terminal – Parker Sep 23 '11 at 03:12
  • 1
    If you run gksu as I suggested, then it will prompt the user in a GUI dialog box. If you run plain "sudo" then that would prompt in the terminal. – steveha Sep 23 '11 at 06:32
  • Sorry, I didn't catch that for some reason. But, I don't have gksu, do you know what something similar for QT might be? – Parker Sep 23 '11 at 06:47
  • 1
    I don't, but I just ran a Google search for "QT equivalent to sudo" and I found this Wikipedia page: http://en.wikipedia.org/wiki/Comparison_of_privilege_authorization_features Looks like "kdesu" or "kdesudo" might work for you. – steveha Sep 23 '11 at 07:04