自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Bash腳本實(shí)現(xiàn)每次登錄到Shell時(shí)可以查看Linux系統(tǒng)信息

系統(tǒng) Linux
Linux 中有很多可以查看系統(tǒng)信息如處理器信息、生產(chǎn)商名字、序列號(hào)等的命令。你可能需要執(zhí)行多個(gè)命令來收集這些信息。同時(shí),記住所有的命令和他們的選項(xiàng)也是有難度。

[[284707]]

Linux 中有很多可以查看系統(tǒng)信息如處理器信息、生產(chǎn)商名字、序列號(hào)等的命令。你可能需要執(zhí)行多個(gè)命令來收集這些信息。同時(shí),記住所有的命令和他們的選項(xiàng)也是有難度。

你可以寫一個(gè) shell 腳本 基于你的需求來自定義顯示的信息。

以前我們出于不同的目的需要寫很多個(gè) bash 腳本。

現(xiàn)在我們寫一個(gè)新的 shell 腳本,在每次登錄到 shell 時(shí)顯示需要的系統(tǒng)信息。

這個(gè)j腳本有 6 部分,細(xì)節(jié)如下:

  1. 通用系統(tǒng)信息
  2. CPU/內(nèi)存當(dāng)前使用情況
  3. 硬盤使用率超過 80%
  4. 列出系統(tǒng) WWN 詳情
  5. Oracle DB 實(shí)例
  6. 可更新的包

我們已經(jīng)基于我們的需求把可能需要到的信息加到了每個(gè)部分。之后你可以基于自己的意愿修改這個(gè)腳本。

這個(gè)j腳本需要用到很多工具,其中大部分我們之前已經(jīng)涉及到了。

如果你想為這個(gè)腳本增加其他的信息,請(qǐng)?jiān)谠u(píng)論去留下你的需求,以便我們幫助你。

Bash 腳本實(shí)現(xiàn)每次登錄到 Shell 時(shí)可以查看 Linux 系統(tǒng)信息

