今天突发奇想,想要每次ssh登录的时候都可以看到系统的相关信息,接下来就把这个脚本分享给大家吧。当然了我们也可以在评论区讨论一下有哪些可以在改进的。
[root@localhost ~]# cat /etc/profile.d/system-info.sh
#!/bin/bash
# 编写一个函数用于获取系统的时间,时区,以及系统相关资源信息,打印出来
get_system_info() {
# 获取系统时间
time=$(date +"%Y-%m-%d %H:%M:%S")
# 获取系统时区
timezone=$(timedatectl | grep "Time zone" | awk '{print $3}')
# 获取CPU数量
cpu_count=$(grep -c '^processor' /proc/cpuinfo)
# 获取内存使用率
memory_usage=$(free | awk 'NR==2{used=$3+$6; printf "%.2f%% (%.2fG/%.2fG)\n", (used*100)/$2, used/1024/1024, $2/1024/1024}')
# 获取CPU使用率
cpu_usage=$(top -b -n 1 | grep "Cpu(s)" | awk '{print $2 + $4}')
# 获取磁盘使用情况
disk_usage=$(df -h | awk '$NF=="/"{printf "%.2f%% (%s/%s)\n", $5, $3,$2 }')
echo "System Information:"
echo "Time: $time"
echo "Time Zone: $timezone"
echo "CPU Count: $cpu_count"
echo "CPU Usage: ${cpu_usage}%"
echo "Memory Usage: $memory_usage"
echo "Disk Usage: $disk_usage"
}
# 调用函数并获取系统信息
get_system_info
[root@localhost ~]# chmod +x /etc/profile.d/system-info.sh
接下来展示效果
Connecting to 192.168.207.131:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Tue Jun 4 00:16:41 2024 from 192.168.207.1
System Information:
Time: 2024-06-04 00:16:54
Time Zone: America/New_York
CPU Count: 2
CPU Usage: 3.2%
Memory Usage: 8.26% (0.30G/3.68G)
Disk Usage: 3.00% (1.3G/50G)
[root@localhost ~]#