【收集电脑信息】collect_info.sh

收集电脑信息

collect_info.sh

bash 复制代码
#!/bin/bash

output="info.txt"
> "$output"

# 1. OS Version
echo "=== 操作系统名称及版本 ===" >> "$output"
lsb_release -d | cut -f2- >> "$output"
echo -e "\n" >> "$output"

# 2. Installation Time
echo "=== 系统安装时间 ===" >> "$output"
ROOT_DEVICE=$(df -h | grep "/dev/root" | awk '{print $1}')
if [ -z "$ROOT_DEVICE" ]; then
    ROOT_DEVICE=$(mount | grep " / " | awk '{print $1}')
fi

if [ -n "$ROOT_DEVICE" ]; then
    fs_create_time=$(sudo tune2fs -l "$ROOT_DEVICE" 2>/dev/null | grep "Filesystem created" | cut -d ':' -f2-)
    if [ -n "$fs_create_time" ]; then
        echo "$fs_create_time" >> "$output"
    else
        echo "无法获取系统安装时间(tune2fs 失败)" >> "$output"
    fi
else
    echo "无法识别根分区设备" >> "$output"
fi
echo -e "\n" >> "$output"

# 3. Disk Info
echo "=== 硬盘信息 ===" >> "$output"

for disk in $(ls /dev/sd* | grep -v "[0-9]" 2>/dev/null); do
    model=$(sudo hdparm -I "$disk" 2>/dev/null | grep "Model Number" | sed 's/.*: //; s/^[ \t]*//')
    serial=$(sudo hdparm -I "$disk" 2>/dev/null | grep "Serial Number" | sed 's/.*: //; s/^[ \t]*//')
    echo "设备: $disk" >> "$output"
    echo "型号: ${model:-N/A}" >> "$output"
    echo "序列号: ${serial:-N/A}" >> "$output"
    echo "-----------------------------" >> "$output"
done

if command -v nvme &> /dev/null; then
    for dev in $(nvme list | awk '/dev\/nvme/{print $1}' 2>/dev/null); do
        sn=$(sudo nvme id-ctrl "$dev" | grep -i "sn" | head -1 | awk '{print $3}' | tr -d '"')
        mn=$(sudo nvme id-ctrl "$dev" | grep -i "mn" | head -1 | awk '{print substr($0, index($0,$3))}' | tr -d '"')
        echo "设备: $dev" >> "$output"
        echo "型号: ${mn:-N/A}" >> "$output"
        echo "序列号: ${sn:-N/A}" >> "$output"
        echo "-----------------------------" >> "$output"
    done
else
    echo "未安装 nvme-cli,请安装:sudo apt install nvme-cli" >> "$output"
fi

if ! ls /dev/sd* > /dev/null 2>&1 && ! (command -v nvme &> /dev/null && nvme list > /dev/null 2>&1); then
    echo "未检测到硬盘设备。" >> "$output"
fi
echo -e "\n" >> "$output"

# 4. Network MAC Addresses
echo "=== 网卡 MAC 地址 ===" >> "$output"

# === 网卡 MAC 地址 ===
echo "=== 网卡 MAC 地址 ===" >> "$output"

eth_detected=0
wifi_detected=0

ip link show | awk '
BEGIN {
    OFS=": "
}
$0 ~ /state UP/ {
    getline
    if ($0 ~ /ether/) {
        mac = $2
        sub(/.*</, "", $1)
        sub(/>:.*/, "", $1)
        interface = $1

        # 根据接口名判断类型(简单规则)
        if (interface ~ /^en/) {
            print "有线网卡 ("interface"): "mac
            eth_detected = 1
        } else if (interface ~ /^wl/) {
            print "无线网卡 ("interface"): "mac
            wifi_detected = 1
        } else {
            print "未知网卡 ("interface"): "mac
        }
    }
}' >> "$output"

# 提示未检测到的情况
if [[ "$eth_detected" -ne 1 ]]; then
    echo "未检测到有线网卡。" >> "$output"
fi

if [[ "$wifi_detected" -ne 1 ]]; then
    echo "未检测到无线网卡。" >> "$output"
fi