.bashrc
用于设置用户的 Bash shell 环境,在每次打开一个新的终端窗口或启动一个新的 Bash 会话时被执行
代码
login_check.sh
#!/bin/bash
clear
LogFileName=polling.$(date +%F-%T)
EchoFormat=$(for (( i=0; i<30; i++ )); do echo -n "="; done)
# 显示标题
show_header() {
printf "${EchoFormat} %s ${EchoFormat}\n" "$1"
}
# 系统信息
SystemInfo() {
show_header "系统信息"
printf "系统类型: %-10s\n" "$(uname -a | awk '{print $NF}')"
printf "系统版本: %-10s\n" "$(cat /etc/redhat-release)"
printf "内核信息: %-10s\n" "$(uname -r)"
printf "主机名: %-10s\n" "$(uname -n)"
printf "编码格式: %-10s\n" "${LANG}"
printf "系统当前时间: %-10s %-10s\n" "$(date +%F)" "$(date +%T)"
printf "系统运行负载: %-4s %-1s\n" "$(uptime | awk -F: '{print $5}' | awk -F, '{print $1}')" "%"
printf "系统运行天数: %-10s\n" "$(uptime | awk '{print $3}')"
printf "在线用户人数: %-3s\n" "$(w | tail -n +3 | wc -l)"
printf "SELinux: %-10s\n" "$(grep 'SELINUX=[d|e|p]' /etc/selinux/config | awk -F= '{print $2}')"
echo -e "最后一次修改时间: $(uptime -p)"
echo -e "IP地址: $(hostname -I | cut -d' ' -f1)"
echo -e "Cpu处理器: $(lscpu | grep 'Model name:' | sed 's/Model name:\s*//')"
echo -e "内存空间: $(free -h | awk '/^Mem:/ { print $3 "/" $2 }')"
echo -e "交换空间: $(free -h | awk '/^Swap:/ { print $3 "/" $2 }')"
}
# CPU信息
CpuInfo() {
show_header "CPU排名前十信息"
top -b -n 1 | head -n 17 | tail -n 10
}
# 内存信息
MemoryInfo() {
show_header "内存信息"
printf "总共内存: %-1s\n" "$(free -mh | awk 'NR==2 {print $2}')"
printf "使用内存: %-1s\n" "$(free -mh | awk 'NR==2 {print $3}')"
printf "剩余内存: %-1s\n" "$(free -mh | awk 'NR==2 {print $4}')"
printf "内存使用占比: %-1s %-1s\n" "$(free | awk '/^Mem:/ {print $6/$2*100}' | cut -c1-5)" "%"
}
# 磁盘使用量
Disk_Info() {
show_header "各分区使用率"
df -hT | grep -v "docker"
echo
}
# Swap信息
SwapInfo() {
show_header "Swap信息"
printf "Swap总大小: %-1s\n" "$(free -mh | awk 'NR==3 {print $2}')"
printf "已用Swap: %-1s\n" "$(free -mh | awk 'NR==3 {print $3}')"
printf "可用Swap: %-1s\n" "$(free -mh | awk 'NR==3 {print $4}')"
}
# 网络信息
NetworkInfo() {
show_header "网络信息"
printf "IP地址: %-1s\n" "$(ip addr | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | grep -v '^fe80::')"
printf "网关: %-1s\n" "$(ip route show default | awk '/default/ {print $3}')"
printf "DNS: %-1s\n" "$(grep 'nameserver' /etc/resolv.conf | awk '{print $2}')"
if (ping -c2 -w2 www.baidu.com &>/dev/null); then
printf "网络是否连通: %s\n" "是"
else
printf "网络是否连通: %s\n" "否"
fi
}
# Docker检查
DockerInfo() {
show_header "docker运行情况"
local running_containers=$(docker ps --format "{{.Names}}")
local exited_containers=$(docker ps --format "{{.Names}}" | grep Exited)
printf "当前正在运行的容器:\n %s\n" "$running_containers"
printf "当前没有运行的容器:\n %s\n" "$exited_containers"
}
# 主函数
main() {
SystemInfo
CpuInfo
MemoryInfo
Disk_Info
SwapInfo
NetworkInfo
DockerInfo
}
# 执行主函数
main
登录自动执行
cp login_check.sh /root/.login_check.sh
chmod +x /root/.login_check.sh
echo "/root/.login_check.sh" >> /root/.bashrc