Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the Bash script can view Linux system information every time you log in to Shell

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how the Bash script can view Linux system information every time you log in to Shell. I hope you will get something after reading this article. Let's discuss it together.

There are many commands in Linux that can view system information such as processor information, manufacturer name, serial number, and so on. You may need to execute multiple commands to collect this information. At the same time, it is difficult to remember all the commands and their options.

You can write a shell script to customize the information displayed based on your needs.

In the past, we had to write a lot of bash scripts for different purposes.

Now let's write a new shell script that displays the required system information each time we log in to shell.

The j script has six parts, and the details are as follows:

General system information

Current usage of CPU/ memory

Hard disk usage is more than 80%

List system WWN details

Oracle DB instance

Updatable package

We have added the information that may be needed to each part based on our needs. You can then modify the script as you wish.

This j script requires a lot of tools, most of which we've covered before.

If you want to add additional information to this script, please leave your requirements in the comments so that we can help you.

The Bash script enables you to view Linux system information every time you log in to Shell.

This script prints the system information to terminal every time you log in to shell.

# vi / OptUniverse scriptsAccordBASHEC-e "- System Information--" echo-e "Hostname:\ t\ t" `hostname`echo-e "uptime:\ t\ t" `uptime | awk'{print $3movie 4}'| sed / / / ``echo-e "Manufacturer:\ t\ t" `cat / sys/class/dmi/id/chassis_ Vendor`echo-e "Product Name:\ t\ t" `cat / sys/class/dmi/id/product_ name`echo-e "Version:\ t\ t" `cat / sys/class/dmi/id/product_ version `echo-e "Serial Number:\ t\ t" `cat / sys/class/dmi/id/product_ serial`echo-e "Machine Type:\ t\ t" `vserver=$ (lscpu | grep Hypervisor | wc-l) If [$vserver-gt 0]; then echo "VM"; else echo "Physical" Fi`echo-e "Operating System:\ t" `hostnamectl | grep "Operating System" | cut-d'- f5-`Echo-e "Kernel:\ t\ t\ t" `uname-r`echo-e "Architecture:\ t\ t" `arch`echo-e "Processor Name:\ t\ t" `awk-Flex awk'/ ^ model name/ {print $2}'/ proc/cpuinfo | uniq | sed-e / ^ [\ t] * / / ``echo-e "Active User:\ t\ t" `w | cut-d'- F1 | grep-v USER | xargs-n1`echo-e "System Main IP:\ t\ t" `hostname-I`echo "" echo-e "- CPU/Memory Usage--" echo-e "Memory Usage:\ t" `free | awk '/ Mem/ {printf (".2f%") Echo-e "Swap Usage:\ t" `free | awk'/ Swap/ {printf ("% .2f%"), $3max / 2f%} '`echo-e "CPU Usage:\ t" `cat / proc/stat | awk' / cpu/ {printf ("% .2f%\ n") ($2 million 4) * 100 / ($2 million 4 minutes 5)}'| awk'{print $0}'| head-1`echo "" echo-e "- Disk Usage > 80% Disk Usage -" df-Ph | sed sdebar% g | awk'{if ($5 > 80) print $0 } 'echo "" echo-e "--For WWN Details--" vserver=$ (lscpu | grep Hypervisor | wc-l) if [$vserver-gt 0] thenecho "$(hostname) is a VM" elsecat / sys/class/fc_host/host?/port_namefiecho " "echo-e"-Oracle DB Instances-- "if id oracle > / dev/null 2 > & 1 Then/bin/ps-ef | grep pmonthenelseecho "oracle user does not exist on $(hostname)" fiecho "" if (($(cat / etc/*-release | grep-w "Oracle | Red Hat | CentOS | Fedora" | wc-l) > 0) thenecho-e "--Package Updates-- "yum updateinfo summary | grep 'Security | Bugfix | Enhancement'echo-e"-"elseecho-e"- -Package Updates-- "cat / var/lib/update-notifier/updates-availableecho-e"- -"fi

Save the contents of the above script to a file system-info.sh, and then add executable permissions:

# chmod + x ~ root/system-info.sh

When the script is ready, add the path to the script file to the end of the .bash _ profile file (Red Hat systems: CentOS, Oracle Linux, and Fedora):

# echo "/ root/system-info.sh" > > ~ root/.bash_profile

Execute the following command to make the changes take effect:

# source ~ root/.bash_profile

For Debian systems, you may need to add the file path to the .profile file:

# echo "/ root/system-info.sh" > > ~ root/.profile

Run the following command to make the changes take effect:

# source ~ root/.profile

You may have seen output similar to the following when you ran the above source command before. From next time, you will see these messages every time you log in to shell. Of course, you can execute this script manually at any time if necessary.

-- System Information--Hostname: daygeek-Y700uptime: 1:20 1Manufacturer: LENOVOProduct Name: 80NVVersion: Lenovo ideapad Y700-15ISKSerial Number: AA0CMRN1Machine Type: PhysicalOperating System: Manjaro LinuxKernel: 4.19.80-1-MANJAROArchitecture: x86_64Processor Name: Intel (R) Core (TM) i7-6700HQ CPU @ 2.60GHzActive User: daygeek renu thanuSystem Main IP: 192.168.1.6 192.168.122.1-- CPU/Memory Usage -- Memory Usage: 37.28%Swap Usage: 0.00%CPU Usage: 15.43%-- Disk Usage > 80% Murray- -Filesystem Size Used Avail Use Mounted on/dev/nvme0n1p1 217G 202G 4.6G 98 / / dev/loop0 109m 109m 0100 / var/lib/snapd/snap/odrive-unofficial/2/dev/loop1 91m 91m 0100 / var/lib/snapd/snap/core/6405/dev/loop2 90m 90m 0100 / var/lib/snapd/snap/core/7713- -For WWN Details--CentOS8.2daygeek.com is a VM-- Oracle DB Instances--oracle user does not Exist on CentOS8.2daygeek.com-- Package Updates-- 13 Security notice (s) 9 Important Security notice (s) 3 Moderate Security notice (s) 1 Low Security notice (s) 35 Bugfix notice (s) 1 Enhancement notice (s)-finish reading this article I believe you have a certain understanding of "how the Bash script can view Linux system information every time you log in to Shell". If you want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report