【收集电脑信息】collect_info.sh
收集电脑信息
collect_info.sh
#!/bin/bashoutput="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" ]; thenROOT_DEVICE=$(mount | grep " / " | awk '{print $1}')
fiif [ -n "$ROOT_DEVICE" ]; thenfs_create_time=$(sudo tune2fs -l "$ROOT_DEVICE" 2>/dev/null | grep "Filesystem created" | cut -d ':' -f2-)if [ -n "$fs_create_time" ]; thenecho "$fs_create_time" >> "$output"elseecho "无法获取系统安装时间(tune2fs 失败)" >> "$output"fi
elseecho "无法识别根分区设备" >> "$output"
fi
echo -e "\n" >> "$output"# 3. Disk Info
echo "=== 硬盘信息 ===" >> "$output"for disk in $(ls /dev/sd* | grep -v "[0-9]" 2>/dev/null); domodel=$(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"
doneif command -v nvme &> /dev/null; thenfor dev in $(nvme list | awk '/dev\/nvme/{print $1}' 2>/dev/null); dosn=$(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
elseecho "未安装 nvme-cli,请安装:sudo apt install nvme-cli" >> "$output"
fiif ! ls /dev/sd* > /dev/null 2>&1 && ! (command -v nvme &> /dev/null && nvme list > /dev/null 2>&1); thenecho "未检测到硬盘设备。" >> "$output"
fi
echo -e "\n" >> "$output"# 4. Network MAC Addresses
echo "=== 网卡 MAC 地址 ===" >> "$output"# === 网卡 MAC 地址 ===
echo "=== 网卡 MAC 地址 ===" >> "$output"eth_detected=0
wifi_detected=0ip link show | awk '
BEGIN {OFS=": "
}
$0 ~ /state UP/ {getlineif ($0 ~ /ether/) {mac = $2sub(/.*</, "", $1)sub(/>:.*/, "", $1)interface = $1# 根据接口名判断类型(简单规则)if (interface ~ /^en/) {print "有线网卡 ("interface"): "maceth_detected = 1} else if (interface ~ /^wl/) {print "无线网卡 ("interface"): "macwifi_detected = 1} else {print "未知网卡 ("interface"): "mac}}
}' >> "$output"# 提示未检测到的情况
if [[ "$eth_detected" -ne 1 ]]; thenecho "未检测到有线网卡。" >> "$output"
fiif [[ "$wifi_detected" -ne 1 ]]; thenecho "未检测到无线网卡。" >> "$output"
fi