linux 获取Pci-E设备列表(脚本)

2022/1/8 7:07:12

本文主要是介绍linux 获取Pci-E设备列表(脚本),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 1 #!/bin/bash
 2 #
 3 #通过dmidecode获取有关“Designation:、Type:、Current Usage:、Length:、Bus Address:的信息,并输出到/tmp/dmidecode_slot.log文件内
 4 dmidecode -t slot | grep -E "Designation:|Type:|Current Usage:|Length:|Bus Address:" | sed 's/^[ \t]*//g' | sed '/^$/d' | grep -v "\-\-" \
 5  > /tmp/dmidecode_slot.log
 6  #在/tmp/dmidecode_slot.log文件内删除x8 x16 x2 x4每行第一份匹配的值
 7 sed -i 's/x8 //;s/x16 //;s/x2 //;s/x4 //' /tmp/dmidecode_slot.log
 8 sed -i 's/Express //' /tmp/dmidecode_slot.log
 9 #获取bus address值,并赋值给busid变量
10 busid=`cat /tmp/dmidecode_slot.log | grep "Bus Address:" | awk -F ":" '{print $3":"$4}'|awk -F "." '{print $1}'| sort | uniq`
11 #通过busid循环出对应的pcie插卡设备名称,将空槽位设置未N/A,并将其值输出到 /tmp/dmidecode_slot.log文件里的bus addres 的下一行
12 for i in $busid
13  do 
14 # pci_devices=$(lspci | grep "$i" |awk -F ":" '{print $NF}'| sort |uniq -c | awk 'NF{NF--}1'| awk 'NF{NF--}1')
15  pci_devices=$(lspci | grep "$i" |awk -F ":" '{print $NF}'| sort |uniq -c|sed s'/^ *//g')
16 
17  if [ -n "$pci_devices" ]
18  then 
19  :
20  else 
21  pci_devices="N/A" 
22  fi
23  sed -i '/'"$i"'/a'"pcie_devices: $pci_devices"'' /tmp/dmidecode_slot.log
24 done
25 #
26 #
27 #去除标题,保留内容,并重新输出到dmidecode_slot1,log
28 cat /tmp/dmidecode_slot.log |awk -F ": " '{print $2}' > /tmp/dmidecode_slot1.log
29 #
30 #去掉busid的0000:
31 cat /tmp/dmidecode_slot1.log | grep 0000:| sed -i 's/\.0//;s/0000://' /tmp/dmidecode_slot1.log
32 #添加格式化输出分割线
33 sed -i 's/^/| &/g' /tmp/dmidecode_slot1.log
34 #
35 #将文件中的空格换成下划线(_),方便printf对其格式化输出
36 sed -i 's/ /_/g' /tmp/dmidecode_slot1.log
37 #将dmidecode_slot1.log内容转换为一行,并赋值为pci_list
38 pci_list=$(echo `cat /tmp/dmidecode_slot1.log`)
39 #清屏
40 clear
41 #格式化输出
42 echo -e "\033[033m
43 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
44 +                                      ____  __________           __    _      __                                                           +  
45 +                                     / __ \/ ____/  _/__        / /   (_)____/ /_                                                          +  
46 +                                    / /_/ / /    / // _ \______/ /   / / ___/ __/                                                          +  
47 +                                   / ____/ /____/ //  __/_____/ /___/ (__  ) /_                                                            +  
48 +                                  /_/    \____/___/\___/     /_____/_/____/\__/   v1.0-2018.10.17                                          +  
49 +                                                                                                                                           +\033[0m"
50 #
51 echo -e "\033[33m+------------+------------+-----------+--------+----------+---------------------------------------------------------------------------------+\033[0m" > /tmp/pcie_list.log
52 echo -e "\033[33m| SOLT       | TPYE       | USAGE     | LENGTH | BUS ADD  | DEVIECS                                                                         |\033[0m" >> /tmp/pcie_list.log
53 echo -e "\033[33m+------------+------------+-----------+--------+----------+---------------------------------------------------------------------------------+\033[0m" >> /tmp/pcie_list.log
54 printf "%-12s %-12s %-11s %-8s %-10s %-1s\n" $pci_list >> /tmp/pcie_list.log
55 echo -e "\033[33m+------------+------------+-----------+--------+----------+---------------------------------------------------------------------------------+\033[0m" >> /tmp/pcie_list.log
56 #将文件中的下划线(_)换成空格,还原输出
57 sed -i 's/_/ /g' /tmp/pcie_list.log
58 #将结果输出到屏幕上
59 cat /tmp/pcie_list.log
60 #删除临时文件
61 #rm -rf /tmp/dmidecode_slot1.log /tmp/dmidecode_slot.log

 



这篇关于linux 获取Pci-E设备列表(脚本)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程