-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnser.sh
executable file
·259 lines (233 loc) · 7.78 KB
/
dnser.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
# Colors
GREEN="\e[32m"
CYAN="\e[36m"
YELLOW="\e[33m"
RED="\e[31m"
RESET="\e[0m"
# ASCII Art
ART=$(cat <<'EOF'
_
| |
__| |_ __ ___ ___ _ __
/ _` | '_ \/ __|/ _ \ '__|
| (_| | | | \__ \ __/ |
\__,_|_| |_|___/\___|_|
EOF
)
# Function to display the ASCII Art before every task
display_art() {
echo -e "${CYAN}$ART${RESET}"
}
show_welcome() {
display_art
echo -e "${CYAN}**************************************"
echo -e "* *"
echo -e "* ${GREEN}Welcome to the DNS Changer${CYAN} *"
echo -e "* *"
echo -e "**************************************"
echo -e "${YELLOW}Your DNS will be updated beautifully!${RESET}"
echo -e "${CYAN}This tool allows you to change your system's DNS quickly and easily!${RESET}"
}
# DNS Presets
SHECAN_PRO1="178.22.122.101"
SHECAN_PRO2="185.51.200.1"
BOGHZAR1="185.55.226.26"
BOGHZAR2="185.55.225.25"
ELECTRO1="78.157.42.101"
ELECTRO2="78.157.42.100"
HOSTIRAN1="172.29.0.100"
HOSTIRAN2="172.29.2.100"
SHATEL1="85.15.1.14"
SHATEL2="85.15.1.15"
RADAR1="10.202.10.10"
RADAR2="10.202.10.11"
FOUR_OH_THREE1="10.202.10.202"
FOUR_OH_THREE2="10.202.10.102"
SHECAN1="178.22.122.100"
SHECAN2="185.51.200.2"
GOOGLE1="8.8.8.8"
GOOGLE2="8.8.4.4"
CLOUDFLARE1="1.1.1.1"
CLOUDFLARE2="1.0.0.1"
# Ensure script is run as root
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}This script must be run as root!${RESET}"
exit 1
fi
# Detect DNS management system
detect_dns_manager() {
if command -v nmcli &>/dev/null; then
echo "networkmanager"
elif systemctl is-active --quiet systemd-resolved; then
echo "systemd-resolved"
elif [ -f /etc/resolv.conf ]; then
echo "resolv.conf"
else
echo "unknown"
fi
}
# Fully clear previous DNS settings
clear_previous_dns() {
display_art
echo -e "${YELLOW}Clearing previous DNS settings...${RESET}"
# 1️⃣ Clear NetworkManager DNS settings
if command -v nmcli &>/dev/null; then
active_connections=$(nmcli -t -f NAME con show --active)
for connection in $active_connections; do
nmcli connection modify "$connection" ipv4.dns ""
nmcli connection modify "$connection" ipv4.ignore-auto-dns no
nmcli connection up "$connection"
done
fi
# 2️⃣ Remove Global DNS from systemd-resolved
if [ -f /etc/systemd/resolved.conf ]; then
echo -e "${YELLOW}Removing global DNS from systemd-resolved...${RESET}"
sed -i '/^DNS=/d' /etc/systemd/resolved.conf # Remove existing DNS entries
sed -i '/^FallbackDNS=/d' /etc/systemd/resolved.conf
systemctl restart systemd-resolved # Restart service
fi
# 3️⃣ Reset per-interface DNS settings
if command -v resolvectl &>/dev/null; then
for iface in $(resolvectl status | grep "Link" | awk '{print $2}'); do
resolvectl dns "$iface" "" # Clear DNS for each interface
done
resolvectl flush-caches # Flush cache
fi
# 4️⃣ Reset /etc/resolv.conf
if [ -L /etc/resolv.conf ]; then
rm -f /etc/resolv.conf
fi
touch /etc/resolv.conf
echo "nameserver 127.0.0.53" >/etc/resolv.conf # Use systemd stub resolver
echo -e "${GREEN}DNS settings cleared successfully!${RESET}"
}
# Change DNS to new settings
change_dns() {
primary_dns="$1"
secondary_dns="$2"
echo -e "${YELLOW}Setting new DNS: ${GREEN}$primary_dns, $secondary_dns${RESET}"
# 1️⃣ Detect DNS Manager
manager=$(detect_dns_manager)
# 2️⃣ Apply DNS for NetworkManager
if command -v nmcli &>/dev/null; then
active_connections=$(nmcli -t -f NAME con show --active)
for connection in $active_connections; do
nmcli connection modify "$connection" ipv4.dns "$primary_dns $secondary_dns"
nmcli connection modify "$connection" ipv4.ignore-auto-dns yes
nmcli connection up "$connection"
done
fi
# 3️⃣ Apply Global DNS in systemd-resolved
if [ -f /etc/systemd/resolved.conf ]; then
echo -e "${YELLOW}Updating /etc/systemd/resolved.conf...${RESET}"
sed -i '/^DNS=/d' /etc/systemd/resolved.conf
sed -i '/^FallbackDNS=/d' /etc/systemd/resolved.conf
echo "DNS=$primary_dns $secondary_dns" >>/etc/systemd/resolved.conf
systemctl restart systemd-resolved
fi
# 4️⃣ Apply DNS per interface in systemd-resolved
if command -v resolvectl &>/dev/null; then
for iface in $(resolvectl status | grep "Link" | awk '{print $2}'); do
resolvectl dns "$iface" "$primary_dns" "$secondary_dns"
done
resolvectl flush-caches
fi
# 5️⃣ Update /etc/resolv.conf
if [ -L /etc/resolv.conf ]; then
rm -f /etc/resolv.conf
fi
echo -e "nameserver $primary_dns\nnameserver $secondary_dns" >/etc/resolv.conf
echo -e "${GREEN}DNS changed successfully to $primary_dns, $secondary_dns!${RESET}"
}
# Show current DNS settings
show_dns_status() {
display_art
echo -e "${CYAN}Current DNS Settings:${RESET}"
resolvectl status | grep -E "DNS Servers|Link"
cat /etc/resolv.conf
}
# CLI menu for setting DNS
show_menu() {
display_art
echo -e "${CYAN}Select a DNS provider:${RESET}"
echo "0) Shecan pro (🇮🇷 178.22.122.101, 185.51.200.1)"
echo "1) Shecan (🇮🇷 178.22.122.100, 185.51.200.2)"
echo "2) 403 (🇮🇷 10.202.10.202, 10.202.10.102)"
echo "3) Google (🌍 8.8.8.8, 8.8.4.4)"
echo "4) Cloudflare (🌍 1.1.1.1, 1.0.0.1)"
echo "5) Boghzar (🌍 185.55.226.26, 185.55.225.25)"
echo "6) Electro (🌍 78.157.42.101, 78.157.42.100)"
echo "7) HostIran (🌍 172.29.0.100, 172.29.2.100)"
echo "8) Shatel (🌍 85.15.1.14, 85.15.1.15)"
echo "9) Radar (🌍 10.202.10.10, 10.202.10.11)"
echo "10) Enter custom DNS"
echo "11) Exit"
read -p "Enter your choice [0-11]: " choice
case "$choice" in
0) change_dns "$SHECAN_PRO1" "$SHECAN_PRO2" ;;
1) change_dns "$SHECAN1" "$SHECAN2" ;;
2) change_dns "$FOUR_OH_THREE1" "$FOUR_OH_THREE2" ;; # Added 403 DNS
3) change_dns "$GOOGLE1" "$GOOGLE2" ;;
4) change_dns "$CLOUDFLARE1" "$CLOUDFLARE2" ;;
5) change_dns "$BOGHZAR1" "$BOGHZAR2" ;;
6) change_dns "$ELECTRO1" "$ELECTRO2" ;;
7) change_dns "$HOSTIRAN1" "$HOSTIRAN2" ;;
8) change_dns "$SHATEL1" "$SHATEL2" ;;
9) change_dns "$RADAR1" "$RADAR2" ;;
10)
read -p "Enter primary DNS: " custom_dns1
read -p "Enter secondary DNS: " custom_dns2
change_dns "$custom_dns1" "$custom_dns2"
;;
11)
echo -e "${RED}Exiting...${RESET}"
exit 0
;;
*)
echo -e "${RED}Invalid option!${RESET}"
exit 1
;;
esac
}
# Command-line arguments
case "$1" in
help)
display_art
echo -e "${CYAN}Available options:${RESET}"
echo "help - Show this help message"
echo "set - Set DNS using a selected provider"
echo "clear --cache - Clear DNS cache"
echo "clear --dns - Clear previous DNS settings"
echo "status - Show current DNS settings"
;;
dns)
case "$2" in
set)
show_menu
;;
status)
show_dns_status
;;
clear)
if [ "$3" == "--cache" ]; then
resolvectl flush-caches
echo -e "${GREEN}DNS cache cleared!${RESET}"
elif [ "$3" == "--dns" ]; then
clear_previous_dns
else
echo -e "${RED}Invalid option for clear. Use --cache or --dns.${RESET}"
exit 1
fi
;;
*)
echo -e "${RED}Invalid option!${RESET}"
exit 1
;;
esac
;;
*)
echo -e "${RED}Usage: $0 dns [help | set | status | clear --cache | clear --dns]${RESET}"
exit 1
;;
esac