#!/bin/bash
#
符号VAR="value"
,使用时$VAR
或${VAR}
$0
:脚本名称$1-$9
:位置参数$#
:参数个数$?
:上一条命令的退出状态grep
, awk
, sed
, cut
find
, xargs
, rsync
, scp
top
, vmstat
, iostat
, netstat
ps
, pgrep
, pkill
, killall
#!/bin/bash
# 监控CPU、内存和磁盘使用情况
HOSTNAME=$(hostname)
DATE=$(date "+%Y-%m-%d %H:%M:%S")
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
MEM_USAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | tr -d '%')
echo "===== 系统监控报告 [$DATE] ====="
echo "主机名: $HOSTNAME"
echo "CPU使用率: $CPU_USAGE%"
echo "内存使用率: $MEM_USAGE%"
echo "根分区使用率: $DISK_USAGE%"
# 设置阈值告警
if (( $(echo "$CPU_USAGE > 90" | bc -l) )); then
echo "警告: CPU使用率过高!"
fi
if (( $(echo "$MEM_USAGE > 85" | bc -l) )); then
echo "警告: 内存使用率过高!"
fi
if [ "$DISK_USAGE" -gt 90 ]; then
echo "警告: 磁盘空间不足!"
fi
#!/bin/bash
# 分析Nginx访问日志
LOG_FILE="/var/log/nginx/access.log"
REPORT_FILE="/tmp/nginx_report_$(date +%Y%m%d).txt"
# 统计访问量前10的IP
echo "===== 访问量前10的IP =====" > $REPORT_FILE
awk '{print $1}' $LOG_FILE | sort | uniq -c | sort -nr | head -10 >> $REPORT_FILE
# 统计访问量前10的URL
echo -e "\n===== 访问量前10的URL =====" >> $REPORT_FILE
awk '{print $7}' $LOG_FILE | sort | uniq -c | sort -nr | head -10 >> $REPORT_FILE
# 统计HTTP状态码
echo -e "\n===== HTTP状态码统计 =====" >> $REPORT_FILE
awk '{print $9}' $LOG_FILE | sort | uniq -c | sort -nr >> $REPORT_FILE
# 统计错误请求(4xx/5xx)
echo -e "\n===== 错误请求(4xx/5xx) =====" >> $REPORT_FILE
awk '$9 ~ /^[45][0-9][0-9]$/ {print $9,$7,$1}' $LOG_FILE | sort | uniq -c | sort -nr >> $REPORT_FILE
echo "日志分析完成,报告已保存至: $REPORT_FILE"
#!/bin/bash
# 批量在多台服务器上执行命令
SERVERS=("server1" "server2" "server3" "server4")
USER="admin"
COMMAND="$1"
if [ -z "$COMMAND" ]; then
echo "用法: $0 '要执行的命令'"
exit 1
fi
for server in "${SERVERS[@]}"; do
echo -e "\n===== 在 $server 上执行: $COMMAND ====="
ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $USER@$server "$COMMAND"
if [ $? -ne 0 ]; then
echo "错误: 在 $server 上执行命令失败"
fi
done
#!/usr/bin/expect
# 自动SSH登录并执行命令
set timeout 30
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh $username@$host
expect {
"*yes/no*" { send "yes\r"; exp_continue }
"*password*" { send "$password\r" }
}
expect "*$ " { send "$command\r" }
expect "*$ " { send "exit\r" }
expect eof
---
- name: 配置Web服务器
hosts: webservers
become: yes
tasks:
- name: 安装Nginx
apt:
name: nginx
state: latest
update_cache: yes
- name: 确保Nginx服务运行
service:
name: nginx
state: started
enabled: yes
- name: 部署网站配置文件
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/sites-available/default
notify:
- 重启Nginx
handlers:
- name: 重启Nginx
service:
name: nginx
state: restarted
# 每天凌晨3点执行备份
0 3 * * * /usr/local/bin/backup_script.sh
# 每5分钟检查一次服务状态
*/5 * * * * /usr/local/bin/monitor_service.sh
# 每周一早上6点清理日志
0 6 * * 1 /usr/local/bin/clean_logs.sh
错误处理:
set -e
使脚本在出错时立即退出set -u
检查未定义变量trap
捕获信号并清理临时文件日志记录:
logger
命令写入系统日志安全性:
可维护性:
测试与验证:
bash -n
检查语法通过合理运用这些自动化脚本和技术,可以显著提高Linux运维效率,减少人为错误,并实现更可靠的系统管理。