-3

I want to map 172.16.14.54 to myblog.dexter.in in hosts file through python code. But editing hosts file requires administrative privileges.

So, how to edit hosts file without administrative privileges in python on windows/mac.

Please note: User need not give any password/username in code or dialog box. It should be done though code only.

imp
  • 1,575
  • 1
  • 24
  • 38
  • [visit](http://playingwithpython.com/updating-host-file-with-python/) this link it may help – Engineer Jun 17 '14 at 09:56
  • 1
    Unfortunately the article to which you link points out that privileges are required for the technique to work. – holdenweb Jun 17 '14 at 10:39

3 Answers3

0

Privileges are required to edit the host file. Your account doesn't have them, so you ask how they can be made irrelevant. They can't - the permissions settings on the hosts file is there to explicitly prevent unauthorized programs from making changes that would potentially affect all users (such as, for example, associating the name google.com with the IP address for Bing service). Bottom line: there's no way to achieve what you want without privileges.

holdenweb
  • 24,217
  • 7
  • 45
  • 67
  • sine user has logged into the system, so is there way through python, to get user details and edit hosts file without asking for admin privileges – imp Jun 17 '14 at 10:59
  • 1
    I am not sure whether this is a question or a comment. On the assumption that it is the latter, there is no reason why a Python program should not READ the hosts file, but unless run by a privileged user there is no way it will be allowed to WRITE the hosts file. You are trying to circumvent standard protection mechanisms. You can't (without exploiting some sort of vulnerability). Please don't ask how to exploit a vulnerability - that's rather out of scope for this question, and might get you dismissed from your job! – holdenweb Jun 17 '14 at 16:17
0

You can run your python script as a superuser. Or you can add yourself to super user group and then run the script, which will not ask for credentials. I feel this is a system administration or access related question and not specific to python.

Vreddhi Bhat
  • 113
  • 6
0

Just as others said, you need to have admin privileges to operate the hosts file.

I have create a tool with python to manage the hosts file. It might will save you some time on this. This is the repo: hostsman , run pip install hostsman to install it.

Qingshan
  • 350
  • 3
  • 5