欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

shell脚本实现收集Mac的基本电脑信息保存到文本

程序员文章站 2022-05-10 16:47:30
...

苹果又更新系统啦!每年都要升级一次!不过不是所有的电脑都能升级哦,很多电脑用的时间久了就会出现越来越慢,或者卡顿,甚至运行应用都困难。那怎么办呢?

看看自己的存储空间是不是快满了?资源占用在各个程序之间是否合理?有没有异常的进程?system目录下的缓存文件太多了?

针对需要对Mac电脑进行简单的快速了解,又不想点点点,那就只有指定收集一些信息来看看啦!


#!/bin/bash

# MACOS INFORMATION GET
# Script to gather resource and health information on Mac computers. 
# 




# Set the relative directory so script and addons can run without cd
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Info_DIR=~/Desktop/Information-$(date '+%k-%M-%S')
mkdir $Info_DIR

# Lets populate the basic hardware info
DEVICE_PROFILE=$(/usr/sbin/system_profiler SPHardwareDataType)
DEVICE_SERIAL=$(ioreg -c "IOPlatformExpertDevice" | awk -F '"' '/IOPlatformSerialNumber/ {print $4}')
DEVICE_MAC=$(networksetup -getmacaddress Wi-Fi | awk -F ' ' '{print $3}')
DEVICE_RAM=$( echo "$DEVICE_PROFILE" | awk -F ' ' '/Memory/ {print $2 " " $3}' )
DEVICE_CPU_SPEED=$( echo "$DEVICE_PROFILE" | awk -F ' ' '/Processor Speed/ {for (i=3; i<NF; i++) printf $i " "; print $NF}' )
DEVICE_CPU_BRAND=$( echo "$DEVICE_PROFILE" | awk -F ' ' '/Processor Name/ {for (i=3; i<NF; i++) printf $i " "; print $NF}' )
DEVICE_BATTERY_CYCLES=$( system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}' )
DEVICE_BATTERY_HEALTH=$( ioreg -l | awk '$3~/Capacity/{c[$3]=$5}END{OFMT="%.3f";max=c["\"DesignCapacity\""];print(max>0?100*c["\"MaxCapacity\""]/max:"?")}' )
DEVICE_STORAGE_CAPACITY=$( diskutil info /dev/disk0 | grep "Disk Size" | awk {'print $3 " " $4'} )

