Files
linux-tools/linux_tool.sh
2025-12-20 15:00:12 +08:00

426 lines
17 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 版权信息
# 四喜科技 免费提供
# 官方网址https://xi.plus
# 定义颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# 清除屏幕
clear
# 主菜单函数
show_menu() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} Ubuntu 命令行工具 ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${PURPLE} 四喜科技 免费提供 ${NC}"
echo -e "${BLUE} 官方网址https://xi.plus ${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 "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}请输入您的选择 [0-9]: ${NC}"
}
# 查看系统信息
view_system_info() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 系统信息 ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${YELLOW}操作系统:${NC} $(lsb_release -d | cut -f2)"
echo -e "${YELLOW}内核版本:${NC} $(uname -r)"
echo -e "${YELLOW}CPU信息:${NC} $(lscpu | grep 'Model name' | cut -f2 -d:)"
echo -e "${YELLOW}内存信息:${NC} $(free -h | grep Mem | awk '{print $2}')"
echo -e "${YELLOW}磁盘信息:${NC} $(df -h | grep '/dev/' | grep -v tmpfs)"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 查看网络信息
view_network_info() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 网络信息 ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${YELLOW}IP地址:${NC} $(ip addr show | grep inet | grep -v 127.0.0.1 | cut -d' ' -f6 | cut -d'/' -f1)"
echo -e "${YELLOW}网关:${NC} $(ip route | grep default | cut -d' ' -f3)"
echo -e "${YELLOW}DNS服务器:${NC} $(cat /etc/resolv.conf | grep nameserver | cut -d' ' -f2)"
echo -e "${YELLOW}网络接口:${NC} $(ip link show | grep UP | grep -v lo | cut -d':' -f2)"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 卸载系统防火墙
uninstall_firewall() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 卸载系统防火墙 ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${YELLOW}正在卸载ufw...${NC}"
sudo apt-get remove -y ufw
sudo apt-get purge -y ufw
echo -e "${GREEN}防火墙卸载完成!${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 修改网络最大连接数到最大
modify_max_connections() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 修改网络最大连接数 ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${YELLOW}正在修改网络最大连接数...${NC}"
# 修改系统限制
echo "* soft nofile 65535" | sudo tee -a /etc/security/limits.conf
echo "* hard nofile 65535" | sudo tee -a /etc/security/limits.conf
# 修改sysctl配置
echo "net.core.somaxconn = 65535" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_max_syn_backlog = 65535" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_max_tw_buckets = 65535" | sudo tee -a /etc/sysctl.conf
# 应用sysctl配置
sudo sysctl -p
echo -e "${GREEN}网络最大连接数修改完成!${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 系统更新功能
system_update() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 系统更新 ${NC}"
echo -e "${CYAN}==========================================${NC}"
# 获取当前系统版本
CURRENT_VERSION=$(lsb_release -c | cut -f2)
echo -e "${YELLOW}当前系统版本: ${CURRENT_VERSION}${NC}"
echo -e "${YELLOW}1. 自动选择系统版本更新${NC}"
echo -e "${YELLOW}2. 手动选择系统版本更新${NC}"
echo -n -e "${PURPLE}请输入您的选择 [1-2]: ${NC}"
read update_choice
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
echo -e "${GREEN}系统更新完成!${NC}"
;;
2)
echo -n -e "${PURPLE}请输入要更新的系统版本代号jammy, focal: ${NC}"
read selected_version
if [ "$selected_version" == "$CURRENT_VERSION" ]; then
echo -e "${YELLOW}正在使用选择的系统版本 ${selected_version} 进行更新...${NC}"
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
echo -e "${GREEN}系统更新完成!${NC}"
else
echo -e "${RED}错误:选择的系统版本 ${selected_version} 与当前系统版本 ${CURRENT_VERSION} 不匹配!${NC}"
fi
;;
*)
echo -e "${RED}无效的选择!${NC}"
;;
esac
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 安装rinetd
install_rinetd() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 安装rinetd ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${YELLOW}正在安装rinetd...${NC}"
sudo apt-get update
sudo apt-get install -y rinetd
echo -e "${GREEN}rinetd安装完成${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 重启rinetd
restart_rinetd() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 重启rinetd ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${YELLOW}正在重启rinetd...${NC}"
sudo systemctl restart rinetd
echo -e "${GREEN}rinetd重启完成${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 停止rinetd
stop_rinetd() {
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 停止rinetd ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -e "${YELLOW}正在停止rinetd...${NC}"
sudo systemctl stop rinetd
echo -e "${GREEN}rinetd停止完成${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回菜单...${NC}"
read -n 1
}
# 配置rinetd
configure_rinetd() {
while true; do
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 配置rinetd ${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}0. 返回主菜单${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}请输入您的选择 [0-4]: ${NC}"
read rinetd_choice
case $rinetd_choice in
1)
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 当前中转配置 ${NC}"
echo -e "${CYAN}==========================================${NC}"
if [ -f /etc/rinetd.conf ]; then
echo -e "${YELLOW}配置内容:${NC}"
cat /etc/rinetd.conf
else
echo -e "${RED}rinetd配置文件不存在${NC}"
fi
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回rinetd配置菜单...${NC}"
read -n 1
;;
2)
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 添加中转配置 ${NC}"
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}输入本地IP默认0.0.0.0: ${NC}"
read local_ip
local_ip=${local_ip:-0.0.0.0}
echo -n -e "${PURPLE}输入本地端口: ${NC}"
read local_port
echo -n -e "${PURPLE}输入目标域名: ${NC}"
read target_domain
echo -n -e "${PURPLE}输入目标端口: ${NC}"
read target_port
echo -n -e "${PURPLE}是否添加到配置文件?(y/n默认y): ${NC}"
read add_to_file
add_to_file=${add_to_file:-y}
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}"
fi
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回rinetd配置菜单...${NC}"
read -n 1
;;
3)
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 修改中转配置 ${NC}"
echo -e "${CYAN}==========================================${NC}"
if [ -f /etc/rinetd.conf ]; then
# 显示当前配置并编号
echo -e "${YELLOW}当前配置:${NC}"
sudo awk '{print NR ". " $0}' /etc/rinetd.conf
echo -n -e "${PURPLE}输入要修改的配置项序号: ${NC}"
read config_num
# 获取当前配置行
current_config=$(sudo sed -n "${config_num}p" /etc/rinetd.conf)
if [ -n "$current_config" ]; then
# 解析当前配置
current_local_ip=$(echo $current_config | awk '{print $1}')
current_local_port=$(echo $current_config | awk '{print $2}')
current_target_domain=$(echo $current_config | awk '{print $3}')
current_target_port=$(echo $current_config | awk '{print $4}')
echo -n -e "${PURPLE}输入新的本地IP默认$current_local_ip: ${NC}"
read new_local_ip
new_local_ip=${new_local_ip:-$current_local_ip}
echo -n -e "${PURPLE}输入新的本地端口(默认$current_local_port: ${NC}"
read new_local_port
new_local_port=${new_local_port:-$current_local_port}
echo -n -e "${PURPLE}输入新的目标域名(默认$current_target_domain: ${NC}"
read new_target_domain
new_target_domain=${new_target_domain:-$current_target_domain}
echo -n -e "${PURPLE}输入新的目标端口(默认$current_target_port: ${NC}"
read new_target_port
new_target_port=${new_target_port:-$current_target_port}
echo -n -e "${PURPLE}是否保存修改?(y/n默认y): ${NC}"
read save_changes
save_changes=${save_changes:-y}
if [ "$save_changes" == "y" ] || [ "$save_changes" == "Y" ]; then
# 修改配置文件
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}"
fi
else
echo -e "${RED}无效的配置项序号!${NC}"
fi
else
echo -e "${RED}rinetd配置文件不存在${NC}"
fi
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回rinetd配置菜单...${NC}"
read -n 1
;;
4)
clear
echo -e "${CYAN}==========================================${NC}"
echo -e "${GREEN} 删除中转配置 ${NC}"
echo -e "${CYAN}==========================================${NC}"
if [ -f /etc/rinetd.conf ]; then
# 显示当前配置并编号
echo -e "${YELLOW}当前配置:${NC}"
sudo awk '{print NR ". " $0}' /etc/rinetd.conf
echo -n -e "${PURPLE}输入要删除的配置项序号: ${NC}"
read config_num
# 检查配置项是否存在
current_config=$(sudo sed -n "${config_num}p" /etc/rinetd.conf)
if [ -n "$current_config" ]; then
echo -n -e "${PURPLE}是否删除该配置项?(y/n默认y): ${NC}"
read delete_config
delete_config=${delete_config:-y}
if [ "$delete_config" == "y" ] || [ "$delete_config" == "Y" ]; then
# 删除配置行
sudo sed -i "${config_num}d" /etc/rinetd.conf
echo -e "${GREEN}中转配置删除完成!${NC}"
fi
else
echo -e "${RED}无效的配置项序号!${NC}"
fi
else
echo -e "${RED}rinetd配置文件不存在${NC}"
fi
echo -e "${CYAN}==========================================${NC}"
echo -n -e "${PURPLE}按任意键返回rinetd配置菜单...${NC}"
read -n 1
;;
0)
break
;;
*)
echo -e "${RED}无效的选择!${NC}"
echo -n -e "${PURPLE}按任意键继续...${NC}"
read -n 1
;;
esac
done
}
# 主循环
while true; do
show_menu
read choice
case $choice in
1)
view_system_info
;;
2)
view_network_info
;;
3)
uninstall_firewall
;;
4)
modify_max_connections
;;
5)
system_update
;;
6)
install_rinetd
;;
7)
restart_rinetd
;;
8)
stop_rinetd
;;
9)
configure_rinetd
;;
0)
clear
echo -e "${GREEN}感谢使用,再见!${NC}"
exit 0
;;
*)
clear
echo -e "${RED}无效的选择,请重新输入!${NC}"
echo -n -e "${PURPLE}按任意键继续...${NC}"
read -n 1
;;
esac
done