帮我写个脚本,我要实现linux系统自动巡检
时间: 2024-01-30 14:26:33 浏览: 152
Linux系统巡检脚本
当然,我可以帮你编写一个简单的脚本来实现Linux系统的自动巡检。以下是一个示例脚本:
```bash
#!/bin/bash
# 巡检主机信息
echo "Hostname: $(hostname)"
echo "Kernel Version: $(uname -r)"
echo "Uptime: $(uptime)"
# 检查CPU使用情况
echo "CPU Usage:"
top -bn1 | grep "Cpu(s)" | awk '{print " CPU Usage: " $2 "%"}'
# 检查内存使用情况
echo "Memory Usage:"
free -h | awk 'NR==2{print " Total: " $2 " Used: " $3 " Free: " $4}'
# 检查磁盘空间使用情况
echo "Disk Space Usage:"
df -h | awk '$NF=="/"{print " Total: " $2 " Used: " $3 " Available: " $4 " Usage: " $5}'
# 检查网络连接情况
echo "Network Connections:"
netstat -ant | awk '{print " " $6}' | sort | uniq -c
# 检查已安装的软件包数量
echo "Installed Packages:"
if command -v dpkg >/dev/null 2>&1; then
echo " $(dpkg-query -f '${binary:Package}\n' -W | wc -l) packages installed"
elif command -v rpm >/dev/null 2>&1; then
echo " $(rpm -qa | wc -l) packages installed"
else
echo " Unable to determine the number of installed packages"
fi
# 检查系统日志
echo "System Logs:"
tail -n 20 /var/log/syslog
```
将以上代码保存到一个脚本文件(例如`autodetect.sh`),然后在终端中运行`chmod +x autodetect.sh`命令赋予脚本执行权限。最后,运行`./autodetect.sh`即可执行自动巡检并输出系统信息。
请注意,此脚本只是一个简单的示例,你可以根据自己的需求进行修改和扩展。
阅读全文