mirror of
https://github.com/leitbogioro/Tools.git
synced 2024-11-15 21:54:40 +08:00
Update InstallNET.sh
This commit is contained in:
parent
a1f3fe215a
commit
8e09f2f290
1 changed files with 17 additions and 7 deletions
|
@ -298,7 +298,7 @@ while [[ $# -ge 1 ]]; do
|
||||||
# Check Root
|
# Check Root
|
||||||
[[ "$EUID" -ne '0' || $(id -u) != '0' ]] && echo -ne "\n[${red}Error${plain}] This script must be executed as root!\n\nTry to type:\n${yellow}sudo -s\n${plain}\nAfter entering the password, switch to root dir to execute this script:\n${yellow}cd ~${plain}\n\n" && exit 1
|
[[ "$EUID" -ne '0' || $(id -u) != '0' ]] && echo -ne "\n[${red}Error${plain}] This script must be executed as root!\n\nTry to type:\n${yellow}sudo -s\n${plain}\nAfter entering the password, switch to root dir to execute this script:\n${yellow}cd ~${plain}\n\n" && exit 1
|
||||||
|
|
||||||
# Ping delay to YouTube($1) and Instagram($2) and Twitter($3), support both ipv4 and ipv6, $4 is $IPStackType
|
# Ping delay to YouTube($1) and Instagram($2) and Wikipedia($3), support both ipv4 and ipv6, $4 is $IPStackType
|
||||||
function checkCN() {
|
function checkCN() {
|
||||||
for TestUrl in "$1" "$2" "$3"; do
|
for TestUrl in "$1" "$2" "$3"; do
|
||||||
# "rtt" result of ping command of Alpine Linux is "round-trip" and it can't handle "sed -n" well.
|
# "rtt" result of ping command of Alpine Linux is "round-trip" and it can't handle "sed -n" well.
|
||||||
|
@ -427,8 +427,8 @@ function getIPv4Address() {
|
||||||
ipPrefix=`echo ${iAddr} | cut -d'/' -f2`
|
ipPrefix=`echo ${iAddr} | cut -d'/' -f2`
|
||||||
ipMask=`netmask "$ipPrefix"`
|
ipMask=`netmask "$ipPrefix"`
|
||||||
# Get real IPv4 subnet of current System
|
# Get real IPv4 subnet of current System
|
||||||
ip4RouteScopeLink=`ip -4 route show scope link | grep -w "$interface4" | grep -w "$ipAddr" | grep -m1 -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -n 1`
|
ip4RouteScopeLink=`ip -4 route show scope link | grep -iv "warp" | grep -w "$interface4" | grep -w "$ipAddr" | grep -m1 -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -n 1`
|
||||||
actualIp4Prefix=`ip -4 route show scope link | grep -w "$interface4" | grep -w "$ip4RouteScopeLink" | head -n 1 | awk '{print $1}' | cut -d'/' -f2`
|
actualIp4Prefix=`ip -4 route show scope link | grep -iv "warp" | grep -w "$interface4" | grep -w "$ip4RouteScopeLink" | head -n 1 | awk '{print $1}' | cut -d'/' -f2`
|
||||||
[[ -z "$actualIp4Prefix" ]] && actualIp4Prefix="$ipPrefix"
|
[[ -z "$actualIp4Prefix" ]] && actualIp4Prefix="$ipPrefix"
|
||||||
actualIp4Subnet=`netmask "$actualIp4Prefix"`
|
actualIp4Subnet=`netmask "$actualIp4Prefix"`
|
||||||
# In most situation, at least 99.9% probability, the first hop of the network should be the same as the available gateway.
|
# In most situation, at least 99.9% probability, the first hop of the network should be the same as the available gateway.
|
||||||
|
@ -437,13 +437,13 @@ function getIPv4Address() {
|
||||||
# But installer said the correct gateway should be 5.45.76.1, in a typical network, for example, your home,
|
# But installer said the correct gateway should be 5.45.76.1, in a typical network, for example, your home,
|
||||||
# the default gateway is the same as the first route hop of the machine, it may be 192.168.0.1.
|
# the default gateway is the same as the first route hop of the machine, it may be 192.168.0.1.
|
||||||
# If possible, we should configure out the real available gateway of the network.
|
# If possible, we should configure out the real available gateway of the network.
|
||||||
FirstRoute=`ip -4 route show default | grep -w "via" | grep -w "dev $interface4*" | head -n 1 | awk -F " " '{for (i=3;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
FirstRoute=`ip -4 route show default | grep -iv "warp" | grep -w "via" | grep -w "dev $interface4*" | head -n 1 | awk -F " " '{for (i=3;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
||||||
# We should find it in ARP, the first hop IP and gateway IP is managed by the same device, use device mac address to configure it out.
|
# We should find it in ARP, the first hop IP and gateway IP is managed by the same device, use device mac address to configure it out.
|
||||||
RouterMac=`arp -n | grep "$FirstRoute" | awk '{print$3}'`
|
RouterMac=`arp -n | grep "$FirstRoute" | awk '{print$3}'`
|
||||||
FrFirst=`echo "$FirstRoute" | cut -d'.' -f 1,2`
|
FrFirst=`echo "$FirstRoute" | cut -d'.' -f 1,2`
|
||||||
FrThird=`echo "$FirstRoute" | cut -d'.' -f 3`
|
FrThird=`echo "$FirstRoute" | cut -d'.' -f 3`
|
||||||
# Print all matched available gateway.
|
# Print all matched available gateway.
|
||||||
ipGates=`ip -4 route show | grep -v "via" | grep -w "dev $interface4*" | grep -w "proto*" | grep -w "scope global\|link src $ipAddr*" | awk '{print$1}'`
|
ipGates=`ip -4 route show | grep -iv "warp" | grep -v "via" | grep -w "dev $interface4*" | grep -w "proto*" | grep -w "scope global\|link src $ipAddr*" | awk '{print$1}'`
|
||||||
# Figure out the line of this list.
|
# Figure out the line of this list.
|
||||||
ipGateLine=`echo "$ipGates" | wc -l`
|
ipGateLine=`echo "$ipGates" | wc -l`
|
||||||
# The line determines the cycling times.
|
# The line determines the cycling times.
|
||||||
|
@ -974,6 +974,14 @@ function checkIpv4OrIpv6() {
|
||||||
[[ "$tmpSetIPv6" == "0" ]] && setIPv6="0" || setIPv6="1"
|
[[ "$tmpSetIPv6" == "0" ]] && setIPv6="0" || setIPv6="1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Some "BiStack" types are accomplished by Warp which provided by CloudFlare, so we need to distinguish whether IPv4 or IPv6 stack in enabled by Warp.
|
||||||
|
function checkWarp() {
|
||||||
|
[[ -f "/etc/wireguard/warp.conf" || -f "/opt/warp-go/warp.conf" && "$IPStackType" == "BiStack" ]] && {
|
||||||
|
[[ -z "$ipGate" ]] && IPStackType="IPv6Stack"
|
||||||
|
[[ -z "$ip6Gate" ]] && IPStackType="IPv4Stack"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getIPv6Address() {
|
function getIPv6Address() {
|
||||||
# Differences from scope link, scope host and scope global of IPv6, reference: https://qiita.com/_dakc_/items/4eefa443306860bdcfde
|
# Differences from scope link, scope host and scope global of IPv6, reference: https://qiita.com/_dakc_/items/4eefa443306860bdcfde
|
||||||
i6Addr=`ip -6 addr show | grep -wA 5 "$interface" | grep -wv "lo\|host" | grep -wv "link" | grep -w "inet6" | grep "scope" | grep "global" | head -n 1 | awk -F " " '{for (i=2;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
i6Addr=`ip -6 addr show | grep -wA 5 "$interface" | grep -wv "lo\|host" | grep -wv "link" | grep -w "inet6" | grep "scope" | grep "global" | head -n 1 | awk -F " " '{for (i=2;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
||||||
|
@ -981,9 +989,9 @@ function getIPv6Address() {
|
||||||
ip6Addr=`echo ${i6Addr} |cut -d'/' -f1`
|
ip6Addr=`echo ${i6Addr} |cut -d'/' -f1`
|
||||||
ip6Mask=`echo ${i6Addr} |cut -d'/' -f2`
|
ip6Mask=`echo ${i6Addr} |cut -d'/' -f2`
|
||||||
# Get real IPv6 subnet of current System
|
# Get real IPv6 subnet of current System
|
||||||
actualIp6Prefix=`ip -6 route show | grep -w "$interface6" | grep -v "default" | grep -v "multicast" | grep -P '../[0-9]{1,3}' | head -n 1 | awk '{print $1}' | cut -d'/' -f2`
|
actualIp6Prefix=`ip -6 route show | grep -iv "warp" | grep -w "$interface6" | grep -v "default" | grep -v "multicast" | grep -P '../[0-9]{1,3}' | head -n 1 | awk '{print $1}' | cut -d'/' -f2`
|
||||||
[[ -z "$actualIp6Prefix" ]] && actualIp6Prefix="$ip6Mask"
|
[[ -z "$actualIp6Prefix" ]] && actualIp6Prefix="$ip6Mask"
|
||||||
ip6Gate=`ip -6 route show default | grep -w "$interface" | grep -w "via" | grep "dev" | head -n 1 | awk -F " " '{for (i=3;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
ip6Gate=`ip -6 route show default | grep -iv "warp" | grep -w "$interface" | grep -w "via" | grep "dev" | head -n 1 | awk -F " " '{for (i=3;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
||||||
[[ -n "$interface4" && -n "$interface6" && "$interface4" != "$interface6" ]] && ip6Gate=`ip -6 route show default | grep -w "$interface6" | grep -w "via" | grep "dev" | head -n 1 | awk -F " " '{for (i=3;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
[[ -n "$interface4" && -n "$interface6" && "$interface4" != "$interface6" ]] && ip6Gate=`ip -6 route show default | grep -w "$interface6" | grep -w "via" | grep "dev" | head -n 1 | awk -F " " '{for (i=3;i<=NF;i++)printf("%s ", $i);print ""}' | awk '{print$1}'`
|
||||||
# IPv6 expansion algorithm code reference: https://blog.caoyu.info/expand-ipv6-by-shell.html
|
# IPv6 expansion algorithm code reference: https://blog.caoyu.info/expand-ipv6-by-shell.html
|
||||||
ip6AddrWhole=`ultimateFormatOfIpv6 "$ip6Addr"`
|
ip6AddrWhole=`ultimateFormatOfIpv6 "$ip6Addr"`
|
||||||
|
@ -2024,6 +2032,8 @@ if [[ "$setNet" == "0" ]]; then
|
||||||
[[ "$IPStackType" != "IPv4Stack" ]] && getIPv6Address
|
[[ "$IPStackType" != "IPv4Stack" ]] && getIPv6Address
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
checkWarp
|
||||||
|
|
||||||
IPv4="$ipAddr"; MASK="$ipMask"; GATE="$ipGate";
|
IPv4="$ipAddr"; MASK="$ipMask"; GATE="$ipGate";
|
||||||
if [[ -z "$IPv4" && -z "$MASK" && -z "$GATE" ]] && [[ -z "$ip6Addr" && -z "$ip6Mask" && -z "$ip6Gate" ]]; then
|
if [[ -z "$IPv4" && -z "$MASK" && -z "$GATE" ]] && [[ -z "$ip6Addr" && -z "$ip6Mask" && -z "$ip6Gate" ]]; then
|
||||||
echo -ne "\n[${red}Error${plain}] The network of your machine may not be available!\n"
|
echo -ne "\n[${red}Error${plain}] The network of your machine may not be available!\n"
|
||||||
|
|
Loading…
Reference in a new issue