forked from PaoloGit99/sherlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.py
83 lines (68 loc) · 1.73 KB
/
dns.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
from functions import get_default_interface
import time
import re
import subprocess
import requests
def change_dns():
iface, _ = get_default_interface()
dns_list = ["8.8.8.8",
"1.1.1.1",
"76.76.2.0",
"9.9.9.9",
"208.67.222.222",
"185.228.168.9",
"76.76.19.19",
"94.140.14.14",
"8.26.56.26",
"205.171.3.65",
"149.112.121.10",
"38.103.195.4",
"216.146.35.35",
"77.88.8.8",
"74.82.42.42",
"94.130.180.225",
"185.236.104.104",
"80.80.80.80"]
try:
for dns in dns_list:
print(f"Checking {dns} as DNS server...")
subprocess.run(['sudo', 'systemd-resolve', '--interface', iface, '--set-dns', dns])
time.sleep(5)
connectivity = check()
if connectivity:
print("DNS correctly set up")
return get_current_dns()
reset_network_manager()
return None
except subprocess.CalledProcessError as e:
reset_network_manager()
print(f"change_dns: {e}")
def check(url = "https://www.example.com/"):
try:
response = requests.get(url, timeout = 5)
if response.status_code == 200:
return True
return False
except:
return False
def reset_network_manager():
subprocess.run(['sudo', 'systemctl', 'restart', 'NetworkManager'])
time.sleep(5)
def get_current_dns():
try:
result = subprocess.run(['resolvectl', 'status'], capture_output=True, text=True)
return extract_current_dns(result.stdout)
except subprocess.CalledProcessError as e:
print(f"get_current_dns: {e}")
def extract_current_dns(string):
match = re.search(r'Current DNS Server:\s+([\d.]+)', string)
if match:
return match.group(1)
else:
return None
if __name__ == "__main__":
DNS = change_dns()
print(DNS)
print("---")
reset_network_manager()
print(get_current_dns())