Snake's Home

changehost

最近写了一个自动修改host文件的代码:

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
#encoding=utf-8
import os
import re
import time
import platform

host_window = r"C:\Windows\System32\drivers\etc\hosts"
host_mac = r"/etc/hosts"
B2B_UAT_HOST = ["#B2B uat deepblue2",
'10.128.34.183 uat.englishtown.com',
'10.128.34.183 uat-cache.englishtown.com',
'10.128.34.233 uatdeepblue2.englishtown.com',
'10.128.34.233 uatdeepblue2cn.englishtown.com']

HOST_LIVE = ["#LIVE",
"104.20.43.137 englishlive.ef.com",
"104.20.43.137 www.englishtown.com",
"104.20.43.137 www.englishtown.com.br",
"104.20.43.137 services.englishtown.com",
"104.20.43.137 etvt.englishtown.com",
"104.20.43.137 axis.englishtown.com",
"104.20.43.137 secure.englishtown.com",
"104.20.43.137 accenture.englishtown.com",
"104.20.43.137 accounts.ef.com"]

def search_host(hostvalue,host_path):
hostfile = open(host_path,'r')
each_line = hostfile.readlines()
hostfile.close()
findresult = re.findall(hostvalue,''.join(each_line))
return findresult

def write_host(hostvalue,host_path):

output = open(host_path, 'a')

for insid in hostvalue:
print insid
output.write(insid)
output.write("\n")
output.close()

if __name__ == "__main__":
#inside_test()
if platform.system is "Windows":
host_path = host_window
else:
host_path = host_mac
os.popen("sudo chmod 777 %s" %host_path)

print host_path
if search_host(HOST_LIVE[0],host_path):
print ("Already exist")
pass
else:
write_host(HOST_LIVE,host_path)