1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > shell脚本案例-登录系统的时候自动显示服务器资源信息脚本

shell脚本案例-登录系统的时候自动显示服务器资源信息脚本

时间:2019-02-20 17:25:38

相关推荐

shell脚本案例-登录系统的时候自动显示服务器资源信息脚本

需求:

写一个脚本,实时显示机器的IP,hostname,cpu,内存,负载和磁盘的信息。

并在登录系统的时候显示。

## 切换系统语言为英文LANG=en_US.UTF8## 获取IPip_info=`hostname -I|awk '{print $1}'`## 打印CPU相关信息cpu_info1=`cat /proc/cpuinfo |grep 'model name'|tail -1 |awk -F: '{print $2}'|sed 's/^ //g'|awk '{print $1,$3,$4,$NF}'`## 打印cpu核心数cpu_info2=`cat /proc/cpuinfo |grep "physical id"|sort |uniq -c|wc -l`## 打印hostnameserv_info=`hostname `## 打印磁盘大小,且把,去掉disk_info=` fdisk -l |grep Disk |egrep -v 'identifier|type' |awk '{print $2,$3,$4}' |sed 's/,//g'`## 打印总内存mem_info=`free -m |grep "Mem"|awk '{print "Total",$1,$2"M"}'`## 打印最近的负载load_info=`uptime |awk '{print "Current Load: "$(NF-2)}'|sed 's/\,//g'`list(){echo "-------------------------"echo IPADDR:${ip_info}echo HOST:$serv_infoecho CPU:${cpu_info1} X${cpu_info2}echo DISK:$disk_infoecho MEMORY:$mem_infoecho LOAD:$load_info}listlist >/etc/motd

执行结果:

直接执行脚本

[root@itlaoxin41 ~]# vim a.sh[root@itlaoxin41 ~]# chmod +x a.sh[root@itlaoxin41 ~]# ./a.sh -------------------------IPADDR:192.168.1.41HOST:itlaoxin41CPU:Intel(R) i5-9300HF CPU 2.40GHz X1DISK:/dev/sda: 21.5 GB /dev/mapper/centos-root: 18.2 GB /dev/mapper/centos-swap: 2147 MBMEMORY:Total Mem: 1819MLOAD:Current Load: 0.09

测试登录系统的时候是否显示:

[root@itlaoxin41 ~]# ssh 192.168.1.41The authenticity of host '192.168.1.41 (192.168.1.41)' can't be established.ECDSA key fingerprint is SHA256:9uJW2mOqzhYy5gtMsTHmt4V584na0LFqEGYXxA4rUM8.ECDSA key fingerprint is MD5:42:35:b0:99:2c:04:c5:94:e6:f4:74:6d:af:ec:07:29.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.1.41' (ECDSA) to the list of known hosts.root@192.168.1.41's password: Last login: Thu Dec 8 19:47:47 **-------------------------IPADDR:192.168.1.41HOST:itlaoxin41CPU:Intel(R) i5-9300HF CPU 2.40GHz X1DISK:/dev/sda: 21.5 GB /dev/mapper/centos-root: 18.2 GB /dev/mapper/centos-swap: 2147 MBMEMORY:Total Mem: 1819MLOAD:Current Load: 0.09

可以看到登录系统的时候,会自动显示监控信息

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。