Files
CatTools/cattools.sh

466 lines
13 KiB
Bash
Raw Normal View History

2023-12-12 17:36:37 +08:00
#!/bin/bash
###
# @Author: miaoermua
# @Date: 2023-12-12 16:59:27
# @LastEditors: miaoermua
2023-12-27 15:43:13 +08:00
# @LastEditTime: 2023-12-27 15:42:39
2023-12-12 17:36:37 +08:00
# @FilePath: \undefinedd:\Git\cattools\cattools.sh
###
2023-12-27 15:43:13 +08:00
DEFAULT_IP="192.168.1.4"
RELEASE="/etc/catwrt_release"
AMD64_REPO="https://fastly.jsdelivr.net/gh/miaoermua/cattools@main/repo/amd64/distfeeds.conf"
MT798X_REPO="https://fastly.jsdelivr.net/gh/miaoermua/cattools@main/repo/mt798x/distfeeds.conf"
AMD64_EFI_SYSUP="https://github.com/miaoermua/CatWrt/releases/download/v23.8/CatWrt.v23.8.x86_64-squashfs-combined-efi.img.gz"
AMD64_BIOS_SYSUP="https://github.com/miaoermua/CatWrt/releases/download/v23.8/CatWrt.v23.8.x86_64-squashfs-combined.img.gz"
2023-12-12 17:36:37 +08:00
2023-12-27 22:18:00 +08:00
# Check ROOT & OpenWrt
2023-12-12 18:02:05 +08:00
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root user"
exit 1
fi
2023-12-27 22:18:00 +08:00
openwrt_release=$(cat /etc/openwrt_release)
if ! grep -q "OpenWrt" <<< "$openwrt_release"; then
echo "Your system is not supported!"
2023-12-27 15:08:52 +08:00
exit 1
2023-12-12 18:02:05 +08:00
fi
2023-12-27 22:18:00 +08:00
2023-12-27 17:40:24 +08:00
update(){
2023-12-27 18:11:12 +08:00
if ! curl -fsSLo /root/cattools https://service.miaoer.xyz/cattools/cattools.sh; then
2023-12-27 22:09:21 +08:00
echo "[main site]Unable to connect to update site! 无法连接更新站点!"
2023-12-27 17:40:24 +08:00
2023-12-27 18:11:12 +08:00
if ! curl -fsSLo /root/cattools https://fastly.jsdelivr.net/gh/miaoermua/cattools@main/repo/mt798x/cattools.sh; then
2023-12-27 22:09:21 +08:00
echo "[site 2]Unable to connect to update site! 无法连接更新仓库"
2023-12-27 17:40:24 +08:00
2023-12-27 18:11:12 +08:00
if ! curl -fsSLo /root/cattools https://raw.githubusercontent.com/miaoermua/service/main/cattools/cattools.sh; then
2023-12-27 22:09:21 +08:00
echo "[site 3]Unable to connect to update site! 无法连接更新仓库"
echo "Unable to connect, please check the internet! 无法连接互联网,请联系作者!"
2023-12-27 17:40:24 +08:00
return
fi
fi
fi
exec $(readlink -f "$0")
}
2023-12-12 17:36:37 +08:00
setip(){
2023-12-27 15:43:13 +08:00
read -p "请输入 IP(默认为 $DEFAULT_IP): " input_ip
2023-12-12 17:36:37 +08:00
if [ -z $input_ip ]; then
2023-12-27 15:43:13 +08:00
input_ip=$DEFAULT_IP
2023-12-12 17:36:37 +08:00
fi
uci set network.lan.ipaddr=$input_ip
uci commit network
/etc/init.d/network restart
2023-12-27 18:02:25 +08:00
echo "默认 IP 已设置为 $input_ip"
2023-12-12 17:36:37 +08:00
}
catwrt_update(){
2023-12-12 18:08:16 +08:00
/usr/bin/catwrt-update
2023-12-12 17:36:37 +08:00
}
catwrt_network_diagnostics(){
2023-12-27 18:15:28 +08:00
echo " "
echo "$(date) - Starting CatWrt Network Diagnostics"
echo " "
# Ping & PPPoE
ping -c 3 223.5.5.5 > /dev/null
if [ $? -eq 0 ]; then
echo "[Ping] Network connection succeeded!"
echo " "
else
ping -c 3 119.29.29.99 > /dev/null
if [ $? -eq 0 ]; then
echo "[Ping] Network connection succeeded,But there may be problems!"
echo " "
else
pppoe_config=$(grep 'pppoe' /etc/config/network)
if [ ! -z "$pppoe_config" ]; then
echo "[PPPoE] Please check if your PPPoE account and password are correct."
echo " "
fi
exit 1
fi
fi
# DNS
valid_dns="1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 223.6.6.6 223.5.5.5 180.76.76.76 208.67.222.222 208.67.220.220 119.29.29.99"
dns_config=$(grep 'option dns' /etc/config/network)
dns_servers=$(echo $dns_config | awk -F "'" '{print $2}')
for ip in $dns_servers; do
if ! [[ $valid_dns =~ (^|[ ])$ip($|[ ]) ]]; then
echo "[DNS] Recommended to delete DNS $ip"
echo " "
exit 1
fi
done
# Bad DNS
echo "[DNS] DNS configuration looks good!"
echo " "
bad_dns="114.114.114.114 114.114.115.115 119.29.29.29"
if [[ $dns_config =~ $bad_dns ]]; then
echo "[DNS] DNS may be polluted or unreliable"
echo " "
fi
# nslookup
nslookup bilibili.com > /dev/null
if [ $? -ne 0 ]; then
nslookup www.miaoer.xyz > /dev/null
if [ $? -eq 0 ]; then
echo "[DNS] DNS resolution succeeded"
echo " "
else
echo "[DNS] NS resolution failed for 'www.miaoer.xyz'"
echo "[DNS] Your DNS server may have issues"
echo " "
fi
fi
# Public IP
echo CatWrt IPv4 Addr: $(curl --silent --connect-timeout 5 4.ipw.cn )
echo " "
curl 6.ipw.cn --connect-timeout 5 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "[IPv6] IPv6 network connection timed out"
echo " "
else
echo CatWrt IPv6 Addr: $(curl --silent 6.ipw.cn)
echo " "
fi
# IPv6
resp=$(curl --silent test.ipw.cn)
if echo "$resp" | grep -q -E '240e|2408|2409|2401'; then
echo "[IPv6] IPv6 access is preferred"
echo " "
else
echo "[IPv6] IPv4 access is preferred"
echo " "
fi
# Default IP
ipaddr_config=$(grep '192.168.1.4' /etc/config/network)
if [ -z "$ipaddr_config" ]; then
echo "[Default-IP] address is not the catwrt default 192.168.1.4"
echo "Please configure your network at 'https://www.miaoer.xyz/posts/network/quickstart-catwrt'"
echo " "
fi
# Bypass Gateway
wan_config=$(grep 'config interface' /etc/config/network | grep 'wan')
if [ -z "$wan_config" ]; then
echo "[Bypass Gateway] No config for 'wan' interface found in /etc/config/network"
echo "Please check if your device is set as a Bypass Gateway"
echo " "
fi
# Rotuer Mode(PPPoE)
pass_config=$(grep 'password' /etc/config/network)
user_config=$(grep 'username' /etc/config/network)
pppoe_config=$(grep 'pppoe' /etc/config/network)
if [ -n "$pass_config" ] && [ -n "$user_config" ] && [ -n "$pppoe_config" ]; then
echo "[PPPoE] PPPoE Rotuer Mode"
echo " "
else
echo "[PPPoE] DHCP protocol detected in WAN interface"
echo "The device may not be in PPPoE Rotuer Mode"
echo " "
fi
# IPv6 WAN6
grep 'config interface' /etc/config/network | grep 'wan6' > /dev/null
if [ $? -ne 0 ]; then
echo "[wan6] Your IPv6 network may have issues"
echo " "
fi
grep 'dhcpv6' /etc/config/network > /dev/null
if [ $? -ne 0 ]; then
echo "[wan6] Your IPv6 network may have issues"
echo " "
fi
# Tcping
echo "[Tcping] Testing..."
tcping -q -c 1 cn.bing.com
[ $? -ne 0 ] && echo "Failed: cn.bing.com"
tcping -q -c 1 bilibili.com
[ $? -ne 0 ] && echo "Failed: bilibili.com"
tcping -q -c 1 github.com
[ $? -ne 0 ] && echo "Failed: github.com"
tcping -q -c 1 google.com.hk
[ $? -ne 0 ] && echo "Failed: google.com.hk"
echo " "
echo "$(date) - Network check completed"
echo " "
2023-12-12 17:36:37 +08:00
}
2023-12-12 18:02:05 +08:00
use_repo(){
2024-01-10 21:32:31 +08:00
# fk is
2024-01-10 21:34:06 +08:00
if [ -f "/var/opkg-lists/istore_compat" ]; then
rm /var/opkg-lists/istore_compat
2024-01-10 21:32:31 +08:00
fi
2023-12-27 18:02:25 +08:00
echo "Warning:"
echo "软件源纯属免费分享,赞助我们复制链接在浏览器打开,这对我们继续保持在线服务有很大影响。"
echo "本人不对所有软件进行保证,我们没有第三方商业服务,风险需要自行承担。"
echo "支持我们: https://www.miaoer.xyz/sponsor"
2023-12-27 13:40:46 +08:00
echo "你需要同意 CatWrt 软件源用户协议,请确认是否继续 (y/n)"
2023-12-12 18:02:05 +08:00
read -t 10 -p "您有 10 秒选择,输入 y 继续,其他退出:" confirm
[ "$confirm" != y ] && return
arch=$(uname -m)
2023-12-27 13:40:46 +08:00
# Check ARCH release
model=$(grep "Model:" $release | cut -d ' ' -f2)
if [[ $model =~ "mt798x" ]]; then
# mt798x
2023-12-27 15:43:13 +08:00
curl -o /etc/opkg/distfeeds.conf $MT798X_REPO
2023-12-27 13:40:46 +08:00
elif [ "$arch" = "x86_64" ]; then
# amd64
2023-12-27 15:43:13 +08:00
curl -o /etc/opkg/distfeeds.conf $AMD64_REPO
2023-12-27 13:40:46 +08:00
2023-12-12 18:02:05 +08:00
else
2023-12-27 13:40:46 +08:00
echo "不支持的机型: $model"
return
2023-12-12 18:02:05 +08:00
fi
}
2023-12-12 17:36:37 +08:00
catwrt_sysupgrade(){
2023-12-27 18:02:25 +08:00
# uname
2023-12-12 17:36:37 +08:00
if [[ $(uname -m) =~ "x86_64" ]]; then
echo "CatWrt ARCH: x86_64(AMD64)"
2023-12-27 18:02:25 +08:00
# Check Disk space remaining
2023-12-12 17:36:37 +08:00
size=$(fdisk -l /dev | grep "Disk /dev" | awk '{print $5}')
size=${size%\*}
if [[ $size > 820 ]]; then
2023-12-27 18:02:25 +08:00
echo "磁盘空间出现了更改!脚本退出!"
2023-12-12 17:36:37 +08:00
exit
fi
2023-12-27 18:02:25 +08:00
# Check EFI remaining
2023-12-12 17:36:37 +08:00
if [[ -b /dev/sda128 || -b /dev/vda128 ]]; then
efi_part=true
else
efi_part=false
fi
2023-12-27 18:02:25 +08:00
# confirmation
echo "即将升级系统,存在风险请你三思确认 (y/n) 30 秒后默认退出脚本!"
2023-12-12 17:36:37 +08:00
read -t 30 confirm
confirm=${confirm:-n}
2023-12-27 18:02:25 +08:00
# upgrade
2023-12-12 17:36:37 +08:00
if [[ $confirm =~ [Yy] ]]; then
if [[ $efi_part == true ]]; then
2023-12-27 15:43:13 +08:00
sysupgrade -v $AMD64_EFI_SYSUP
2023-12-12 17:36:37 +08:00
else
2023-12-27 15:43:13 +08:00
sysupgrade -v $AMD64_BIOS_SYSUP
2023-12-12 17:36:37 +08:00
fi
else
2023-12-27 18:02:25 +08:00
echo "用户已取消升级!"
2023-12-12 17:36:37 +08:00
fi
else
2023-12-27 18:02:25 +08:00
echo "仅有 x86_64 可以使用脚本进行系统升级。"
2023-12-12 17:36:37 +08:00
fi
}
2023-12-27 14:11:12 +08:00
debug(){
2023-12-27 14:52:00 +08:00
2023-12-27 14:11:12 +08:00
if [ -f /www/logs.txt ]; then
rm /www/logs.txt
fi
2023-12-27 17:32:18 +08:00
cat /etc/banner >> /www/logs.txt
echo "## RELEASE" >> /www/logs.txt
2024-01-06 00:23:23 +08:00
echo "==========" >> /www/logs.txt
2023-12-27 15:08:52 +08:00
cat /etc/catwrt_release >> /www/logs.txt
2024-01-06 00:23:23 +08:00
echo "## Application" >> /www/logs.txt
echo "==========" >> /www/logs.txt
opkg list_installed >> /www/logs.txt
2023-12-27 17:32:18 +08:00
echo "## SYSLOG" >> /www/logs.txt
2024-01-06 00:23:23 +08:00
echo "==========" >> /www/logs.txt
2023-12-27 15:04:26 +08:00
logread >> /www/logs.txt
2023-12-27 17:32:18 +08:00
echo "## DMESG" >> /www/logs.txt
2024-01-06 00:23:23 +08:00
echo "==========" >> /www/logs.txt
2023-12-27 14:11:12 +08:00
dmesg >> /www/logs.txt
lan_ip=$(uci get network.lan.ipaddr)
2023-12-27 17:32:18 +08:00
echo "日志已收集到 /www/logs.txt"
echo "使用浏览器访问下载 http://$lan_ip/logs.txt"
2023-12-27 15:04:26 +08:00
exit
2023-12-27 14:11:12 +08:00
}
2024-01-06 00:23:23 +08:00
setup(){
read -p "Do you want toNetwork Setup? 是否使用网络向导? (Enter/1) " use_wizard
if [ "$use_wizard" != 1 ]; then
exit 0
fi
echo "CatWrt default IP is 192.168.1.4. 默认 CatWrt IP 为 192.168.1.4"
read -p "Do you want to change IP address? 是否修改IP地址? (Enter/1) " change_ip
if [ "$change_ip" = 1 ]; then
read -p "Please input new IP: " new_ip
uci set network.lan.ipaddr=$new_ip
fi
echo "Recommended DNS: 223.6.6.6 119.29.29.99. 推荐使用的DNS: 223.6.6.6 119.29.29.99"
read -p "Use recommended DNS servers? 使用推荐的DNS服务器? (Enter/1) " use_recommended_dns
if [ "$use_recommended_dns" = 1 ]; then
read -p "Please input DNS servers separated by space 请输入以空格分隔的DNS服务器: " dns_servers
uci set network.lan.dns="$dns_servers"
else
uci set network.lan.dns="223.6.6.6 119.29.29.99"
fi
echo "IPv6 is enabled by default. IPv6 默认是开启的"
read -p "Disable IPv6? 是否禁用IPv6? (Enter/1) " disable_ipv6
if [ "$disable_ipv6" = 1 ]; then
uci delete dhcp.lan.dhcpv6
uci delete dhcp.lan.ra
uci delete dhcp.lan.ra_management
uci delete network.lan.ip6assign
fi
echo "Default connection mode is DHCP. 默认模式为 DHCP"
read -p "Use PPPoE dial up instead? 使用PPPoE拨号上网? (Enter/1) " use_pppoe
if [ "$use_pppoe" = 1 ]; then
read -p "Please input PPPoE username: " pppoe_user
read -p "Please input PPPoE password: " pppoe_pass
uci set network.wan.proto=pppoe
uci set network.wan.username=$pppoe_user
uci set network.wan.password=$pppoe_pass
fi
echo "Default client IP range: 30 to 200. 默认客户端 IP 段为 30 到 200"
read -p "Set custom client IP range instead? 设置自定义客户端IP段? (Enter/1)" custom_ip_range
if [ "$custom_ip_range" = 1 ]; then
read -p "Please input start IP: " dhcp_start
read -p "Please input end IP: " dhcp_limit
uci set dhcp.lan.start=$dhcp_start
uci set dhcp.lan.limit=$dhcp_limit
fi
echo "WiFi 部分将等待完善!"
uci commit
/etc/init.d/network restart
/etc/init.d/dnsmasq restart
/etc/init.d/firewall restart
ping -c 2 bilibili.com > /dev/null
if [ $? -eq 0 ]; then
echo "Network connectivity OK!"
else
lan_ip=$(uci get network.lan.ipaddr)
echo "Network error, bilibili.com ping failed."
echo "Please login to LuCI interface: http://$lan_ip to check settings."
fi
echo "Network Wizard completed!"
2023-12-27 14:52:00 +08:00
bypass_gateway(){
echo "没做完"
exit
}
2023-12-27 15:08:52 +08:00
2023-12-27 17:40:24 +08:00
update
2023-12-27 13:11:16 +08:00
2023-12-12 17:36:37 +08:00
while :; do
clear
2023-12-27 15:08:52 +08:00
echo " "
2023-12-27 14:52:00 +08:00
echo " CatTools"
echo "----------------------------------------"
2024-01-06 00:23:23 +08:00
echo "1. Setup Network 网络向导"
echo "2. Set IPv4 Addr 设置 IP"
echo "3. check update 检查系统更新"
echo "4. network diagnostics 网络诊断"
echo "5. use repo 使用软件源"
echo "6. sysupgrade 升级系统"
echo "7. debug 日志收集"
echo "8. setup bypass gateway 旁路网关"
2023-12-27 14:52:00 +08:00
echo "0. Exit 退出脚本"
echo "----------------------------------------"
2023-12-27 22:42:12 +08:00
echo -en "请选择数字按下回车: "
2023-12-27 22:47:23 +08:00
read choice
2023-12-12 17:36:37 +08:00
case $choice in
1)
2024-01-06 00:23:23 +08:00
setup
2023-12-12 17:36:37 +08:00
;;
2)
2024-01-06 00:23:23 +08:00
setip
2023-12-12 17:36:37 +08:00
;;
3)
2024-01-06 00:23:23 +08:00
catwrt_update
2023-12-12 17:36:37 +08:00
;;
2023-12-12 18:02:05 +08:00
4)
2024-01-06 00:23:23 +08:00
catwrt_network_diagnostics
2023-12-12 18:02:05 +08:00
;;
2023-12-12 18:04:03 +08:00
5)
2024-01-06 00:23:23 +08:00
use_repo
2023-12-12 17:36:37 +08:00
;;
2023-12-27 14:11:12 +08:00
6)
2024-01-06 00:23:23 +08:00
catwrt_sysupgrade
2023-12-27 14:11:12 +08:00
;;
2023-12-27 14:52:00 +08:00
7)
bypass_gateway
;;
2024-01-06 00:23:23 +08:00
8)
debug
;;
2023-12-12 17:36:37 +08:00
0)
2023-12-12 18:02:05 +08:00
echo "Exit CatTools 退出脚本..."
2023-12-12 17:36:37 +08:00
break
;;
*)
echo "无效的选择, 请重新输入"
2023-12-27 22:54:02 +08:00
exit
2023-12-12 17:36:37 +08:00
;;
esac
sleep 1
done
2023-12-12 18:02:05 +08:00
echo "结束!"