Shell写的检测Linux系统硬件信息的脚本


这里是很久之前用shell写的一个linux系统硬件信息检测脚本,可以对照看一下。检测内容基本一样:操作系统信息、内存、CPU、硬盘分区及挂载情况,网卡配置情况、安装的软件信息等。

  1. #!/bin/bash 
  2. #This script is used to check the server 
  3. #system info 
  4. system_info() { 
  5. echo "**********************************************" 
  6. echo "system info:" 
  7. echo 
  8. echo "   System-release : `cat /etc/RedHat-release`" 
  9. echo "   Kernel-release : `uname -a|awk '{print $1,$3}'`" 
  10. echo "   Server-Model : `dmidecode | grep "Product Name:"|sed -n '1p'|awk -F': ' '{print $2}'`" 
  11. echo 
  12.  
  13.  
  14.  
  15.  
  16. #CPU info 
  17. cpu_info() { 
  18. echo "**********************************************" 
  19. echo "CPU info:" 
  20. echo 
  21. echo "    Frequency : `cat /proc/cpuinfo | grep "model name" | uniq |awk -F': ' '{print $2}'`" 
  22. echo "    CPU cores:  `cat /proc/cpuinfo | grep "cpu cores" | uniq |awk -F': ' '{print $2}'`" 
  23. echo "    Logic Count : `cat /proc/cpuinfo | grep "processor" | sort -u| wc -l `" 
  24. echo "    Physical Count : `cat /proc/cpuinfo | grep "physical" | sort -u| wc -l`" 
  25. echo "    Cache size : `cat /proc/cpuinfo| grep "cache size"|uniq|awk '{print $4,$5}'`" 
  26. echo 
  27.  
  28.  
  29.  
  30.  
  31.  
  32. #memory info 
  33. mem_info() { 
  34. memory=`dmidecode |grep "Range Size"|head -1|awk '{print $3$4}'
  35. mem_size=`echo "This server has ${memory} memory."
  36.  
  37. echo "**********************************************" 
  38. echo "Memory info:" 
  39. echo 
  40. echo "   Total : ${mem_size}" 
  41. echo "   Count : `dmidecode |grep -A16 "Memory Device$"|grep Size|awk '{if($2!~/No/) print $0}'|wc -l`" 
  42. dmidecode |grep -A20 "Memory Device$"|grep Size|sed '{s/^       */   /g};{/No/d}' 
  43. echo 
  44.  
  45.  
  46.  
  47.  
  48.  
  49. #disk and partitions 
  50. swap_pos=`cat /proc/swaps|sed -n '2p'|awk '{print $1}'
  51. partition_info() { 
  52. echo "**********************************************" 
  53. echo "Hard disk info:" 
  54. echo 
  55. echo "`fdisk -l|grep Disk|awk -F, '{print $1}'`" 
  56. echo "**********************************************" 
  57. echo "Partition info:" 
  58. echo 
  59. df -h | grep -v Filesystem | sed "s:none:${swap_pos}:" 
  60. echo 
  61.  
  62.  
  63. #network adapter info 
  64. adapter_info() { 
  65.  
  66. duplex_eth0=`ethtool eth0 | grep Duplex | awk '{if($2~/Full/) print "Full"};{if($2~/Half/)print "Half"};{if($2~/Uknown!/) print "unknown"}'
  67.  
  68. duplex_eth1=`ethtool eth1 | grep Duplex | awk '{if($2~/Full/) print "Full"};{if($2~/Half/)print "Half"};{if($2~/Uknown!/) print "unknown"}'
  69.  
  70. Negotiation_eth0=`ethtool eth0 | grep "Advertised auto-negotiation"|awk -F': ' '{if($2~/No/) print "Non-negotiation."};{if($2~/Yes/) print "Negotiation"}'
  71.  
  72. Negotiation_eth1=`ethtool eth1 | grep "Advertised auto-negotiation"|awk -F': ' '{if($2~/No/) print "Non-negotiation"};{if($2~/Yes/) print "Negotiation"}'
  73.  
  74. IP_eth0=`cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep IPADDR|awk -F= '{print $2}'
  75.  
  76. IP_eth1=`cat /etc/sysconfig/network-scripts/ifcfg-eth1|grep IPADDR|awk -F= '{print $2}'
  77.  
  78. speed_eth0=`ethtool eth0|grep Speed|awk '{print $2}'
  79. speed_eth1=`ethtool eth1|grep Speed|awk '{print $2}'
  80.  
  81. echo "**********************************************" 
  82. echo "Network adapter info:" 
  83. echo 
  84. echo "  IP_eth0 : ${IP_eth0}        IP_eth0 : ${IP_eth1}" 
  85. echo "  Speed_eth0 : ${speed_eth0}          Speed_eth1 : ${speed_eth1}" 
  86. echo "  Duplex_eth0 : ${duplex_eth0}            Duplex_eth1 : ${duplex_eth1}" 
  87. echo "  Negotiation_eth0 : ${Negotiation_eth0}  Negotiation_eth1 : ${Negotiation_eth1}" 
  88. echo 
  89.  
  90.  
  91.  
  92.  
  93. #software package 
  94. software_info() { 
  95. echo "**********************************************" 
  96. echo "SELinux is `cat /etc/selinux/config |grep SELINUX=disabled|awk -F= '{print $2}'||echo "enabled"`" 
  97. echo "`service iptables status|sed 's/Firewall/Iptables/g'`" 
  98. echo 
  99. echo "**********************************************" 
  100. sed -n '/%packages/,/%post/p;' /root/anaconda-ks.cfg|sed '/%post/d;/^$/d' 
  101. echo "**********************************************" 
  102.  
  103.  
  104.  
  105. #del mac-addr 
  106. #sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0 
  107. #sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth1 
  108.  
  109.  
  110. system_info 
  111. cpu_info 
  112. mem_info 
  113. partition_info 
  114. adapter_info 
  115. software_info 

相关内容