Ai
1 Star 0 Fork 0

stan/one-click-installation-script

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
network.sh 4.14 KB
一键复制 编辑 原始数据 按行查看 历史
spiritLHLS 提交于 2022-12-18 11:53 +08:00 . Update network.sh
#!/bin/bash
#by spiritlhl
#from https://github.com/spiritLHLS/one-click-installation-script
#version: 2022.12.18
GREEN="\033[32m"
PLAIN="\033[0m"
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"; }
head() {
# 支持系统:Ubuntu 18+,Debian 8+,centos 7+,Fedora,Almalinux 8.5+
ver="2022.12.18"
changeLog="一键修复linux网络脚本"
clear
echo "#######################################################################"
echo "# ${YELLOW}一键修复linux网络脚本${PLAIN} #"
echo "# 版本:$ver #"
echo "# 更新日志:$changeLog #"
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+"
echo "1.检测ping谷歌有问题修改nameserver为google源或cloudflare源"
echo "2.检测ping谷歌还有问题尝试修复为IP类型对应的网络优先级(默认IPV4类型,纯V6类型再替换为IPV6类型)"
# Display prompt asking whether to proceed with checking and changing
reading "Do you want to proceed with checking and changing nameserver? [y/n] " confirm
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
if ping -c 1 google.com; then
green "Ping successful"
else
yellow "Ping failed. Checking nameserver."
# Check current nameserver
nameserver=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
yellow "Current nameserver: $nameserver"
# Try using Google's nameserver
green "Trying Google's nameserver: 8.8.8.8"
sudo sed -i "s/$nameserver/8.8.8.8/g" /etc/resolv.conf
if ping -c 1 google.com; then
green "Ping successful with Google's nameserver"
else
yellow "Ping failed with Google's nameserver. Trying Cloudflare's nameserver."
# Try using Cloudflare's nameserver
green "Trying Cloudflare's nameserver: 1.1.1.1"
sudo sed -i "s/8.8.8.8/1.1.1.1/g" /etc/resolv.conf
if ping -c 1 google.com; then
green "Ping successful with Cloudflare's nameserver"
else
yellow "Ping failed with Cloudflare's nameserver. Checking network configuration."
# 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 ""
# Check user's input and exit if they do not want to proceed
if [ "$priority" != "y" ]; then
exit 0
fi
# Check IP type and network priority
ip_type=$(curl -s ip.sb | grep -oP '(?<=is )(.+)(?=\.)')
green "IP type: $ip_type"
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
green "Network priority: $priority"
# 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
red "Network configuration is correct."
fi
# Try to ping again after modifying network priority
if ping -c 1 google.com; then
green "Ping successful after modifying network priority"
else
red "Network problem is not related to nameserver or network priority."
fi
fi
fi
fi
}
head
main
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/spring_0000/one-click-installation-script.git
git@gitee.com:spring_0000/one-click-installation-script.git
spring_0000
one-click-installation-script
one-click-installation-script
add-license-1

搜索帮助