This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpatch_hosts.py
59 lines (50 loc) · 1.69 KB
/
patch_hosts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# patch and restore hosts for resilio sync config
import os
if os.name == 'nt': import ctypes
def get_host_filename():
if os.name == 'nt':
return "C:\\Windows\\System32\\drivers\\etc\\hosts"
elif os.name == 'posix':
return "/etc/hosts"
else:
raise RuntimeError, "Unsupported operating system for this module: %s" % (os.name,)
def add_hosts_entry(ipaddress, hostname):
filename = get_host_filename()
outputfile = open(filename, 'a')
entry = "\n" + ipaddress + "\t" + hostname + "\n"
outputfile.writelines(entry)
outputfile.close()
def del_hosts_entry(hostname):
filename = get_host_filename()
lines = open(filename, 'r').readlines()
outputfile = open(filename, 'w')
for line in lines:
stripped = line.split("#", 1)[0]
if hostname in stripped: continue
outputfile.write(line)
outputfile.close()
def is_admin():
if os.name == 'nt':
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
traceback.print_exc()
print "Admin check failed, assuming not an admin."
return False
elif os.name == 'posix':
# Check for root on Posix
return os.getuid() == 0
else:
raise RuntimeError, "Unsupported operating system for this module: %s" % (os.name,)
def sync_patch_hosts():
if not is_admin():
print "Need admin previledge. Please run as admin."
return False
add_hosts_entry("127.0.0.1", "config.resilio.com")
return True
def sync_unpatch_hosts():
if not is_admin():
print "Need admin previledge. Please run as admin."
return False
del_hosts_entry("config.resilio.com")
return True