# Which os version are we running
OS_PRODUCT_VERSION=$(sw_vers -productVersion)
OS_VERSION=( ${OS_PRODUCT_VERSION//./ } )
OS_VERSION_MAJOR="${OS_VERSION[0]}"
OS_VERSION_MINOR="${OS_VERSION[1]}"
OS_VERSION_PATCH="${OS_VERSION[2]}"
OS_VERSION_BUILD=$(sw_vers -buildVersion)

# Now we'll fetch software details
DEVICE_NAME=$(scutil --get ComputerName)
FILEVAULT_STATUS=$(fdesetup status | awk '{print substr($NF, 1, length($NF)-1)}')


# Determine the model and year
DEVICE_MODEL=$( curl -s https://support-sp.apple.com/sp/product?cc=` echo $DEVICE_SERIAL | cut -c 9-` | sed 's|.*<configCode>\(.*\)</configCode>.*|\1|' )
DEVICE_YEAR=$( echo "$DEVICE_MODEL" | grep -o -E '[0-9][0-9][0-9][0-9]' )

# Formatting variables
FORMATTING_HR=$( echo "===============================================" )
FORMATTING_DATE=$( date )

# Let's print out the Mac profile
echo $FORMATTING_HR  >> $Info_DIR/Mac_info.txt
echo "DEVICE REPORT - $FORMATTING_DATE"  >> $Info_DIR/Mac_info.txt
echo $FORMATTING_HR  >> $Info_DIR/Mac_info.txt
echo "Name: $DEVICE_NAME"  >> $Info_DIR/Mac_info.txt
echo "Serial: $DEVICE_SERIAL"  >> $Info_DIR/Mac_info.txt
echo "MAC (WiFi): $DEVICE_MAC"  >> $Info_DIR/Mac_info.txt
echo "Model: $DEVICE_MODEL"  >> $Info_DIR/Mac_info.txt
echo "Year: $DEVICE_YEAR"  >> $Info_DIR/Mac_info.txt
echo "CPU Speed: $DEVICE_CPU_SPEED"  >> $Info_DIR/Mac_info.txt
echo "CPU Type: $DEVICE_CPU_BRAND"  >> $Info_DIR/Mac_info.txt
echo "CPU Full: $DEVICE_CPU_SPEED $DEVICE_CPU_BRAND"  >> $Info_DIR/Mac_info.txt
echo "RAM: $DEVICE_RAM"  >> $Info_DIR/Mac_info.txt
echo "Storage: $DEVICE_STORAGE_CAPACITY"  >> $Info_DIR/Mac_info.txt
if [ $DEVICE_BATTERY_CYCLES ] ; then
  # If a battery was found we'll show the cycles and health
  echo "Battery Cycles: $DEVICE_BATTERY_CYCLES"  >> $Info_DIR/Mac_info.txt
  echo "Battery Health: $DEVICE_BATTERY_HEALTH"  >> $Info_DIR/Mac_info.txt
fi
echo "macOS Version: ${OS_VERSION_MAJOR}.${OS_VERSION_MINOR}.${OS_VERSION_PATCH}+${OS_VERSION_BUILD}"  >> $Info_DIR/Mac_info.txt
echo "FileVault: $FILEVAULT_STATUS"  >> $Info_DIR/Mac_info.txt

echo $FORMATTING_HR  >> $Info_DIR/Mac_info.txt



echo "Disk Partition of free space available : "  >> $Info_DIR/Mac_info.txt
df -h  >> $Info_DIR/Mac_info.txt
echo $FORMATTING_HR  >> $Info_DIR/Mac_info.txt

echo "systemstats : " >> $Info_DIR/Systemstats.txt
systemstats >> $Info_DIR/Systemstats.txt
echo $FORMATTING_HR >> $Info_DIR/Systemstats.txt

echo "ps user : " >> $Info_DIR/Ps_user.txt
echo \"`ps -axo user,pid,ppid,%cpu,%mem,start,time,command` >> $Info_DIR/Ps_user.txt
echo $FORMATTING_HR >> $Info_DIR/Ps_user.txt

if [ -f "/var/log/system.log" ]
then
    cp /var/log/system.log $Info_DIR
fi

if [ -f "/var/log/system.log" ]
then
    cp /var/log/install.log $Info_DIR
fi


# Compress folder
echo "Compressing folder..."


zip -r -X "$Info_DIR".zip "$Info_DIR" > /dev/null

# If compression was successful, remove the original folder
if [ $? -eq "0" ]
then
    rm -rf "$Info_DIR"
    printf "\r\n"
    echo "The System Report has been placed in $Info_DIR.zip"
else
    printf "\r\n"
    echo "Compression Failed!"
    printf "\r\n"
    echo "The System Report has been placed in $Info_DIR"
fi







输出:

s-iMac-4:~ iobit$ sh /Users/io/Documents/10.11/information\ collect/SYSTEM_INFO_GET.sh 
Compressing folder...

The System Report has been placed in /Users/io/Desktop/Information-14-46-46.zip
s-iMac-4:~ iobit$ 

这样就在桌面生成了一个以当前时间命名的压缩文件,里面包含前面收集到的信息。

shell脚本实现收集Mac的基本电脑信息保存到文本

 

其中Mac_info.txt的内容是这样的:

===============================================
DEVICE REPORT - Wed Jun  5 14:46:48 CST 2019
===============================================
Name: IObit’s iMac (3)
Serial: C02K666666666
MAC (WiFi): ec:35:86:56:b6:06
Model: iMac (21.5-inch, Late 2012)
Year: 2012
CPU Speed: 2.7 GHz
CPU Type: Intel Core i5
CPU Full: 2.7 GHz Intel Core i5
RAM: 8 GB
Storage: 240.1 GB
macOS Version: 10.14.5+18F132
FileVault: Off
===============================================
Disk Partition of free space available : 
Filesystem      Size   Used  Avail Capacity iused               ifree %iused  Mounted on
/dev/disk1s1   223Gi  112Gi  108Gi    51% 2242414 9223372036852533393    0%   /
devfs          188Ki  188Ki    0Bi   100%     650                   0  100%   /dev
/dev/disk1s4   223Gi  3.0Gi  108Gi     3%       2 9223372036854775805    0%   /private/var/vm
/dev/disk1s5   223Gi  1.5Mi  108Gi     1%     292 9223372036854775515    0%   /Volumes/test
map -hosts       0Bi    0Bi    0Bi   100%       0                   0  100%   /net
map auto_home    0Bi    0Bi    0Bi   100%       0                   0  100%   /home
===============================================

其它就不再举例了,这样,我们就比较了解电脑的情况了,去寻找到底是始末占用了我的电脑资源呢?