-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetchDNS.py
41 lines (22 loc) · 979 Bytes
/
fetchDNS.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
#!/usr/bin/env python3
# Usage: su root and place it in /root/fetchDNS and chmod a+x
# crontab: 0 3 * * 0 /root/fetchDNS
import os
import urllib.request
data = urllib.request.urlopen("https://gitee.com/ineo6/hosts/raw/master/hosts")
hosts = [line.decode('ascii').strip() for line in data]
hosts = [h for h in hosts if len(h) > 0 and not h.startswith("#")]
ipAndAddrs = [h.strip().split() for h in hosts]
ips = [h[0].strip() for h in ipAndAddrs]
addresses = [h[1].strip() for h in ipAndAddrs]
with open("/etc/hosts", "r") as fp:
records = fp.readlines()
records = [r for r in records if not (r.startswith("# Github") or r.isspace())]
records = [r for r in records if all(addr not in r for addr in addresses)]
records.append("# Github Hosts Start" + os.linesep)
records.extend(hosts)
records.append("# Github Hosts End" + os.linesep)
result = os.linesep.join(records)
with open("/etc/hosts", "w") as fp:
fp.write(result)
print("Set hosts finished.")