這個(gè)腳本會(huì)在你每次登錄 shell 時(shí)把系統(tǒng)信息打印到 terminal。

  1. # vi /opt/scripts/system-info.sh
  1. #!/bin/bash
  2. echo -e "-------------------------------System Information----------------------------"
  3. echo -e "Hostname:\t\t"`hostname`
  4. echo -e "uptime:\t\t\t"`uptime | awk '{print $3,$4}' | sed 's/,//'`
  5. echo -e "Manufacturer:\t\t"`cat /sys/class/dmi/id/chassis_vendor`
  6. echo -e "Product Name:\t\t"`cat /sys/class/dmi/id/product_name`
  7. echo -e "Version:\t\t"`cat /sys/class/dmi/id/product_version`
  8. echo -e "Serial Number:\t\t"`cat /sys/class/dmi/id/product_serial`
  9. echo -e "Machine Type:\t\t"`vserver=$(lscpu | grep Hypervisor | wc -l); if [ $vserver -gt 0 ]; then echo "VM"; else echo "Physical"; fi`
  10. echo -e "Operating System:\t"`hostnamectl | grep "Operating System" | cut -d ' ' -f5-`
  11. echo -e "Kernel:\t\t\t"`uname -r`
  12. echo -e "Architecture:\t\t"`arch`
  13. echo -e "Processor Name:\t\t"`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`
  14. echo -e "Active User:\t\t"`w | cut -d ' ' -f1 | grep -v USER | xargs -n1`
  15. echo -e "System Main IP:\t\t"`hostname -I`
  16. echo ""
  17. echo -e "-------------------------------CPU/Memory Usage------------------------------"
  18. echo -e "Memory Usage:\t"`free | awk '/Mem/{printf("%.2f%"), $3/$2*100}'`
  19. echo -e "Swap Usage:\t"`free | awk '/Swap/{printf("%.2f%"), $3/$2*100}'`
  20. echo -e "CPU Usage:\t"`cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1`
  21. echo ""
  22. echo -e "-------------------------------Disk Usage >80%-------------------------------"
  23. df -Ph | sed s/%//g | awk '{ if($5 > 80) print $0;}'
  24. echo ""
  25.  
  26. echo -e "-------------------------------For WWN Details-------------------------------"
  27. vserver=$(lscpu | grep Hypervisor | wc -l)
  28. if [ $vserver -gt 0 ]
  29. then
  30. echo "$(hostname) is a VM"
  31. else
  32. cat /sys/class/fc_host/host?/port_name
  33. fi
  34. echo ""
  35.  
  36. echo -e "-------------------------------Oracle DB Instances---------------------------"
  37. if id oracle >/dev/null 2>&1; then
  38. /bin/ps -ef|grep pmon
  39. then
  40. else
  41. echo "oracle user does not exist on $(hostname)"
  42. fi
  43. echo ""
  44.  
  45. if (( $(cat /etc/*-release | grep -w "Oracle|Red Hat|CentOS|Fedora" | wc -l) > 0 ))
  46. then
  47. echo -e "-------------------------------Package Updates-------------------------------"
  48. yum updateinfo summary | grep 'Security|Bugfix|Enhancement'
  49. echo -e "-----------------------------------------------------------------------------"
  50. else
  51. echo -e "-------------------------------Package Updates-------------------------------"
  52. cat /var/lib/update-notifier/updates-available
  53. echo -e "-----------------------------------------------------------------------------"
  54. fi

把上面腳本內(nèi)容保存到一個(gè)文件 system-info.sh,之后添加可執(zhí)行權(quán)限:

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

當(dāng)腳本準(zhǔn)備好后,把腳本文件的路徑加到 .bash_profile 文件末尾(紅帽系列的系統(tǒng):CentOS、Oracle Linux 和 Fedora):

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

執(zhí)行以下命令,來讓修改的內(nèi)容生效:

  1. # source ~root/.bash_profile

對(duì)于 Debian 系統(tǒng)的系統(tǒng),你可能需要把文件路徑加到 .profile 文件中:

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

運(yùn)行以下命令使修改生效:

  1. # source ~root/.profile

你以前運(yùn)行上面 source 命令時(shí)可能見過類似下面的輸出。從下次開始,你在每次登錄 shell 時(shí)會(huì)看到這些信息。當(dāng)然,如果有必要你也可以隨時(shí)手動(dòng)執(zhí)行這個(gè)腳本。

  1. -------------------------------System Information---------------------------
  2. Hostname: daygeek-Y700
  3. uptime: 1:20 1
  4. Manufacturer: LENOVO
  5. Product Name: 80NV
  6. Version: Lenovo ideapad Y700-15ISK
  7. Serial Number: AA0CMRN1
  8. Machine Type: Physical
  9. Operating System: Manjaro Linux
  10. Kernel: 4.19.80-1-MANJARO
  11. Architecture: x86_64
  12. Processor Name: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  13. Active User: daygeek renu thanu
  14. System Main IP: 192.168.1.6 192.168.122.1
  15.  
  16. -------------------------------CPU/Memory Usage------------------------------
  17. Memory Usage: 37.28%
  18. Swap Usage: 0.00%
  19. CPU Usage: 15.43%
  20.  
  21. -------------------------------Disk Usage >80%-------------------------------
  22. Filesystem Size Used Avail Use Mounted on
  23. /dev/nvme0n1p1 217G 202G 4.6G 98 /
  24. /dev/loop0 109M 109M 0 100 /var/lib/snapd/snap/odrive-unofficial/2
  25. /dev/loop1 91M 91M 0 100 /var/lib/snapd/snap/core/6405
  26. /dev/loop2 90M 90M 0 100 /var/lib/snapd/snap/core/7713
  27.  
  28. -------------------------------For WWN Details-------------------------------
  29. CentOS8.2daygeek.com is a VM
  30.  
  31. -------------------------------Oracle DB Instances---------------------------
  32. oracle user does not exist on CentOS8.2daygeek.com
  33.  
  34. -------------------------------Package Updates-------------------------------
  35. 13 Security notice(s)
  36. 9 Important Security notice(s)
  37. 3 Moderate Security notice(s)
  38. 1 Low Security notice(s)
  39. 35 Bugfix notice(s)
  40. 1 Enhancement notice(s)
  41. -----------------------------------------------------------------------------

 

責(zé)任編輯:龐桂玉 來源: Linux中國(guó)
相關(guān)推薦

2010-06-23 15:55:36

Linux Bash

2021-06-10 06:57:40

網(wǎng)絡(luò)接口Shell

2010-06-23 11:09:14

Linux Bash

2018-02-01 17:32:30

LinuxUNIXBash Shell

2022-05-02 18:29:35

bashshellLinux

2021-12-30 10:26:37

Bash Shell腳本文件命令

2020-09-23 06:00:04

ShellLinux郵件監(jiān)控

2010-06-23 17:34:03

Linux Bash

2022-01-20 16:43:38

Bash 腳本ShellLinux

2022-02-28 11:02:53

函數(shù)Bash Shell語句

2010-06-23 16:35:50

Linux Bash

2010-06-23 17:37:14

Linux Bash

2011-08-02 10:15:53

ActiveDirec

2020-03-23 21:10:03

BashLinux文件系統(tǒng)

2021-05-11 07:50:31

BashShell腳本

2010-06-23 17:16:33

Linux Bash

2009-10-15 09:58:13

Linux系統(tǒng)信息查看

2010-06-23 16:31:10

Linux Bash

2010-06-23 17:29:07

Linux Bash

2010-02-03 14:57:18

Linux shell
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)