-
Notifications
You must be signed in to change notification settings - Fork 4
/
device-info
executable file
·147 lines (129 loc) · 4.36 KB
/
device-info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
#
# Output one or more lines of information about
# - wireless LAN
# - ethernet controller
# - display controller
# - VGA compatible controller
# - CPU
# - is running in virtualbox vm
# device.
#
# This command can be used e.g. with programs and scripts that
# make decisions based on certain hardware,
# or finding information about certain hardware.
Usage() {
test -n "$1" && echo "Error: $1." >&2
cat <<EOF >&2
Usage: $progname option
where
--wireless
--wifi shows info about the wireless LAN device
--ethernet shows info about the ethernet controller
--display shows info about the display controller
--nvidia-gpuid shows the id (4 hex numbers, lowercase) of the installed Nvidia card
--vga shows info about the VGA compatible controller and 3D controller
--graphics same as both --vga and --display
--cpu shows the name of the CPU type
--vm if running in VM, echoes the name of the VM (virtualbox, qemu, vmware)
--virtualbox echoes "yes" is running in VirtualBox VM, otherwise "no"
EOF
}
NvidiaGpuId() {
local -r NVIDIA="10de"
lspci -vnn | grep -P 'VGA|Display|3D' | grep "\[$NVIDIA:" | sed "s|.*\[$NVIDIA:\([0-9a-f]*\).*|\1|"
}
PCI_info() {
# Many search strings may be given - show all results.
local result
for str in "$@" ; do
result="$(lspci | grep "$str" | sed 's|^.*'"$str"'||')"
if [ -n "$result" ] ; then
echo "$result"
fi
done
}
CPU_info() {
# lscpu | grep "^Vendor ID:" | awk '{print $3}'
grep -m1 -w "^vendor_id" /proc/cpuinfo | awk '{print $3}'
}
InVirtualBox() {
if [ "$(InVm)" = "virtualbox" ] ; then
echo yes
else
echo no
fi
#test -n "$(lspci | grep "VirtualBox Graphics Adapter")" && echo yes || echo no
}
InVm() {
local vmname="$(systemd-detect-virt --vm)"
case "$vmname" in
oracle)
echo virtualbox ;;
qemu | kvm | vmware)
echo $vmname ;;
esac
return
# old implementation:
case "$(lspci -vnn)" in
*" QEMU "*) echo qemu ;;
*VirtualBox*) echo virtualbox ;;
*VMware*) echo vmware ;; # this should be the last here!
esac
}
EthernetShow() {
local name="$1"
local value="$2"
printf "%-15s : %s\n" "$name" "$value"
}
Ethernet() {
local devstring="Ethernet controller"
local data=$(lspci -vnn | sed -n "/$devstring/,/^$/p")
local card=$( echo "$data" | grep -w "$devstring")
local id=$( echo "$card" | sed 's|.*\[\([0-9a-f:]*\)\].*|\1|')
local driver=$(echo "$data" | grep 'Kernel driver in use' | awk '{print $NF}')
EthernetShow "card id" "$id"
EthernetShow "card info" "$card"
EthernetShow "driver in use" "$driver"
}
Options() {
opts="$(/bin/getopt -o="$SO" --longoptions "$LO,$LO2" --name "$progname" -- "$@")" || {
Usage
return 1
}
eval set -- "$opts"
while true ; do
case "$1" in
--cpu) CPU_info ;;
--display) PCI_info " Display controller: " ;;
--ethernet) Ethernet ;;
--graphics) $FUNCNAME --vga ; $FUNCNAME --display ;;
--nvidia-gpuid) NvidiaGpuId ;;
--vga) PCI_info " VGA compatible controller: " " 3D controller: " ;;
--virtualbox) InVirtualBox ;;
--vm) InVm ;;
--wifi | --wireless) PCI_info " Network controller: " ;;
--help | -h) Usage; return 0 ;;
--dump-options) echo "${SO//?/-& }--${LO//,/ --} --${LO2//,/ --}"
## - LO may *not* be empty
## - SO handling requires 'patsub_replacement' enabled by 'shopt'
;;
--) shift; break ;;
*) Usage "unsupported option '$1'"
return 1
;;
esac
shift
done
}
Main()
{
local -r progname="${0##*/}"
local -r LO="cpu,display,ethernet,graphics,help,nvidia-gpuid,vga,virtualbox,vm,wifi,wireless"
local -r LO2="dump-options"
local -r SO="h"
local opts
test -n "$1" || { Usage "option missing" ; return 1 ; }
Options "$@"
}
Main "$@"