-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-hosts-local.py
66 lines (49 loc) · 1.86 KB
/
update-hosts-local.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
60
61
62
63
64
65
66
import os
import socket
import platform
# 这里要更新局域网hosts的主机名称 key对应的是你目标主机名称
# key对应的是映射后的url(保持唯一性) value 对应的是节点名称(也就是服务名称)
url_node_dict = {'nas.com':'YGC-NAS',
'mws.com':'THINK-YANG',
'mwsvm0.com': 'mwsvm0'}
node_name_list = url_node_dict.values()
# windows 的默认hosts路径
HOSTS_PATH = "C:/Windows/System32/drivers/etc/hosts"
flushdns = "ipconfig /flushdns"
node_nameip_dict = {}
def platformAdapter():
if ("Linux" == platform.system()):
hostsPath ="/etc/hosts"
# CentOS需要提前安装 nscd
flushdns = "nscd -i hosts"
def findSvrIpByName():
for node_name in node_name_list:
try:
node_ip = socket.gethostbyname(node_name)
print("host: " + node_name + " IP: " + node_ip)
node_nameip_dict[node_name] = node_ip
except:
print("node: " + node_name + " IP not found")
def udpateHostInfo():
if not bool(node_nameip_dict):
print('node_nameip_dict is null')
os.system.exit()
updatedHostIp = set()
newHostContents = ""
with open(r''+ HOSTS_PATH, 'r', encoding='UTF-8') as filerReader:
lines = filerReader.readlines()
for currLine in lines:
hosts_old_ip_url = currLine.split(" ")
old_url = hosts_old_ip_url[1].strip('\n')
if (old_url in url_node_dict.keys()):
currLine = currLine.replace(currLine, node_nameip_dict[url_node_dict[old_url]]+" "+ old_url + "\n")
newHostContents += currLine
with open(r''+ HOSTS_PATH, 'w', encoding='UTF-8') as fileWriter:
fileWriter.write(newHostContents)
# 打印文本已替换
platformAdapter()
findSvrIpByName()
udpateHostInfo()
os.system(flushdns)
print("hosts is update")
os.system('pause')