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
|
2022-12-18 11:53:20 +08:00
|
|
|
|
#version: 2022.12.18
|
2022-12-16 10:58:42 +08:00
|
|
|
|
|
2022-12-18 11:53:20 +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+
|
2022-12-18 11:53:20 +08:00
|
|
|
|
ver="2022.12.18"
|
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 #"
|
|
|
|
|
echo "# ${GREEN}作仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
|
|
|
|
|
echo "#######################################################################"
|
|
|
|
|
echo "支持系统:Ubuntu 18+,Debian 8+,centos 7+,Fedora,Almalinux 8.5+"
|
2022-12-18 12:25:14 +08:00
|
|
|
|
echo "1.检测ping谷歌如果有问题修改nameserver为google源或cloudflare源"
|
2022-12-17 22:26:08 +08:00
|
|
|
|
echo "2.检测ping谷歌还有问题尝试修复为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() {
|
|
|
|
|
# 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 12:25:14 +08:00
|
|
|
|
green "Ping successful, no need modify"
|
2022-12-16 10:58:42 +08:00
|
|
|
|
else
|
2022-12-18 11:53:20 +08:00
|
|
|
|
yellow "Ping failed. Checking nameserver."
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
|
|
|
|
# Check current nameserver
|
|
|
|
|
nameserver=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
|
2022-12-18 11:53:20 +08:00
|
|
|
|
yellow "Current nameserver: $nameserver"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
|
|
|
|
# Try using Google's nameserver
|
2022-12-18 11:53:20 +08:00
|
|
|
|
green "Trying Google's nameserver: 8.8.8.8"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
sudo sed -i "s/$nameserver/8.8.8.8/g" /etc/resolv.conf
|
2022-12-16 10:58:42 +08:00
|
|
|
|
if ping -c 1 google.com; then
|
2022-12-18 11:53:20 +08:00
|
|
|
|
green "Ping successful with Google's nameserver"
|
2022-12-16 10:58:42 +08:00
|
|
|
|
else
|
2022-12-18 11:53:20 +08:00
|
|
|
|
yellow "Ping failed with Google's nameserver. Trying Cloudflare's nameserver."
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
|
|
|
|
# Try using Cloudflare's nameserver
|
2022-12-18 11:53:20 +08:00
|
|
|
|
green "Trying Cloudflare's nameserver: 1.1.1.1"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
sudo sed -i "s/8.8.8.8/1.1.1.1/g" /etc/resolv.conf
|
2022-12-16 11:01:56 +08:00
|
|
|
|
if ping -c 1 google.com; then
|
2022-12-18 11:53:20 +08:00
|
|
|
|
green "Ping successful with Cloudflare's nameserver"
|
2022-12-16 11:01:56 +08:00
|
|
|
|
else
|
2022-12-18 11:53:20 +08:00
|
|
|
|
yellow "Ping failed with Cloudflare's nameserver. Checking network configuration."
|
2022-12-17 22:20:36 +08:00
|
|
|
|
|
|
|
|
|
# Display prompt asking whether to proceed with checking and changing priority
|
2022-12-18 11:53:20 +08:00
|
|
|
|
reading "Do you want to proceed with checking and changing network priority? [y/n] " priority
|
2022-12-17 22:20:36 +08:00
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
# Check IP type and network priority
|
|
|
|
|
ip_type=$(curl -s ip.sb | grep -oP '(?<=is )(.+)(?=\.)')
|
2022-12-18 11:53:20 +08:00
|
|
|
|
green "IP type: $ip_type"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
if [ "$ip_type" = "IPv4" ]; then
|
|
|
|
|
priority=$(cat /etc/gai.conf | grep -oP '(?<=precedence ::ffff:0:0\/96 )\d+')
|
|
|
|
|
else
|
|
|
|
|
priority=$(cat /etc/gai.conf | grep -oP '(?<=precedence ::/0 )\d+')
|
|
|
|
|
fi
|
2022-12-18 11:53:20 +08:00
|
|
|
|
green "Network priority: $priority"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
|
|
|
|
|
# Modify network priority if necessary
|
|
|
|
|
if [ "$ip_type" = "IPv4" ] && [ "$priority" -gt "100" ]; then
|
|
|
|
|
sudo sed -i 's/precedence ::ffff:0:0\/96 100/precedence ::ffff:0:0\/96 50/' /etc/gai.conf
|
|
|
|
|
elif [ "$ip_type" = "IPv6" ] && [ "$priority" -lt "100" ]; then
|
|
|
|
|
sudo sed -i 's/precedence ::\/0 50/precedence ::\/0 100/' /etc/gai.conf
|
|
|
|
|
else
|
2022-12-18 11:53:20 +08:00
|
|
|
|
red "Network configuration is correct."
|
2022-12-17 22:17:58 +08:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Try to ping again after modifying network priority
|
|
|
|
|
if ping -c 1 google.com; then
|
2022-12-18 11:53:20 +08:00
|
|
|
|
green "Ping successful after modifying network priority"
|
2022-12-17 22:17:58 +08:00
|
|
|
|
else
|
2022-12-18 11:53:20 +08:00
|
|
|
|
red "Network problem is not related to nameserver or network priority."
|
2022-12-17 22:17:58 +08:00
|
|
|
|
fi
|
2022-12-16 11:01:56 +08:00
|
|
|
|
fi
|
2022-12-16 10:58:42 +08:00
|
|
|
|
fi
|
|
|
|
|
fi
|
2022-12-17 22:17:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
head
|
|
|
|
|
main
|