shell脚本收集Linux系统信息,Bash 脚本实现每次登录到 Shell 时可以查看 Linux 系统信息...

linux 中有很多可以查看系统信息如处理器信息、生产商名字、序列号等的命令。你可能需要执行多个命令来收集这些信息。同时,记住所有的命令和他们的选项也是有难度。

你可以写一个 shell 脚本 基于你的需求来自定义显示的信息。

以前我们出于不同的目的需要写很多个 bash 脚本。

现在我们写一个新的 shell 脚本,在每次登录到 shell 时显示需要的系统信息。

这个j脚本有 6 部分,细节如下:

通用系统信息

cpu/内存当前使用情况

硬盘使用率超过 80%

列出系统 wwn 详情

oracle db 实例

可更新的包

我们已经基于我们的需求把可能需要到的信息加到了每个部分。之后你可以基于自己的意愿修改这个脚本。

这个j脚本需要用到很多工具,其中大部分我们之前已经涉及到了。

下面重点给大家介绍bash 脚本实现每次登录到 shell 时可以查看 linux 系统信息,具体内容如下所示:

这个脚本会在你每次登录 shell 时把系统信息打印到 terminal。

# vi /opt/scripts/system-info.sh

#!/bin/bash

echo -e "-------------------------------system information----------------------------"

echo -e "hostname:\t\t"`hostname`

echo -e "uptime:\t\t\t"`uptime | awk '{print $3,$4}' | sed 's/,//'`

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 -f':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \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%"), $3/$2*100}'`

echo -e "swap usage:\t"`free | awk '/swap/{printf("%.2f%"), $3/$2*100}'`

echo -e "cpu usage:\t"`cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1`

echo ""

echo -e "-------------------------------disk usage >80%-------------------------------"

df -ph | sed s/%//g | awk '{ if($5 > 80) print $0;}'

echo ""

echo -e "-------------------------------for wwn details-------------------------------"

vserver=$(lscpu | grep hypervisor | wc -l)

if [ $vserver -gt 0 ]

then

echo "$(hostname) is a vm"

else

cat /sys/class/fc_host/host?/port_name

fi

echo ""

echo -e "-------------------------------oracle db instances---------------------------"

if id oracle >/dev/null 2>&1; then

/bin/ps -ef|grep pmon

then

else

echo "oracle user does not exist on $(hostname)"

fi

echo ""

if (( $(cat /etc/*-release | grep -w "oracle|red hat|centos|fedora" | wc -l) > 0 ))

then

echo -e "-------------------------------package updates-------------------------------"

yum updateinfo summary | grep 'security|bugfix|enhancement'

echo -e "-----------------------------------------------------------------------------"

else

echo -e "-------------------------------package updates-------------------------------"

cat /var/lib/update-notifier/updates-available

echo -e "-----------------------------------------------------------------------------"

fi

把上面脚本内容保存到一个文件 system-info.sh,之后添加可执行权限:

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

当脚本准备好后,把脚本文件的路径加到 .bash_profile 文件末尾(红帽系列的系统:centos、oracle linux 和 fedora):

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

执行以下命令,来让修改的内容生效:

# source ~root/.bash_profile

对于 debian 系统的系统,你可能需要把文件路径加到 .profile 文件中:

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

运行以下命令使修改生效:

# source ~root/.profile

你以前运行上面 source 命令时可能见过类似下面的输出。从下次开始,你在每次登录 shell 时会看到这些信息。当然,如果有必要你也可以随时手动执行这个脚本。

-------------------------------system information---------------------------

hostname: daygeek-y700

uptime: 1:20 1

manufacturer: lenovo

product name: 80nv

version: lenovo ideapad y700-15isk

serial number: aa0cmrn1

machine type: physical

operating system: manjaro linux

kernel: 4.19.80-1-manjaro

architecture: x86_64

processor name: intel(r) core(tm) i7-6700hq cpu @ 2.60ghz

active user: daygeek renu thanu

system 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%-------------------------------

filesystem size used avail use mounted on

/dev/nvme0n1p1 217g 202g 4.6g 98 /

/dev/loop0 109m 109m 0 100 /var/lib/snapd/snap/odrive-unofficial/2

/dev/loop1 91m 91m 0 100 /var/lib/snapd/snap/core/6405

/dev/loop2 90m 90m 0 100 /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)

-----------------------------------------------------------------------------

总结

以上所述是小编给大家介绍的bash 脚本实现每次登录到 shell 时可以查看 linux 系统信息,希望对大家有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值