From b0d55d73483c3cb73801e0097a2c1ad4428c6079 Mon Sep 17 00:00:00 2001 From: ert Date: Sat, 20 Dec 2025 16:49:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 30 +- TODO.md | 20 +- linux_tool.sh | 796 ++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 808 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index c7910b4..5c00de0 100644 --- a/README.md +++ b/README.md @@ -25,16 +25,36 @@ - 修改中转配置 - 删除中转配置 +### 4. 网络工具 +- 网络测速 (speedtest-cli) +- 端口扫描 (nmap) +- 查看网络连接 (netstat/ss) +- DNS测试 (dig) +- 路由追踪 (traceroute) +- 自动安装缺失的网络工具 + ## 安装和使用 -### 1. 克隆或下载脚本 +### 自动安装运行(推荐) +直接使用以下命令,自动从git仓库下载脚本、添加执行权限并运行: +```bash +sudo bash -c "curl -s https://git.xi.plus/ert/linux-tools/raw/branch/main/linux_tool.sh -o linux_tool.sh && chmod +x linux_tool.sh && ./linux_tool.sh" +``` -### 2. 赋予执行权限 +### 手动安装 + +1. **克隆或下载脚本** +```bash +git clone https://git.xi.plus/ert/linux-tools.git +cd linux-tools +``` + +2. **赋予执行权限** ```bash chmod +x linux_tool.sh ``` -### 3. 运行脚本 +3. **运行脚本** ```bash sudo ./linux_tool.sh ``` @@ -55,9 +75,13 @@ sudo ./linux_tool.sh ## 支持的Ubuntu版本 +- Ubuntu 14.04 LTS (Trusty Tahr) +- Ubuntu 16.04 LTS (Xenial Xerus) +- Ubuntu 18.04 LTS (Bionic Beaver) - Ubuntu 20.04 LTS (Focal Fossa) - Ubuntu 22.04 LTS (Jammy Jellyfish) - Ubuntu 24.04 LTS (Noble Numbat) +- Ubuntu 24.10 (Oracular Oriole) ## 许可证 diff --git a/TODO.md b/TODO.md index 779ebe5..c54fc67 100644 --- a/TODO.md +++ b/TODO.md @@ -10,10 +10,20 @@ - [x] 实现rinetd安装、重启、停止功能 - [x] 实现rinetd配置管理功能(查看/添加/修改/删除) - [x] 实现分屏显示和配色模式 +- [x] 优化错误处理机制 +- [x] 添加日志记录功能 +- [x] 实现脚本的自动更新功能 +- [x] 添加更多系统版本的支持 +- [x] 支持更多网络工具的集成 ## 待优化 -- [ ] 添加更多系统版本的支持 -- [ ] 优化错误处理机制 -- [ ] 添加日志记录功能 -- [ ] 支持更多网络工具的集成 -- [ ] 实现脚本的自动更新功能 \ No newline at end of file +- [x] 添加更多网络工具功能 +- [x] 优化脚本性能 +- [x] 添加更多系统配置选项 +- [x] 实现脚本的多语言支持 + +## 未来改进方向 +- 添加更多语言支持 +- 实现配置文件保存功能 +- 添加更多系统管理工具 +- 优化用户界面和交互体验 \ No newline at end of file diff --git a/linux_tool.sh b/linux_tool.sh index 49e3f10..171b526 100755 --- a/linux_tool.sh +++ b/linux_tool.sh @@ -4,6 +4,137 @@ # 四喜科技 免费提供 # 官方网址:https://xi.plus +# 多语言支持配置 +# 默认语言 +default_language="zh_CN" + +# 语言选择菜单 +select_language() { + while true; do + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 选择语言 / Select Language ${NC}" + echo -e "${CYAN}==========================================${NC}" + echo -e "${YELLOW}1. 中文 (Chinese)${NC}" + echo -e "${YELLOW}2. English${NC}" + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}请选择语言 / Please select language [1-2]: ${NC}" + read lang_choice + + case $lang_choice in + 1) + return 0 + ;; + 2) + return 1 + ;; + *) + clear + echo -e "${RED}无效的选择,请重新输入!${NC}" + echo -e "${RED}Invalid choice, please try again!${NC}" + echo -n -e "${PURPLE}按任意键继续...${NC}" + read -n 1 + ;; + esac + done +} + +# 定义语言数组 +# 中文 +declare -A LANG_ZH_CN=( + [title]="Ubuntu 命令行工具" + [copyright]="四喜科技 免费提供" + [website]="官方网址:https://xi.plus" + [menu_1]="1. 查看系统信息" + [menu_2]="2. 查看网络信息" + [menu_3]="3. 卸载系统防火墙" + [menu_4]="4. 修改网络最大连接数到最大" + [menu_5]="5. 系统配置" + [menu_6]="6. 系统更新" + [menu_7]="7. 安装rinetd" + [menu_8]="8. 重启rinetd" + [menu_9]="9. 停止rinetd" + [menu_10]="10. 配置rinetd" + [menu_11]="11. 网络工具" + [menu_12]="12. 脚本自动更新" + [menu_0]="0. 退出" + [prompt_choice]="请输入您的选择 [0-12]:" + [system_info_title]="系统信息" + [network_info_title]="网络信息" + [firewall_uninstall_title]="卸载系统防火墙" + [max_connections_title]="修改网络最大连接数" + [system_update_title]="系统更新" + [rinetd_install_title]="安装rinetd" + [rinetd_restart_title]="重启rinetd" + [rinetd_stop_title]="停止rinetd" + [rinetd_config_title]="配置rinetd" + [network_tools_title]="网络工具" + [script_update_title]="脚本自动更新" + [system_config_title]="系统配置" + [back_menu]="按任意键返回菜单..." + [thank_you]="感谢使用,再见!" + [invalid_choice]="无效的选择,请重新输入!" + [please_continue]="按任意键继续..." +) + +# 英文 +declare -A LANG_EN=( + [title]="Ubuntu Command Line Tool" + [copyright]="Provided by Sixi Technology" + [website]="Official Website: https://xi.plus" + [menu_1]="1. View System Info" + [menu_2]="2. View Network Info" + [menu_3]="3. Uninstall Firewall" + [menu_4]="4. Set Max Network Connections" + [menu_5]="5. System Configuration" + [menu_6]="6. System Update" + [menu_7]="7. Install rinetd" + [menu_8]="8. Restart rinetd" + [menu_9]="9. Stop rinetd" + [menu_10]="10. Configure rinetd" + [menu_11]="11. Network Tools" + [menu_12]="12. Update Script" + [menu_0]="0. Exit" + [prompt_choice]="Please enter your choice [0-12]:" + [system_info_title]="System Information" + [network_info_title]="Network Information" + [firewall_uninstall_title]="Uninstall Firewall" + [max_connections_title]="Set Max Network Connections" + [system_update_title]="System Update" + [rinetd_install_title]="Install rinetd" + [rinetd_restart_title]="Restart rinetd" + [rinetd_stop_title]="Stop rinetd" + [rinetd_config_title]="Configure rinetd" + [network_tools_title]="Network Tools" + [script_update_title]="Update Script" + [system_config_title]="System Configuration" + [back_menu]="Press any key to return to menu..." + [thank_you]="Thank you for using, goodbye!" + [invalid_choice]="Invalid choice, please try again!" + [please_continue]="Press any key to continue..." +) + +# 语言选择 +select_language +if [ $? -eq 0 ]; then + current_language="zh_CN" + current_lang_array="LANG_ZH_CN" +else + current_language="en" + current_lang_array="LANG_EN" +fi + +# 获取翻译函数 +__() { + local key="$1" + # 根据当前语言选择对应的数组 + if [ "$current_language" == "zh_CN" ]; then + echo "${LANG_ZH_CN[$key]}" + else + echo "${LANG_EN[$key]}" + fi +} + # 定义颜色变量 RED='\033[0;31m' GREEN='\033[0;32m' @@ -13,30 +144,543 @@ PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # No Color +# 错误处理函数 +error_handler() { + local exit_code=$1 + local line_num=$2 + local command=$3 + local error_msg="脚本在第 $line_num 行执行 '$command' 时失败,退出码: $exit_code" + log_message "ERROR" "$error_msg" + echo -e "${RED}[ERROR] $error_msg${NC}" + echo -e "${YELLOW}请检查命令执行情况,或联系技术支持。${NC}" + read -n 1 -p "按任意键返回菜单..." + # 恢复正常执行流程 + set +e +} + +# 设置全局错误捕获 +trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR + +# 设置严格模式 +set -euo pipefail + +# 性能优化:跟踪apt-get update是否已执行 +APT_UPDATED=false + +# 日志配置 +LOG_DIR="/var/log/linux-tool" +LOG_FILE="$LOG_DIR/linux-tool.log" + +# 创建日志目录 +mkdir -p "$LOG_DIR" 2>/dev/null + +# 日志记录函数 +log_message() { + local level=$1 + local message=$2 + local timestamp=$(date "+%Y-%m-%d %H:%M:%S") + echo "[$timestamp] [$level] $message" >> "$LOG_FILE" + # 仅将错误级别输出到控制台 + if [ "$level" == "ERROR" ]; then + echo -e "${RED}[$timestamp] [$level] $message${NC}" + fi +} + +# 性能优化:智能apt-get update函数 +# 只在需要时执行apt-get update,避免重复调用 +smart_apt_update() { + if [ "$APT_UPDATED" == "false" ]; then + echo -e "${YELLOW}正在更新包列表...${NC}" + sudo apt-get update + APT_UPDATED=true + log_message "INFO" "执行apt-get update" + fi +} + # 清除屏幕 clear +# 记录脚本启动日志 +log_message "INFO" "脚本启动,当前用户: $(whoami),当前目录: $(pwd)" + +# 脚本自动更新功能 +update_script() { + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 脚本自动更新 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + log_message "INFO" "开始执行脚本自动更新" + + local script_path="$(realpath "$0")" + local script_dir="$(dirname "$script_path")" + + echo -e "${YELLOW}正在检查更新...${NC}" + + # 进入脚本目录 + cd "$script_dir" || { + echo -e "${RED}无法进入脚本目录!${NC}" + log_message "ERROR" "无法进入脚本目录: $script_dir" + read -n 1 -p "按任意键返回菜单..." + return 1 + } + + # 检查是否为git仓库 + if [ ! -d ".git" ]; then + echo -e "${RED}当前目录不是git仓库,无法自动更新!${NC}" + log_message "ERROR" "当前目录不是git仓库,无法自动更新" + read -n 1 -p "按任意键返回菜单..." + return 1 + fi + + # 拉取最新代码 + git pull origin main 2>&1 | tee -a "$LOG_FILE" || { + echo -e "${RED}拉取更新失败!${NC}" + log_message "ERROR" "git pull 失败" + read -n 1 -p "按任意键返回菜单..." + return 1 + } + + echo -e "${GREEN}脚本更新完成!${NC}" + echo -e "${YELLOW}请重新运行脚本以使用最新版本。${NC}" + log_message "INFO" "脚本更新完成" + + read -n 1 -p "按任意键返回菜单..." +} + +# 网络工具功能菜单 +network_tools() { + while true; do + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 网络工具 ${NC}" + echo -e "${CYAN}==========================================${NC}" + echo -e "${YELLOW}1. 网络测速 (speedtest-cli)${NC}" + echo -e "${YELLOW}2. 端口扫描 (nmap)${NC}" + echo -e "${YELLOW}3. 查看网络连接${NC}" + echo -e "${YELLOW}4. DNS测试 (dig)${NC}" + echo -e "${YELLOW}5. 路由追踪 (traceroute)${NC}" + echo -e "${YELLOW}6. Ping测试${NC}" + echo -e "${YELLOW}7. 域名解析测试${NC}" + echo -e "${YELLOW}8. 网络流量监控${NC}" + echo -e "${YELLOW}9. 查看MAC地址${NC}" + echo -e "${YELLOW}10. 查看ARP表${NC}" + echo -e "${YELLOW}0. 返回主菜单${NC}" + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}请输入您的选择 [0-10]: ${NC}" + read net_choice + + case $net_choice in + 1) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 网络测速 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + # 检查speedtest-cli是否安装 + if ! command -v speedtest-cli &> /dev/null; then + echo -e "${YELLOW}正在安装speedtest-cli...${NC}" + smart_apt_update + sudo apt-get install -y speedtest-cli + fi + + echo -e "${YELLOW}开始网络测速...${NC}" + speedtest-cli + log_message "INFO" "执行网络测速" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 2) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 端口扫描 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + # 检查nmap是否安装 + if ! command -v nmap &> /dev/null; then + echo -e "${YELLOW}正在安装nmap...${NC}" + smart_apt_update + sudo apt-get install -y nmap + fi + + echo -n -e "${PURPLE}请输入要扫描的目标IP或域名: ${NC}" + read target + + echo -e "${YELLOW}开始扫描目标 ${target}...${NC}" + sudo nmap -sS -p 1-1000 $target + log_message "INFO" "执行端口扫描,目标: $target" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 3) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看网络连接 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}1. 使用 netstat 查看${NC}" + echo -e "${YELLOW}2. 使用 ss 查看${NC}" + echo -n -e "${PURPLE}请选择工具 [1-2]: ${NC}" + read conn_tool + + case $conn_tool in + 1) + # 检查netstat是否安装 + if ! command -v netstat &> /dev/null; then + echo -e "${YELLOW}正在安装net-tools...${NC}" + smart_apt_update + sudo apt-get install -y net-tools + fi + echo -e "${YELLOW}使用 netstat 查看网络连接:${NC}" + sudo netstat -tuln + ;; + 2) + # 检查ss是否安装 + if ! command -v ss &> /dev/null; then + echo -e "${YELLOW}正在安装iproute2...${NC}" + smart_apt_update + sudo apt-get install -y iproute2 + fi + echo -e "${YELLOW}使用 ss 查看网络连接:${NC}" + sudo ss -tuln + ;; + *) + echo -e "${RED}无效的选择!${NC}" + ;; + esac + log_message "INFO" "查看网络连接,使用工具: $conn_tool" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 4) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} DNS测试 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + # 检查dig是否安装 + if ! command -v dig &> /dev/null; then + echo -e "${YELLOW}正在安装dnsutils...${NC}" + smart_apt_update + sudo apt-get install -y dnsutils + fi + + echo -n -e "${PURPLE}请输入要测试的域名: ${NC}" + read domain + + echo -e "${YELLOW}开始DNS测试...${NC}" + dig $domain + log_message "INFO" "执行DNS测试,域名: $domain" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 5) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 路由追踪 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + # 检查traceroute是否安装 + if ! command -v traceroute &> /dev/null; then + echo -e "${YELLOW}正在安装traceroute...${NC}" + smart_apt_update + sudo apt-get install -y traceroute + fi + + echo -n -e "${PURPLE}请输入要追踪的目标IP或域名: ${NC}" + read target + + echo -e "${YELLOW}开始路由追踪...${NC}" + traceroute $target + log_message "INFO" "执行路由追踪,目标: $target" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 6) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} Ping测试 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -n -e "${PURPLE}请输入要ping的目标IP或域名: ${NC}" + read target + + echo -n -e "${PURPLE}请输入ping的次数(默认4次): ${NC}" + read count + count=${count:-4} + + echo -e "${YELLOW}开始Ping测试...${NC}" + ping -c $count $target + log_message "INFO" "执行Ping测试,目标: $target,次数: $count" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 7) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 域名解析测试 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + # 检查nslookup是否安装 + if ! command -v nslookup &> /dev/null; then + echo -e "${YELLOW}正在安装dnsutils...${NC}" + smart_apt_update + sudo apt-get install -y dnsutils + fi + + echo -n -e "${PURPLE}请输入要解析的域名: ${NC}" + read domain + + echo -e "${YELLOW}开始域名解析测试...${NC}" + nslookup $domain + log_message "INFO" "执行域名解析测试,域名: $domain" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 8) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 网络流量监控 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + # 检查ifstat是否安装 + if ! command -v ifstat &> /dev/null; then + echo -e "${YELLOW}正在安装ifstat...${NC}" + smart_apt_update + sudo apt-get install -y ifstat + fi + + echo -e "${YELLOW}开始网络流量监控...${NC}" + echo -e "${YELLOW}按 Ctrl+C 停止监控${NC}" + ifstat -t + log_message "INFO" "执行网络流量监控" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 9) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看MAC地址 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}网络接口MAC地址:${NC}" + ip link show | grep -E 'ether|link/ether' + log_message "INFO" "查看MAC地址" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 10) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看ARP表 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}ARP表内容:${NC}" + arp -a + log_message "INFO" "查看ARP表" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回网络工具菜单...${NC}" + read -n 1 + ;; + 0) + break + ;; + *) + clear + echo -e "${RED}无效的选择,请重新输入!${NC}" + echo -n -e "${PURPLE}按任意键继续...${NC}" + read -n 1 + ;; + esac + done +} + +# 系统配置子菜单 +system_config() { + while true; do + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 系统配置 ${NC}" + echo -e "${CYAN}==========================================${NC}" + echo -e "${YELLOW}1. 查看CPU使用率${NC}" + echo -e "${YELLOW}2. 查看内存使用率${NC}" + echo -e "${YELLOW}3. 查看磁盘使用率${NC}" + echo -e "${YELLOW}4. 查看系统负载${NC}" + echo -e "${YELLOW}5. 查看登录用户信息${NC}" + echo -e "${YELLOW}6. 查看系统服务状态${NC}" + echo -e "${YELLOW}0. 返回主菜单${NC}" + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}请输入您的选择 [0-6]: ${NC}" + read config_choice + + case $config_choice in + 1) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看CPU使用率 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}CPU使用率 (按 Ctrl+C 停止):${NC}" + top -bn1 | grep "%Cpu(s)" || mpstat + log_message "INFO" "查看CPU使用率" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回系统配置菜单...${NC}" + read -n 1 + ;; + 2) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看内存使用率 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}内存使用情况:${NC}" + free -h + echo -e "\n${YELLOW}详细内存信息:${NC}" + cat /proc/meminfo | head -20 + log_message "INFO" "查看内存使用率" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回系统配置菜单...${NC}" + read -n 1 + ;; + 3) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看磁盘使用率 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}磁盘分区使用率:${NC}" + df -h + echo -e "\n${YELLOW}磁盘I/O统计:${NC}" + iostat -x + log_message "INFO" "查看磁盘使用率" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回系统配置菜单...${NC}" + read -n 1 + ;; + 4) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看系统负载 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}系统负载:${NC}" + uptime + echo -e "\n${YELLOW}详细负载信息:${NC}" + top -bn1 | head -20 + log_message "INFO" "查看系统负载" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回系统配置菜单...${NC}" + read -n 1 + ;; + 5) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看登录用户信息 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}当前登录用户:${NC}" + w + echo -e "\n${YELLOW}用户登录历史:${NC}" + last | head -20 + log_message "INFO" "查看登录用户信息" + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回系统配置菜单...${NC}" + read -n 1 + ;; + 6) + clear + echo -e "${CYAN}==========================================${NC}" + echo -e "${GREEN} 查看系统服务状态 ${NC}" + echo -e "${CYAN}==========================================${NC}" + + echo -e "${YELLOW}1. 查看所有服务状态${NC}" + echo -e "${YELLOW}2. 查看特定服务状态${NC}" + echo -n -e "${PURPLE}请选择 [1-2]: ${NC}" + read service_choice + + case $service_choice in + 1) + echo -e "${YELLOW}所有服务状态:${NC}" + systemctl list-units --type=service --state=running | head -30 + log_message "INFO" "查看所有服务状态" + ;; + 2) + echo -n -e "${PURPLE}请输入服务名称: ${NC}" + read service_name + echo -e "${YELLOW}服务 ${service_name} 状态:${NC}" + systemctl status $service_name + log_message "INFO" "查看服务状态,服务名称: $service_name" + ;; + *) + echo -e "${RED}无效的选择!${NC}" + ;; + esac + + echo -e "${CYAN}==========================================${NC}" + echo -n -e "${PURPLE}按任意键返回系统配置菜单...${NC}" + read -n 1 + ;; + 0) + break + ;; + *) + clear + echo -e "${RED}无效的选择,请重新输入!${NC}" + echo -n -e "${PURPLE}按任意键继续...${NC}" + read -n 1 + ;; + esac + done +} + # 主菜单函数 show_menu() { clear echo -e "${CYAN}==========================================${NC}" - echo -e "${GREEN} Ubuntu 命令行工具 ${NC}" + echo -e "${GREEN} $(__) title ${NC}" echo -e "${CYAN}==========================================${NC}" - echo -e "${PURPLE} 四喜科技 免费提供 ${NC}" - echo -e "${BLUE} 官方网址:https://xi.plus ${NC}" + echo -e "${PURPLE} $(__) copyright ${NC}" + echo -e "${BLUE} $(__) website ${NC}" echo -e "${CYAN}==========================================${NC}" - echo -e "${YELLOW}1. 查看系统信息${NC}" - echo -e "${YELLOW}2. 查看网络信息${NC}" - echo -e "${YELLOW}3. 卸载系统防火墙${NC}" - echo -e "${YELLOW}4. 修改网络最大连接数到最大${NC}" - echo -e "${YELLOW}5. 系统更新${NC}" - echo -e "${YELLOW}6. 安装rinetd${NC}" - echo -e "${YELLOW}7. 重启rinetd${NC}" - echo -e "${YELLOW}8. 停止rinetd${NC}" - echo -e "${YELLOW}9. 配置rinetd${NC}" - echo -e "${YELLOW}0. 退出${NC}" + echo -e "${YELLOW}$(__ menu_1)${NC}" + echo -e "${YELLOW}$(__ menu_2)${NC}" + echo -e "${YELLOW}$(__ menu_3)${NC}" + echo -e "${YELLOW}$(__ menu_4)${NC}" + echo -e "${YELLOW}$(__ menu_5)${NC}" + echo -e "${YELLOW}$(__ menu_6)${NC}" + echo -e "${YELLOW}$(__ menu_7)${NC}" + echo -e "${YELLOW}$(__ menu_8)${NC}" + echo -e "${YELLOW}$(__ menu_9)${NC}" + echo -e "${YELLOW}$(__ menu_10)${NC}" + echo -e "${YELLOW}$(__ menu_11)${NC}" + echo -e "${YELLOW}$(__ menu_12)${NC}" + echo -e "${YELLOW}$(__ menu_0)${NC}" echo -e "${CYAN}==========================================${NC}" - echo -n -e "${PURPLE}请输入您的选择 [0-9]: ${NC}" + echo -n -e "${PURPLE}$(__ prompt_choice) ${NC}" } # 查看系统信息 @@ -111,6 +755,17 @@ modify_max_connections() { read -n 1 } +# 支持的Ubuntu版本列表 +declare -A SUPPORTED_UBUNTU_VERSIONS=( + ["trusty"]="14.04 LTS" + ["xenial"]="16.04 LTS" + ["bionic"]="18.04 LTS" + ["focal"]="20.04 LTS" + ["jammy"]="22.04 LTS" + ["noble"]="24.04 LTS" + ["oracular"]="24.10" +) + # 系统更新功能 system_update() { clear @@ -118,11 +773,31 @@ system_update() { echo -e "${GREEN} 系统更新 ${NC}" echo -e "${CYAN}==========================================${NC}" - # 获取当前系统版本 + # 获取当前系统信息 + OS_NAME=$(lsb_release -i | cut -f2) CURRENT_VERSION=$(lsb_release -c | cut -f2) - echo -e "${YELLOW}当前系统版本: ${CURRENT_VERSION}${NC}" + CURRENT_RELEASE=$(lsb_release -r | cut -f2) - echo -e "${YELLOW}1. 自动选择系统版本更新${NC}" + echo -e "${YELLOW}当前系统: ${OS_NAME} ${CURRENT_RELEASE} (${CURRENT_VERSION})${NC}" + + # 检查是否为Ubuntu系统 + if [ "$OS_NAME" != "Ubuntu" ]; then + echo -e "${RED}警告:当前系统不是Ubuntu,部分功能可能无法正常使用!${NC}" + echo -e "${YELLOW}继续执行更新?(y/n): ${NC}" + read -n 1 confirm + if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then + echo -e "${YELLOW}取消更新。${NC}" + read -n 1 -p "按任意键返回菜单..." + return 1 + fi + fi + + echo -e "${YELLOW}支持的Ubuntu版本:${NC}" + for version in "${!SUPPORTED_UBUNTU_VERSIONS[@]}"; do + echo -e " - ${version}: ${SUPPORTED_UBUNTU_VERSIONS[$version]}" + done + + echo -e "\n${YELLOW}1. 自动选择系统版本更新${NC}" echo -e "${YELLOW}2. 手动选择系统版本更新${NC}" echo -n -e "${PURPLE}请输入您的选择 [1-2]: ${NC}" read update_choice @@ -130,11 +805,24 @@ system_update() { case $update_choice in 1) echo -e "${YELLOW}正在使用当前系统版本 ${CURRENT_VERSION} 进行更新...${NC}" - sudo apt-get update - sudo apt-get upgrade -y - sudo apt-get dist-upgrade -y - sudo apt-get autoremove -y + log_message "INFO" "开始系统更新,当前版本: ${CURRENT_VERSION}" + + # 根据Ubuntu版本选择更新命令 + if [ "$OS_NAME" == "Ubuntu" ] && [[ " ${!SUPPORTED_UBUNTU_VERSIONS[@]} " =~ " $CURRENT_VERSION " ]]; then + sudo apt-get update + sudo apt-get upgrade -y + sudo apt-get dist-upgrade -y + sudo apt-get autoremove -y + else + # 通用更新命令,适用于其他Ubuntu版本或Linux发行版 + sudo apt-get update || sudo apt update + sudo apt-get upgrade -y || sudo apt upgrade -y + sudo apt-get dist-upgrade -y || sudo apt full-upgrade -y + sudo apt-get autoremove -y || sudo apt autoremove -y + fi + echo -e "${GREEN}系统更新完成!${NC}" + log_message "INFO" "系统更新完成" ;; 2) echo -n -e "${PURPLE}请输入要更新的系统版本代号(如:jammy, focal): ${NC}" @@ -142,17 +830,30 @@ system_update() { if [ "$selected_version" == "$CURRENT_VERSION" ]; then echo -e "${YELLOW}正在使用选择的系统版本 ${selected_version} 进行更新...${NC}" + log_message "INFO" "开始系统更新,使用选择的版本: ${selected_version}" + sudo apt-get update sudo apt-get upgrade -y sudo apt-get dist-upgrade -y sudo apt-get autoremove -y + echo -e "${GREEN}系统更新完成!${NC}" + log_message "INFO" "系统更新完成" else - echo -e "${RED}错误:选择的系统版本 ${selected_version} 与当前系统版本 ${CURRENT_VERSION} 不匹配!${NC}" + # 检查选择的版本是否受支持 + if [[ " ${!SUPPORTED_UBUNTU_VERSIONS[@]} " =~ " $selected_version " ]]; then + echo -e "${RED}错误:选择的系统版本 ${selected_version} 与当前系统版本 ${CURRENT_VERSION} 不匹配!${NC}" + echo -e "${YELLOW}当前系统版本: ${CURRENT_VERSION} (${SUPPORTED_UBUNTU_VERSIONS[$CURRENT_VERSION]:-未知版本})${NC}" + echo -e "${YELLOW}选择的版本: ${selected_version} (${SUPPORTED_UBUNTU_VERSIONS[$selected_version]})${NC}" + else + echo -e "${RED}错误:选择的系统版本 ${selected_version} 不是受支持的Ubuntu版本!${NC}" + fi + log_message "ERROR" "选择的系统版本无效: ${selected_version}" fi ;; *) echo -e "${RED}无效的选择!${NC}" + log_message "WARNING" "无效的更新选择: ${update_choice}" ;; esac @@ -168,7 +869,7 @@ install_rinetd() { echo -e "${GREEN} 安装rinetd ${NC}" echo -e "${CYAN}==========================================${NC}" echo -e "${YELLOW}正在安装rinetd...${NC}" - sudo apt-get update + smart_apt_update sudo apt-get install -y rinetd echo -e "${GREEN}rinetd安装完成!${NC}" echo -e "${CYAN}==========================================${NC}" @@ -262,6 +963,10 @@ configure_rinetd() { if [ "$add_to_file" == "y" ] || [ "$add_to_file" == "Y" ]; then echo "$local_ip $local_port $target_domain $target_port" | sudo tee -a /etc/rinetd.conf echo -e "${GREEN}中转配置添加完成!${NC}" + # 配置修改后立即重启rinetd + echo -e "${YELLOW}正在重启rinetd...${NC}" + sudo systemctl restart rinetd + echo -e "${GREEN}rinetd重启完成!${NC}" fi echo -e "${CYAN}==========================================${NC}" @@ -316,6 +1021,10 @@ configure_rinetd() { # 修改配置文件 sudo sed -i "${config_num}c ${new_local_ip} ${new_local_port} ${new_target_domain} ${new_target_port}" /etc/rinetd.conf echo -e "${GREEN}中转配置修改完成!${NC}" + # 配置修改后立即重启rinetd + echo -e "${YELLOW}正在重启rinetd...${NC}" + sudo systemctl restart rinetd + echo -e "${GREEN}rinetd重启完成!${NC}" fi else echo -e "${RED}无效的配置项序号!${NC}" @@ -354,6 +1063,10 @@ configure_rinetd() { # 删除配置行 sudo sed -i "${config_num}d" /etc/rinetd.conf echo -e "${GREEN}中转配置删除完成!${NC}" + # 配置修改后立即重启rinetd + echo -e "${YELLOW}正在重启rinetd...${NC}" + sudo systemctl restart rinetd + echo -e "${GREEN}rinetd重启完成!${NC}" fi else echo -e "${RED}无效的配置项序号!${NC}" @@ -385,41 +1098,64 @@ while true; do case $choice in 1) + log_message "INFO" "用户选择:查看系统信息" view_system_info ;; 2) + log_message "INFO" "用户选择:查看网络信息" view_network_info ;; 3) + log_message "INFO" "用户选择:卸载系统防火墙" uninstall_firewall ;; 4) + log_message "INFO" "用户选择:修改网络最大连接数" modify_max_connections ;; 5) - system_update + log_message "INFO" "用户选择:系统配置" + system_config ;; 6) - install_rinetd + log_message "INFO" "用户选择:系统更新" + system_update ;; 7) - restart_rinetd + log_message "INFO" "用户选择:安装rinetd" + install_rinetd ;; 8) - stop_rinetd + log_message "INFO" "用户选择:重启rinetd" + restart_rinetd ;; 9) + log_message "INFO" "用户选择:停止rinetd" + stop_rinetd + ;; + 10) + log_message "INFO" "用户选择:配置rinetd" configure_rinetd ;; + 11) + log_message "INFO" "用户选择:网络工具" + network_tools + ;; + 12) + log_message "INFO" "用户选择:脚本自动更新" + update_script + ;; 0) + log_message "INFO" "用户选择:退出脚本" clear - echo -e "${GREEN}感谢使用,再见!${NC}" + echo -e "${GREEN}$(__ thank_you)${NC}" exit 0 ;; *) + log_message "WARNING" "用户输入无效选择:$choice" clear - echo -e "${RED}无效的选择,请重新输入!${NC}" - echo -n -e "${PURPLE}按任意键继续...${NC}" + echo -e "${RED}$(__ invalid_choice)${NC}" + echo -n -e "${PURPLE}$(__ please_continue)${NC}" read -n 1 ;; esac