[指令碼例項]——統計系統資訊指令碼

Jelly_lyj發表於2017-03-18

 

#!/bin/bash
#title:統計系統資訊
#author:Jelly_lyj
#date:2016-08-05
#version:V0.01
#other:此指令碼僅用於Linux系統的系統資訊統計

#===================================================== 
#Fuction System_info()-->收集和輸出 系統|硬體相關資訊
#=====================================================
System_info()
{
#顯示系統版本號
  System_version=`cat /etc/redhat-release |awk '{print $1,$2,$7}'`  
#核心版本
  System_kernel=`uname -r`
#系統物理cpu個數和各個cpu有幾個核
  Sytem_cpu=`cat /proc/cpuinfo | grep "physical id"|sort|uniq -c|awk '{print "CPU:"$2,$NF,"Core:"$1}'`
#機器型號 
   System_machine_model=`dmidecode | grep "Product Name" |sed -n '1p'|sed 's/[\t]//g'`
#CPU型號
   System_cpu_model=`cat /proc/cpuinfo |egrep "model name"|uniq|awk '{print$4" "$5}'`    
#顯示卡型號
   System__gpu_model=`lspci|grep VGA |awk '{print $5" "$6" "$7" "$8" "$9}'`

echo "=============System_info==============="
echo "系統使用的Linux系統版本是:$System_version"
echo "系統的核心版本是:$System_kernel"
echo -e "系統cpu個數與核數是:\n$Sytem_cpu"
echo "各個硬體型號:"
echo "  (1)機器型號:$System_machine_model"
echo "  (2)CPU型號:$System_cpu_model"
echo "  (3)顯示卡型號:$System__gpu_model"
echo "=============System_info==============="
}  


#==================================== 
#Fuction Disk_info() 收集硬碟|分割槽資訊
#====================================
Disk_info()
{
#使用/tmp/disk.txt檔案來存放各個硬碟名稱
[ -f /tmp/disk.txt ]
if [ $? -eq 0 ]
then
   echo -n "">/tmp/disk.txt
   fdisk -l |egrep "Disk /dev"|awk '{print $2}'|awk -F: '{print$1}' >/tmp/disk.txt
else
   touch /tmp/disk.txt
   fdisk -l |egrep "Disk /dev"|awk '{print $2}'|awk -F: '{print$1}' >/tmp/disk.txt   
fi

Disk_count=`fdisk -l |egrep "Disk /dev"|wc -l`
echo "=============Disk_info==============="
echo "本機共有$Disk_count塊硬碟"
while read disk
do
  Disk_DX=`fdisk -l $disk 2>/dev/null | egrep "Disk */" |awk '{print $2$3$4}'|sed -e 's/:/=/g' -e s/,//g`
  Disk_FQ=`fdisk -l $disk |egrep "^/dev"|wc -l`  
  echo "  其中$Disk_DX,有$Disk_FQ個分割槽"
done < /tmp/disk.txt
echo "=============Disk_info==============="
}


#==================================== 
#Fuction Mem_info()--->收集記憶體資訊
#====================================
Mem_info()
{
#分別提取出記憶體的total、used、free
total=`free |egrep "^Mem"|awk '{print $2}'`
used=`free |egrep "^Mem"|awk '{print $3}'`
free=`free |egrep "^Mem"|awk '{print $4}'`
#利用bc按照單位為GB的格式輸出
total_G=`echo "scale=1;$total/1000000"|bc`
used_G=`echo "scale=1;$used/1000000" |bc`
free_G=`echo "scale=1;$free/1000000" |bc`

echo "=============Mem_info==============="
echo  "Mem total:$total_G G" 
echo  "Mem used:$used_G G" 
echo  "Mem free:$free_G G"  
echo "=============Mem_info==============="
}


#==================================== 
#Fuction Os_info() 收集作業系統資訊
#====================================
Os_info()
{
#當前系統執行級別
 Os_runlevel=`runlevel |awk '{print$2}'`
#當前系統登入使用者
 Os_user=`whoami`
echo "=============Os_info==============="
echo "當前執行的系統級別是:$Os_runlevel"
echo "當前系統登入使用者:$Os_user"
echo "=============Os_info==============="
}


#========================================= 
#Fuction Network_info() 收集作業系統資訊
#=========================================
Network_info()
{
#計算本機IP個數
 IPGS=`ifconfig |egrep "\<inet\>" |wc -l`
#查詢本機閘道器地址
 GW=`route -n |egrep "^0.0.0.0"|awk '{print $2}'`
echo "=============Network_info==============="
echo "本機IP地址有$IPGS個"
ifconfig |egrep "\<inet\>" |awk '{print$2":"$4}'|awk -F: '{print"  IP:"$2",""子網掩碼:"$4}'
echo "閘道器地址為:$GW"
echo "=============Network_info==============="
}


#==================================== 
#Function-->供使用者互動選擇的主選單介面
#====================================
Menu()
{
   echo "=============歡迎使用系統資訊查詢功能==============="
   echo
   echo "1. 系統|硬體資訊"
   echo
   echo "2. 硬碟|分割槽資訊"
   echo
   echo "3. 記憶體資訊"
   echo 
   echo "4. 作業系統|執行資訊"
   echo
   echo "5. 網路資訊"
   echo
   echo "0. 退出系統"
   read -p "       請你輸入你想查詢的資訊:"  choice
   
   case $choice in
   1) clear
      System_info
      Menu
   ;;
  
   2) clear
      Disk_info
      Menu  
   ;;
  
   3) clear
      Mem_info 
      Menu
   ;;
  
   4) clear
      Os_info  
      Menu
   ;;
   
   5) clear
      Network_info
      Menu
   ;;
   
   0) exit 0
   ;;
   
   *) echo "sorry,輸入錯誤!"
      Menu
   ;;
   
   esac

}


#==================================== 
#Function--->主函式
#====================================
Main()
{
  Menu
}


#==================================== 
#入口
#====================================
Main;

 

相關文章