Skip to content

Commit

Permalink
Add Update Local Hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaomeng2 committed May 14, 2020
1 parent 7ccaf1c commit 5cb7939
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,27 @@ def UpdateDNS(access_key_id, access_key_secret, host, old_host_ip, new_load_bala
subdomain_record_id = DescribeDomainRecords(access_key_id, access_key_secret, domain_name, subdomain_name, old_host_ip)
# print (subdomain_record_id)
UpdateDomainRecord(access_key_id, access_key_secret, subdomain_record_id, subdomain_name, new_load_balancer_ip)
print("DNS update record successful domain:%s, old ip:%s, new ip:%s " % (host, old_host_ip, new_load_balancer_ip))
print("DNS update record successful domain:%s, old ip:%s, new ip:%s " % (host, old_host_ip, new_load_balancer_ip))

def UpdateHosts(new_load_balancer_ip, host):
"""
更新本地Hosts, 防止DNS更新不及时导致一直检测旧IP
:param new_load_balancer_ip:
:param host:
:return:
"""

with open("/etc/hosts", "r", encoding="utf-8") as f:
lines = f.readlines()
f.close()
# print(lines)

with open("/etc/hosts", "w", encoding="utf-8") as f_w:
for line in lines:
if host in line:
f_w.write("%s %s\n" % (new_load_balancer_ip, host))
print("Hosts update record successful domain:%s, new ip:%s " % (host, new_load_balancer_ip))
continue

f_w.write(line)
f_w.close()
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
# 去DNS里更新A记录
dns.UpdateDNS(access_key_id, access_key_secret, host, host_ip, new_load_balancer_ip)

# 更新本地Hosts, 防止DNS更新不及时导致一直检测旧IP
dns.UpdateHosts(new_load_balancer_ip, host)

else:
print("New LoadBalancer:%s Port:%s is not open,return code:%s" % (new_load_balancer_ip, port, result))

Expand Down

0 comments on commit 5cb7939

Please sign in to comment.