-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_status-domain2.py
104 lines (88 loc) · 3.34 KB
/
check_status-domain2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests
import json
import subprocess
from ping3 import ping
# Cấu hình API Cloudflare
CF_API_TOKEN = 'aR3wIq39PRpmNjgQ2OF9S1j_WzK5J3-lpZYZH4Uo'
CF_ZONE_ID = '7c559a7cb5be75c630994222fe114a22'
CF_RECORD_NAME = 'b.dautay.xyz'
# Hàm lấy IP hiện tại từ lệnh curl ifconfig.me
def get_current_ip():
result = subprocess.run(['curl', 'ifconfig.me'], stdout=subprocess.PIPE)
return result.stdout.decode('utf-8').strip()
# Hàm kiểm tra xem IP có ping được không
def check_ping(ip):
response = ping(ip, timeout=2) # Thời gian chờ 2 giây
return response is not None
# Hàm lấy các bản ghi A hiện có
def get_a_records():
url = f"https://api.cloudflare.com/client/v4/zones/{CF_ZONE_ID}/dns_records"
headers = {
"Authorization": f"Bearer {CF_API_TOKEN}",
"Content-Type": "application/json"
}
params = {
"type": "A",
"name": CF_RECORD_NAME
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
return response.json().get("result", [])
else:
print(f"Error fetching DNS records: {response.status_code}")
print(response.text)
return []
# Hàm thêm một bản ghi DNS mới
def add_a_record(ip):
url = f"https://api.cloudflare.com/client/v4/zones/{CF_ZONE_ID}/dns_records"
headers = {
"Authorization": f"Bearer {CF_API_TOKEN}",
"Content-Type": "application/json"
}
data = {
"type": "A",
"name": CF_RECORD_NAME,
"content": ip,
"ttl": 60, # Thời gian TTL (có thể chỉnh)
"proxied": False # Nếu muốn bật proxy Cloudflare, để True
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
print(f"Đã thêm IP mới: {ip}")
else:
print(f"Error adding new IP: {response.status_code}")
print(response.text)
# Hàm xóa bản ghi DNS không còn hoạt động
def delete_record(record_id):
url = f"https://api.cloudflare.com/client/v4/zones/{CF_ZONE_ID}/dns_records/{record_id}"
headers = {
"Authorization": f"Bearer {CF_API_TOKEN}",
"Content-Type": "application/json"
}
response = requests.delete(url, headers=headers)
if response.status_code == 200:
print(f"Đã xóa bản ghi với ID: {record_id}")
else:
print(f"Lỗi khi xóa bản ghi: {response.status_code} - {response.text}")
def main():
current_ip = get_current_ip() # Lấy IP hiện tại từ curl ifconfig.me
print(f"IP hiện tại: {current_ip}")
a_records = get_a_records()
# Kiểm tra xem IP mới đã tồn tại hay chưa
existing_ips = [record['content'] for record in a_records]
if current_ip in existing_ips:
print(f"IP {current_ip} đã tồn tại.")
else:
add_a_record(current_ip)
# Kiểm tra ping các IP hiện có và xóa các bản ghi không hoạt động
for record in a_records:
ip = record['content']
record_id = record['id']
print(f"Đang kiểm tra IP: {ip}")
if check_ping(ip):
print(f"IP {ip} ping được, giữ nguyên.")
else:
print(f"IP {ip} không ping được, xóa bản ghi.")
delete_record(record_id)
if __name__ == "__main__":
main()