编辑代码

#!/bin/bash

# 脚本版本
sh_v="1.0.0"

# 字体颜色
huang='\033[33m'
bai='\033[0m'
kjlan='\033[96m'
lv='\033[0;32m'

# 复制将当前目录下的 vps.sh 文件复制到 /usr/local/bin 目录,并将其重命名为 k。
# 复制过程中所有的输出信息和错误信息都被重定向到 /dev/null,因此不会在终端显示任何输出。这通常用于静默执行命令,避免输出干扰。
cp ./vps.sh /usr/local/bin/k > /dev/null 2>&1

# 获取服务器 IPV4、IPV6 公网地址
ip_address() {
  ipv4_address=$(curl -s ipv4.ip.sb)
  ipv6_address=$(curl -s --max-time 1 ipv6.ip.sb)
}

# 获取服务器流量统计状态,格式化输出(单位保留 GB)
output_status() {
  output=$(awk 'BEGIN { rx_total = 0; tx_total = 0 }
		NR > 2 { rx_total += $2; tx_total += $10 }
		END {
			rx_units = "Bytes";
			tx_units = "Bytes";
			if (rx_total > 1024) { rx_total /= 1024; rx_units = "KB"; }
			if (rx_total > 1024) { rx_total /= 1024; rx_units = "MB"; }
			if (rx_total > 1024) { rx_total /= 1024; rx_units = "GB"; }

			if (tx_total > 1024) { tx_total /= 1024; tx_units = "KB"; }
			if (tx_total > 1024) { tx_total /= 1024; tx_units = "MB"; }
			if (tx_total > 1024) { tx_total /= 1024; tx_units = "GB"; }

			printf("总接收: %.2f %s\n总发送: %.2f %s\n", rx_total, rx_units, tx_total, tx_units);
		}' /proc/net/dev)
}

# 系统更新
linux_update() {
    # Update system on Debian-based systems
    if [ -f "/etc/debian_version" ]; then
				apt update -y && DEBIAN_FRONTEND=noninteractive apt full-upgrade -y
    fi

    # Update system on Red Hat-based systems
    if [ -f "/etc/redhat-release" ]; then
				yum -y update
    fi

    # Update system on Alpine Linux
    if [ -f "/etc/alpine-release" ]; then
        apk update && apk upgrade
    fi

}

# 任意键返回上一步
break_end() {
	echo -e "${lv}操作完成${bai}"
	echo "按任意键继续..."
	read -n 1 -s -r -p ""
	echo ""
	clear
}

while true; do
clear

echo -e "${kjlan}_  _ ____  _ _ _    _ ____ _  _ "
echo "|_/  |___  | | |    | |  | |\ | "
echo "| \_ |___ _| | |___ | |__| | \| "
echo "                                "
echo -e "${kjlan}科技lion一键脚本工具 v$sh_v (支持Ubuntu/Debian/CentOS/Alpine系统)${bai}"
echo -e "${kjlan}-输入${huang}k${kjlan}可快速启动此脚本-${bai}"
echo "------------------------"
echo "1. 系统信息查询"
echo "2. 系统更新"
echo "3. 系统清理"
echo "------------------------"
echo "00. 脚本更新"
echo "------------------------"
echo "0. 退出脚本"
echo "------------------------"
read -p "请输入你的选择: " choice

case $choice in
  # 信息系统查询
  1)
    clear
    # 执行函数:获取 IPv4 和 IPv6 地址
    ip_address

		# ------------------------
		# 获取主机名
		hostname=$(hostname)

		# 获取服务器 IP 地址所属的组织(如 ISP 或公司)(包含了关于 IP 地址的详细信息,包括组织、位置、ASN 等)
		isp_info=$(curl -s ipinfo.io/org)

		# ------------------------
		# 尝试使用 lsb_release 获取系统信息
		os_info=$(lsb_release -ds 2>/dev/null)
		# 如果 lsb_release 命令失败,则尝试其它方法
		if [ -z "$os_info" ]; then
			# 检查常见的发行文件
			if [ -f "/etc/os-release" ]; then
				os_info=$(source /etc/os-release && echo "$PRETTY_NAME")
				elif [ -f "/etc/debian_version" ]; then
					os_info="Debian $(cat /etc/debian_version)"
				elif [ -f "/etc/redhat-release" ]; then
					os_info=$(cat /etc/redhat-release)
				else
					os_info="Unknown"
				fi
			fi

		# 获取 Linux 版本
		kernel_version=$(uname -r)

		# ------------------------
		# 获取 CPU 架构
		cpu_arch=$(uname -m)

		# 获取 CPU 型号
    if [ "$(uname -m)" == "x86_64" ]; then
				cpu_info=$(cat /proc/cpuinfo | grep 'model name' | uniq | sed -e 's/model name[[:space:]]*: //')
		else
				cpu_info=$(lscpu | grep 'BIOS Model name' | awk -F': ' '{print $2}' | sed 's/^[ \t]*//')
		fi

		# 获取 CPU 核心数
		cpu_cores=$(nproc)

		# ------------------------
		# CPU 占用率
		if [ -f /etc/alpine-release ]; then
				# Alpine Linux 使用以下命令获取 CPU 使用率
				cpu_usage_percent=$(top -bn1 | grep '^CPU' | awk '{print " "$4}' | cut -c 1-2)
		else
				# 其他系统使用以下命令获取 CPU 使用率
				cpu_usage_percent=$(top -bn1 | grep "Cpu(s)" | awk '{print " "$2}')
		fi

		# 获取物理内存
		mem_info=$(free -b | awk 'NR==2{printf "%.2f/%.2f MB (%.2f%%)", $3/1024/1024, $2/1024/1024, $3*100/$2}')

		# 获取虚拟物理内存
		swap_used=$(free -m | awk 'NR==3{print $3}')
		swap_total=$(free -m | awk 'NR==3{print $2}')

		if [ "$swap_total" -eq 0 ]; then
				swap_percentage=0
		else
				swap_percentage=$((swap_used * 100 / swap_total))
		fi

		swap_info="${swap_used}MB/${swap_total}MB (${swap_percentage}%)"

		# 获取硬盘占用
		disk_info=$(df -h | awk '$NF=="/"{printf "%s/%s (%s)", $3, $2, $5}')

		# ------------------------
		# 获取网络拥堵算法
		congestion_algorithm=$(sysctl -n net.ipv4.tcp_congestion_control)
		queue_algorithm=$(sysctl -n net.core.default_qdisc)

		# ------------------------
		# 获取地理位置 / 城市
		country=$(curl -s ipinfo.io/country)
		city=$(curl -s ipinfo.io/city)

		# 获取系统时间
		current_time=$(date "+%Y-%m-%d %I:%M %p")

		# 执行函数:
    output_status

    echo ""
    echo "系统信息查询"
    echo "------------------------"
    echo "主机名:$hostname"
    echo "运营商:$isp_info"
    echo "------------------------"
		echo "系统版本: $os_info"
		echo "Linux版本: $kernel_version"
		echo "------------------------"
		echo "CPU架构: $cpu_arch"
		echo "CPU型号: $cpu_info"
		echo "CPU核心数: $cpu_cores"
		echo "------------------------"
		echo "CPU占用: $cpu_usage_percent%"
		echo "物理内存: $mem_info"
		echo "虚拟内存: $swap_info"
		echo "硬盘占用: $disk_info"
    echo "------------------------"
    echo "$output"
    echo "------------------------"
    echo "网络拥堵算法: $congestion_algorithm $queue_algorithm"
    echo "------------------------"
    echo "公网IPv4地址: $ipv4_address"
    echo "公网IPv6地址: $ipv6_address"
    echo "------------------------"
    echo "地理位置: $country $city"
		echo "系统时间: $current_time"
    echo "------------------------"
  ;;

  # 退出脚本
  0)
	clear
	exit
	;;

*)
	echo "无效的输入!"
	;;
esac
	break_end
done