-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysinfo
executable file
·465 lines (448 loc) · 11.6 KB
/
sysinfo
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env bash
# Use bash or zsh, sh won't work in many cases!
#
# TODO:
# - info_env: Add $MAIL
# - info_locale: $TZ (or /etc/localtime or date +"%Z %z")
#
LOCALE_OLD=$LC_ALL
export LC_ALL=C
version_info() {
echo "sysinfo 0.20240111"
echo "Copyright (c) 2014-2024 Malte Bublitz"
}
display_help() {
version_info
echo -e "
Generic options:
\t-V, --version\tDisplay version information
\t-h, --help\tDisplay this help
Output control:
\t-o\tOperating System
\t-r\tOS Kernel
\t-p\tPlatform/machine type
\t-n\tNodename/Hostname
\t-u\tUptime
\t-w\tWindow Manager
\t-m\tUsed/available RAM
\t-H\tHardware Info
\t-c\tCPU info
\t-t\tTerminal info
\t-e\tEnvironment (Shell/Editor/Browser)
\t-l\tLocale
\t-a\tDisplay all information
"
}
info_os() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 3))');'`
echo -n "OS${DOTS}: "
if [ -f /etc/arch-release ]; then
echo -n "Arch Linux"
elif [ -f /etc/lsb-release ]; then
(
. /etc/lsb-release
echo -n $DISTRIB_DESCRIPTION
)
elif [ -f /etc/os-release ]; then
(
. /etc/os-release
echo -n $NAME
)
elif [ `uname` == "Darwin" ]; then
echo -n "Mac OS X "
python3 -c 'import platform; print(platform.mac_ver()[0],end="")' | tr -d "\n"
elif grep GhostBSD /etc/motd &>/dev/null; then
echo -n "GhostBSD"
[ `uname -r` == "10.0-RELEASE" ] && echo -n " 4.0"
elif [ `uname -o` == "Cygwin" ]; then
python -c 'import OSDetect; _i = OSDetect.OSInfo(); print _i.GetInfo()["Distribution"]' | tr -d "\n"
elif [ `uname -o` == "Android" ]; then
echo -n "$(uname -o) $(getprop ro.build.version.release) (API Level $(getprop ro.build.version.sdk))"
else
uname -o | tr -d "\n"
fi
echo
}
info_platform() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 8))');'`
echo -n "Machine${DOTS}: "
uname -m
}
info_kernel() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 7))');'`
echo -n "Kernel${DOTS}: "
uname -o &>/dev/null && [ `uname -o` == "Cygwin" ] && {
echo -n "Windows NT "
python -c 'import OSDetect; _i = OSDetect.OSInfo(); print _i.GetInfo()["OSVersion"]'
} && exit
uname -sr
}
info_nodename() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 9))');'`
echo -n "Hostname${DOTS}: "
if [ -z "$TERMUX_ROOT" ]; then
hostname -f 2>/dev/null
[[ $? -ne 0 ]] && hostname
else
getprop net.hostname
fi
}
info_uptime() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 7))');'`
echo -n "Uptime${DOTS}: "
if [ `uname` == "Darwin" ]; then
LC_ALL=C uptime | python3 -c 'import sys;l = sys.stdin.readline().split(","); print(" ".join(l[:-2]))' | cut -d" " -f4-
elif [ `uname -o ` == Cygwin ]; then
python -c 'import OSDetect; _i = OSDetect.OSInfo(); print _i.GetInfo()["Uptime"]'
elif [ -n "$TERMUX_ROOT" ]; then
# Termux's uptime doesn't show the user count!
LC_ALL=C uptime | python -c 'import sys;l = sys.stdin.readline().split(","); print(" ".join(l[:-3]))' | cut -d" " -f4- | stripwhite
else
LC_ALL=C uptime | python3 -c 'import sys;l = sys.stdin.readline().split(","); print(" ".join(l[:-4]))' | cut -d" " -f4- | stripwhite
fi
}
info_wm() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 3))');'`
echo -n "WM${DOTS}: "
WM=""
ps x | grep "X" >/dev/null | grep -v grep || WM="None"
ps x | grep "gnome-session" | grep -v grep >/dev/null && WM="GNOME `gnome-help --version | cut -d\ -f2`"
ps x | grep "mate-session" | grep -v grep >/dev/null && WM="`mate-about --version`"
ps x | grep "awesome" | grep -v grep >/dev/null && WM="Awesome"
ps x | grep "startkde" | grep -v grep >/dev/null && WM="KDE $(kde-open --version | grep KDE | cut -d\ -f2)"
ps x | grep "SystemUIServer" | grep -v grep >/dev/null && WM="Aqua"
ps x | grep "cinnamon" | grep -v grep >/dev/null && WM="$(cinnamon --version)"
ps x | grep "lxsession" | grep -v grep >/dev/null && WM="LXDE"
ps x | grep "karmen" | grep -v grep >/dev/null && WM="Karmen"
#[ -z $WM -o $WM = "None" ] && WM="Unknown"
echo $WM
}
info_mem() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 4))');'`
echo -n "RAM${DOTS}: "
if [ `uname` == "Darwin" ]; then
# Darwin/OS X is missing the free command, using "top" instead.
top -l 1 | head -n 10 | grep PhysMem | cut -d"(" -f 2 | cut -d" " -f1 | cut -f1 | tr -d "M" | tr -d "\n"
echo -n " MB / "
{
top -l 1 | head -n 10 | grep PhysMem | awk '{ print $2 }' | sed 's/M//g;s/G/*1024/g' | tr -d "M\n"
echo -n "+"
top -l 1 | head -n 10 | grep PhysMem | awk '{ print $6 }' | tr -d "M"
} | bc | tr -d "\n"
echo " MB"
else
FREE="free"
GREPSTRING="buffers/cache"
if [ `uname` == "FreeBSD" ]; then
# Use freecolor on BSD (available in sysutils/freecolor).
# freecolor gives an output similiar to GNU's free, but not exactly the same,
# therefore we need another string for grep.
FREE="freecolor -o"
fi
$FREE | grep $GREPSTRING &>/dev/null || GREPSTRING="^Mem"
$FREE -m | grep $GREPSTRING | awk '{ print $3 "MB" }' | tr -d "\n"
echo -n " / "
$FREE -m | grep ^Mem | awk '{ print $2 "MB" }'
fi
}
info_hardware() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 9))');'`
echo -n "Hardware${DOTS}: "
if [ `uname` == "Linux" ]; then
if [ -r "/sys/devices/virtual/dmi/id/product_name" ]; then
if grep KVM /sys/devices/virtual/dmi/id/product_name &>/dev/null; then
echo "Virtual Machine (KVM)"
elif grep "^flags" /proc/cpuinfo | grep hypervisor &>/dev/null; then
echo "Virtual Machine"
else
cat /sys/devices/virtual/dmi/id/product_name
fi
elif [ -r /sys/firmware/devicetree/base/model ]; then
cat /sys/firmware/devicetree/base/model | tr "\0" "\n"
fi
elif [ `uname` == "Darwin" ]; then
# # https://apple.stackexchange.com/a/98089
# curl -s https://support-sp.apple.com/sp/product\?cc=$(
# system_profiler SPHardwareDataType \
# | awk '/Serial/ {print $4}' \
# | cut -c 9-
# ) | sed 's|.*<configCode>\(.*\)</configCode>.*|\1|'
# echo
system_profiler SPHardwareDataType | awk '/Model Name/ {print $3" "$4" ("}; /Model Ident/ {print $3")"}' | tr -d "\n"
echo
else
echo "Unable to detect"
fi
}
info_cpu() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 4))');'`
echo -n "CPU${DOTS}: "
if [ `uname` == "Darwin" ]; then
sysctl -a | grep machdep.cpu.brand_string | cut -c 27-
elif [ `uname` == "FreeBSD" ]; then
sysctl -a | grep hw.model | cut -c 11-
else
if grep "S390" /proc/cpuinfo &>/dev/null
then
lscpu | grep "Vendor ID" | cut -c24- | tr "\n" " "
echo -n "(CPU Type "
grep "^processor 0" /proc/cpuinfo | cut -d= -f4 | stripwhite | tr -d "\n"
echo ")"
elif [ `uname -m` == "aarch64" ]
then
grep ^Hardware /proc/cpuinfo | cut -d: -f2 | stripwhite || echo "unknown"
else
grep "model name" /proc/cpuinfo | uniq | cut -d: -f2- | stripwhite
fi
fi
}
info_android() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 7))');'`
echo "Device${DOTS}: `getprop ro.config.marketing_name` (`getprop ro.product.model`)"
}
info_env() {
#
# $SHELL
#
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 7))');'`
#[ -n "$SHELL" ] && echo "\$SHELL${DOTS}: `basename $SHELL`"
ENV_SHELL=="$(basename $(which $SHELL)) ($(dirname $(which $SHELL)))"
[ -n "$SHELL" ] && echo "\$SHELL${DOTS}: $ENV_SHELL"
#
# $EDITOR
#
if [ -n "$EDITOR" ]; then
# Values might be "vim" or "/bin/vim", so run which before basename!
#ENV_EDITOR="$(basename $(which $EDITOR))"
#ENV_EDITOR="$(which $EDITOR)"
ENV_EDITOR="$(basename $(which $EDITOR)) ($(dirname $(which $EDITOR)))"
else
ENV_EDITOR=""
fi
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 8))');'`
[ -n "$ENV_EDITOR" ] && echo "\$EDITOR${DOTS}: $ENV_EDITOR"
#
# $VISUAL
#
if [ -n "$VISUAL" ]; then
ENV_VISUAL="$(basename $(which $VISUAL)) ($(dirname $(which $VISUAL)))"
else
ENV_VISUAL=""
fi
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 8))');'`
[ -n "$ENV_VISUAL" ] && echo "\$VISUAL${DOTS}: $ENV_VISUAL"
#
# $PAGER
#
if [ -n "$PAGER" ]; then
ENV_PAGER="$(basename $(which $PAGER)) ($(dirname $(which $PAGER)))"
else
ENV_PAGER=""
fi
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 7))');'`
[ -n "$ENV_PAGER" ] && echo "\$PAGER${DOTS}: $ENV_PAGER"
#
# $BROWSER
#
if [ -n "$BROWSER" ]; then
ENV_BROWSER="$(basename $(which $BROWSER)) ($(dirname $(which $BROWSER)))"
else
ENV_BROWSER=""
fi
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 9))');'`
[ -n "$ENV_BROWSER" ] && echo "\$BROWSER${DOTS}: $ENV_BROWSER"
}
info_terminal() {
if [ -z "$TTY" ]; then
# Ensure $TTY is set up as expected
TTY=$(tty 2>/dev/null)
fi
# Check if we are connected to a pseudo terminal device
if [ -z "$TTY" ]; then
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 13))');'`
echo "Terminal TTY${DOTS}: (no pseudo terminal)"
return
fi
# Terminal device
if [ -n "$TTY" ]; then
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 13))');'`
echo "Terminal TTY${DOTS}: $TTY"
fi
# Terminal type
if [ -n "$TERM" ]; then
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 14))');'`
echo "Terminal Type${DOTS}: $TERM"
fi
# Terminal size: Number of columns * Number of lines
if [ -n "$COLUMNS" ] && [ -n "$LINES" ]; then
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 14))');'`
echo "Terminal Size${DOTS}: ${COLUMNS}x${LINES}"
fi
}
info_locale() {
DOTS=`perl -e 'print("." x '$(( ${DESC_COL_LENGTH} - 7))');'`
[ -n $LOCALE_OLD ] && locale=$LOCALE_OLD # LC_ALL is overwritten, use the backup.
[ -n $LANG ] && locale=$LANG
[ -z $locale ] && locale=C
echo "Locale${DOTS}: $locale"
# TODO: Time zone
}
info_all() {
export DESC_COL_LENGTH=14
info_os
info_kernel
info_platform
info_nodename
info_uptime
info_wm
info_mem
if [ -z "$TERMUX_ROOT" ]; then
info_hardware
info_cpu
else
info_android
fi
info_env
info_terminal
info_locale
}
if [[ -z "$1" ]]; then
info_all
exit 0
elif [[ "$1" == "--help" ]]; then
display_help
exit 0
elif [[ "$1" == "--version" ]]; then
display_version
exit 0
fi
# Length for descriptive column
DESC_COL_LENGTH=2
# parse arguments once to get $DESC_COL_LENGTH
while getopts "VhornuwmetplacH" opt; do
case $opt in
o)
if [[ $DESC_COL_LENGTH -lt 3 ]]; then
DESC_COL_LENGTH=3
fi
;;
r)
if [[ $DESC_COL_LENGTH -lt 7 ]]; then
DESC_COL_LENGTH=7
fi
;;
p)
if [[ $DESC_COL_LENGTH -lt 8 ]]; then
DESC_COL_LENGTH=8
fi
;;
H)
if [[ $DESC_COL_LENGTH -lt 8 ]]; then
DESC_COL_LENGTH=8
fi
;;
n)
if [[ $DESC_COL_LENGTH -lt 9 ]]; then
DESC_COL_LENGTH=9
fi
;;
u)
if [[ $DESC_COL_LENGTH -lt 7 ]]; then
DESC_COL_LENGTH=7
fi
;;
w)
if [[ $DESC_COL_LENGTH -lt 3 ]]; then
DESC_COL_LENGTH=3
fi
;;
m)
if [[ $DESC_COL_LENGTH -lt 3 ]]; then
DESC_COL_LENGTH=3
fi
;;
h)
if [[ $DESC_COL_LENGTH -lt 9 ]]; then
DESC_COL_LENGTH=9
fi
;;
c)
if [[ $DESC_COL_LENGTH -lt 4 ]]; then
DESC_COL_LENGTH=4
fi
;;
e)
if [[ $DESC_COL_LENGTH -lt 9 ]]; then
DESC_COL_LENGTH=9
fi
;;
t)
if [[ $DESC_COL_LENGTH -lt 14 ]]; then
DESC_COL_LENGTH=14
fi
;;
l)
if [[ $DESC_COL_LENGTH -lt 6 ]]; then
DESC_COL_LENGTH=6
fi
;;
esac
done
OPTIND=1
# parse arguments again, and now, display the information.
while getopts "VhornuwmpetlaHc" opt; do
case $opt in
V)
version_info
exit 0
;;
h)
display_help
exit 0
;;
o)
info_os
;;
r)
info_kernel
;;
p)
info_platform
;;
n)
info_nodename
;;
u)
info_uptime
;;
w)
info_wm
;;
m)
info_mem
;;
H)
info_hardware
;;
c)
info_cpu
;;
e)
info_env
;;
t)
info_terminal
;;
l)
info_locale
;;
a)
info_all
;;
*)
display_help
exit 1
;;
esac
done
exit 0