2022-12-16 10:58:42 +08:00
|
|
|
|
#!/bin/bash
|
2022-12-17 19:14:15 +08:00
|
|
|
|
#by spiritlhl
|
|
|
|
|
#from https://github.com/spiritLHLS/one-click-installation-script
|
2023-01-05 09:40:35 +08:00
|
|
|
|
#version: 2023.01.05
|
2022-12-16 10:58:42 +08:00
|
|
|
|
|
2023-04-27 20:50:46 +08:00
|
|
|
|
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8")
|
|
|
|
|
if [[ -z "$utf8_locale" ]]; then
|
|
|
|
|
echo "No UTF-8 locale found"
|
|
|
|
|
else
|
|
|
|
|
export LC_ALL="$utf8_locale"
|
|
|
|
|
export LANG="$utf8_locale"
|
|
|
|
|
export LANGUAGE="$utf8_locale"
|
|
|
|
|
echo "Locale set to $utf8_locale"
|
|
|
|
|
fi
|
|
|
|
|
|
2023-09-26 13:52:13 +08:00
|
|
|
|
red() { echo -e "\033[31m\033[01m$1$2\033[0m"; }
|
|
|
|
|
green() { echo -e "\033[32m\033[01m$1$2\033[0m"; }
|
|
|
|
|
yellow() { echo -e "\033[33m\033[01m$1$2\033[0m"; }
|
|
|
|
|
reading() { read -rp "$(green "$1")" "$2"; }
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
|
|
|
|
head() {
|
2022-12-17 22:26:08 +08:00
|
|
|
|
# 支持系统:Ubuntu 18+,Debian 8+,centos 7+,Fedora,Almalinux 8.5+
|
2023-01-05 09:40:35 +08:00
|
|
|
|
ver="2023.01.05"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
changeLog="一键修复linux网络脚本"
|
|
|
|
|
clear
|
|
|
|
|
echo "#######################################################################"
|
2022-12-17 22:26:08 +08:00
|
|
|
|
echo "# ${YELLOW}一键修复linux网络脚本${PLAIN} #"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
echo "# 版本:$ver #"
|
2022-12-17 22:26:08 +08:00
|
|
|
|
echo "# 更新日志:$changeLog #"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
echo "# ${GREEN}作者${PLAIN}: spiritlhl #"
|
2023-04-01 18:32:56 +08:00
|
|
|
|
echo "# ${GREEN}仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
echo "#######################################################################"
|
|
|
|
|
echo "支持系统:Ubuntu 18+,Debian 8+,centos 7+,Fedora,Almalinux 8.5+"
|
2023-01-05 09:40:16 +08:00
|
|
|
|
echo "1.检测ping谷歌和GitHub如果有问题修改nameserver为google源或cloudflare源"
|
|
|
|
|
echo "2.检测ping谷歌和Github还有问题尝试修复为IP类型对应的网络优先级(默认IPV4类型,纯V6类型再替换为IPV6类型)"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
# Display prompt asking whether to proceed with checking and changing
|
2022-12-18 11:53:20 +08:00
|
|
|
|
reading "Do you want to proceed with checking and changing nameserver? [y/n] " confirm
|
2022-12-17 22:17:58 +08:00
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
|
|
# Check user's input and exit if they do not want to proceed
|
|
|
|
|
if [ "$confirm" != "y" ]; then
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main() {
|
2022-12-18 19:27:21 +08:00
|
|
|
|
external_ip=$(host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}')
|
|
|
|
|
# 判断 IP 类型并执行对应的函数
|
|
|
|
|
if [[ $external_ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
|
2023-09-26 13:52:13 +08:00
|
|
|
|
main_v4
|
2022-12-18 19:27:21 +08:00
|
|
|
|
elif [[ $external_ip =~ ^[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}$ ]]; then
|
2023-09-26 13:52:13 +08:00
|
|
|
|
main_v6
|
2022-12-18 19:27:21 +08:00
|
|
|
|
else
|
2023-09-26 13:52:13 +08:00
|
|
|
|
echo "无法识别外网 IP 地址类型"
|
2022-12-18 19:27:21 +08:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main_v4() {
|
2022-12-18 19:14:00 +08:00
|
|
|
|
# Check if /etc/resolv.conf and /etc/gai.conf exist before backing them up
|
|
|
|
|
if [ -f /etc/resolv.conf ]; then
|
|
|
|
|
cp /etc/resolv.conf /etc/resolv.conf.bak
|
|
|
|
|
fi
|
|
|
|
|
if [ -f /etc/gai.conf ]; then
|
|
|
|
|
cp /etc/gai.conf /etc/gai.conf.bak
|
|
|
|
|
fi
|
2022-12-18 17:36:28 +08:00
|
|
|
|
|
2022-12-17 22:17:58 +08:00
|
|
|
|
# Check if ping to google.com is successful
|
2022-12-16 10:58:42 +08:00
|
|
|
|
if ping -c 1 google.com; then
|
2022-12-18 19:10:05 +08:00
|
|
|
|
return
|
|
|
|
|
fi
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
# Try using Google's nameserver
|
2023-09-26 13:52:13 +08:00
|
|
|
|
echo "nameserver 8.8.8.8" >/etc/resolv.conf
|
2022-12-18 19:10:05 +08:00
|
|
|
|
if ping -c 1 google.com; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
# Try using Cloudflare's nameserver
|
2023-09-26 13:52:13 +08:00
|
|
|
|
echo "nameserver 1.1.1.1" >/etc/resolv.conf
|
2022-12-18 19:10:05 +08:00
|
|
|
|
if ping -c 1 google.com; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
2023-09-26 13:52:13 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
# Display prompt asking whether to proceed with checking and changing priority
|
|
|
|
|
reading "Do you want to proceed with checking and changing network priority? [y/n] " priority
|
|
|
|
|
echo ""
|
2022-12-17 22:20:36 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
# Check user's input and exit if they do not want to proceed
|
|
|
|
|
if [ "$priority" != "y" ]; then
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
# Check IP type and network priority
|
|
|
|
|
ip_type=$(curl -s ip.sb | grep -oP '(?<=is )(.+)(?=\.)')
|
|
|
|
|
if [ -z "$ip_type" ]; then
|
|
|
|
|
echo "Error: curl request failed"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
if [ "$ip_type" = "IPv4" ]; then
|
|
|
|
|
priority=$(grep precedence /etc/gai.conf | grep -oP '(?<=precedence ::ffff:0:0\/96 )\d+')
|
|
|
|
|
else
|
|
|
|
|
priority=$(grep precedence /etc/gai.conf | grep -oP '(?<=precedence ::/0 )\d+')
|
|
|
|
|
fi
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
# Modify network priority if necessary
|
|
|
|
|
if [ "$ip_type" = "IPv4" ] && [ "$priority" -gt "100" ]; then
|
2023-09-26 13:52:13 +08:00
|
|
|
|
echo "precedence ::ffff:0:0/96 50" >/etc/gai.conf
|
2022-12-18 19:10:05 +08:00
|
|
|
|
elif [ "$ip_type" = "IPv6" ] && [ "$priority" -lt "100" ]; then
|
2023-09-26 13:52:13 +08:00
|
|
|
|
echo "precedence ::/0 100" >/etc/gai.conf
|
2022-12-18 19:10:05 +08:00
|
|
|
|
fi
|
2022-12-18 18:07:58 +08:00
|
|
|
|
|
2022-12-18 19:10:05 +08:00
|
|
|
|
# Check if ping to google.com is successful after modifying network priority
|
|
|
|
|
if ping -c 1 google.com; then
|
|
|
|
|
green "Ping successful after modifying network priority"
|
|
|
|
|
return
|
|
|
|
|
else
|
|
|
|
|
# Restore original configuration if ping fails after modifying network priority
|
|
|
|
|
mv /etc/resolv.conf.bak /etc/resolv.conf
|
|
|
|
|
mv /etc/gai.conf.bak /etc/gai.conf
|
|
|
|
|
echo "Error: Network problem is not related to nameserver or network priority. Original configuration restored."
|
|
|
|
|
exit 1
|
2022-12-16 10:58:42 +08:00
|
|
|
|
fi
|
2022-12-17 22:17:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 19:27:21 +08:00
|
|
|
|
main_v6() {
|
2023-09-26 13:52:13 +08:00
|
|
|
|
# 定义 nameserver 列表
|
|
|
|
|
nameservers=(
|
|
|
|
|
"2001:67c:2960:5353:5353:5353:5353:5353"
|
|
|
|
|
"2001:67c:2960:6464:6464:6464:6464:6464"
|
|
|
|
|
"2602:fc23:18::7"
|
|
|
|
|
"2001:67c:27e4::60"
|
|
|
|
|
"2001:67c:27e4:15::64"
|
|
|
|
|
"2001:67c:27e4::64"
|
|
|
|
|
"2001:67c:27e4:15::6411"
|
|
|
|
|
"2a01:4f9:c010:3f02::1"
|
|
|
|
|
"2a00:1098:2c::1"
|
|
|
|
|
"2a00:1098:2b::1"
|
|
|
|
|
"2a01:4f8:c2c:123f::1"
|
|
|
|
|
"2001:67c:2960::64"
|
|
|
|
|
"2001:67c:2960::6464"
|
|
|
|
|
"2001:67c:2960::64"
|
|
|
|
|
"2001:67c:2960::6464"
|
|
|
|
|
"2001:67c:2b0::6"
|
|
|
|
|
"2001:67c:2b0::4"
|
|
|
|
|
"2a03:7900:2:0:31:3:104:161"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 保存当前 nameserver 的值,以便之后恢复
|
|
|
|
|
current_nameserver=$(cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}')
|
|
|
|
|
|
|
|
|
|
# 循环尝试替换 nameserver 并测试网络
|
|
|
|
|
for nameserver in "${nameservers[@]}"; do
|
|
|
|
|
# 替换 nameserver
|
|
|
|
|
echo "nameserver $nameserver" >/etc/resolv.conf
|
|
|
|
|
|
|
|
|
|
# 让修改生效
|
2022-12-18 19:27:21 +08:00
|
|
|
|
resolvconf -u
|
|
|
|
|
|
2023-09-26 13:52:13 +08:00
|
|
|
|
# ping 测试
|
|
|
|
|
if ping -c 3 google.com &>/dev/null && ping -c 3 github.com &>/dev/null; then
|
|
|
|
|
green "网络恢复成功"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# 如果所有 nameserver 都尝试过了仍然无法修复,则恢复为原来的 nameserver
|
|
|
|
|
echo "nameserver $current_nameserver" >/etc/resolv.conf
|
|
|
|
|
resolvconf -u
|
|
|
|
|
}
|
2022-12-18 19:27:21 +08:00
|
|
|
|
|
2022-12-17 22:17:58 +08:00
|
|
|
|
head
|
|
|
|
|
main
|
2022-12-18 19:28:20 +08:00
|
|
|
|
# ping 测试
|
2023-09-26 13:52:13 +08:00
|
|
|
|
if ping -c 3 google.com &>/dev/null && ping -c 3 github.com &>/dev/null; then
|
|
|
|
|
green "V4网络恢复成功"
|
2022-12-18 19:28:20 +08:00
|
|
|
|
fi
|