Modify Windows Hosts File

Please use the script to modify the Hosts file on your windows machine which helps you to update the website list that you want to block surfing on your endpoint.

You can change the variable ‘content’ as per your requirement.

If you want to block www.facebook.com and www.msdn.com then you can change the variable values as follows
content=r’‘’1.1.1.1 www.facebook.com
1.1.1.2 www.msdn.com‘’'

Note:
your list of IP address and website domain should be as r’‘’IP1<space>Domain1<enter>IP2<space>Domain2‘’'

content=r'''1.1.1.1 www.facebook.com
2.2.2.2 www.skype.com'''

print 'Before Change: '
with open('C:\Windows\System32\drivers\etc\hosts') as r:
    print r.read()

with open('C:\Windows\System32\drivers\etc\hosts', 'a') as f:
    f.write('
')
    f.write(content)
print '


'
print 'After Change: '
with open('C:\Windows\System32\drivers\etc\hosts') as r:
    print r.read()

The script in JSON format:

Sample Output:

20170419-Modify-Existing-Hosts-File.json (775 Bytes)