Linux Shell

Dos 攻击防范(自动屏蔽攻击 IP)

  1. #!/bin/bash
  2. DATE=$(date +%d/%b/%Y:%H:%M)
  3. LOG_FILE=/usr/local/nginx/logs/demo2.access.log
  4. ABNORMAL_IP=$(tail -n5000 $LOG_FILE |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>10)print i}')
  5. for IP in $ABNORMAL_IP; do
  6. if [ $(iptables -vnL |grep -c "$IP") -eq 0 ]; then
  7. iptables -I INPUT -s $IP -j DROP
  8. echo "$(date +'%F_%T') $IP" >> /tmp/drop_ip.log
  9. fi
  10. done

Linux 系统发送告警脚本

  1. # yum install mailx
  2. # vi /etc/mail.rc
  3. set from=baojingtongzhi@163.com smtp=smtp.163.com
  4. set smtp-auth-user=baojingtongzhi@163.com smtp-auth-password=123456
  5. set smtp-auth=login

MySQL 数据库备份单循环

  1. #!/bin/bash
  2. DATE=$(date +%F_%H-%M-%S)
  3. HOST=localhost
  4. USER=backup
  5. PASS=123.com
  6. BACKUP_DIR=/data/db_backup
  7. DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")
  8. for DB in $DB_LIST; do
  9. BACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sql
  10. if ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; then
  11. echo "$BACKUP_NAME 备份失败!"
  12. fi
  13. done

MySQL 数据库备份多循环

  1. #!/bin/bash
  2. DATE=$(date +%F_%H-%M-%S)
  3. HOST=localhost
  4. USER=backup
  5. PASS=123.com
  6. BACKUP_DIR=/data/db_backup
  7. DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")
  8. for DB in $DB_LIST; do
  9. BACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}
  10. [ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/null
  11. TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)
  12. for TABLE in $TABLE_LIST; do
  13. BACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sql
  14. if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; then
  15. echo "$BACKUP_NAME 备份失败!"
  16. fi
  17. done
  18. done

Nginx 访问访问日志按天切割

  1. #!/bin/bash
  2. LOG_DIR=/usr/local/nginx/logs
  3. YESTERDAY_TIME=$(date -d "yesterday" +%F)
  4. LOG_MONTH_DIR=$LOG_DIR/$(date +"%Y-%m")
  5. LOG_FILE_LIST="default.access.log"
  6. for LOG_FILE in $LOG_FILE_LIST; do
  7. [ ! -d $LOG_MONTH_DIR ] && mkdir -p $LOG_MONTH_DIR
  8. mv $LOG_DIR/$LOG_FILE $LOG_MONTH_DIR/${LOG_FILE}_${YESTERDAY_TIME}
  9. done
  10. kill -USR1 $(cat /var/run/nginx.pid)

Nginx 访问日志分析脚本

  1. #!/bin/bash
  2. # 日志格式: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"
  3. LOG_FILE=$1
  4. echo "统计访问最多的10个IP"
  5. awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr |head -10
  6. echo "----------------------"
  7. echo "统计时间段访问最多的IP"
  8. awk '$4>="[01/Dec/2018:13:20:25" && $4<="[27/Nov/2018:16:20:49"{a[$1]++}END{for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr|head -10
  9. echo "----------------------"
  10. echo "统计访问最多的10个页面"
  11. awk '{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>10)print v,a[v]}}' $LOG_FILE |sort -k2 -nr
  12. echo "----------------------"
  13. echo "统计访问页面状态码数量"
  14. awk '{a[$7" "$9]++}END{for(v in a){if(a[v]>5)print v,a[v]}}'

查看网卡实时流量脚本

  1. #!/bin/bash
  2. NIC=$1
  3. echo -e " In ------ Out"
  4. while true; do
  5. OLD_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev)
  6. OLD_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev)
  7. sleep 1
  8. NEW_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev)
  9. NEW_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev)
  10. IN=$(printf "%.1f%s" "$((($NEW_IN-$OLD_IN)/1024))" "KB/s")
  11. OUT=$(printf "%.1f%s" "$((($NEW_OUT-$OLD_OUT)/1024))" "KB/s")
  12. echo "$IN $OUT"
  13. sleep 1
  14. done

服务器系统配置初始化脚本

  1. #/bin/bash
  2. # 设置时区并同步时间
  3. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  4. if ! crontab -l |grep ntpdate &>/dev/null ; then
  5. (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
  6. fi
  7. # 禁用selinux
  8. sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config
  9. # 关闭防火墙
  10. if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
  11. systemctl stop firewalld
  12. systemctl disable firewalld
  13. elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
  14. service iptables stop
  15. chkconfig iptables off
  16. fi
  17. # 历史命令显示操作时间
  18. if ! grep HISTTIMEFORMAT /etc/bashrc; then
  19. echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc
  20. fi
  21. # SSH超时时间
  22. if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
  23. echo "export TMOUT=600" >> /etc/profile
  24. fi
  25. # 禁止root远程登录
  26. sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
  27. # 禁止定时任务向发送邮件
  28. sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
  29. # 设置最大打开文件数
  30. if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
  31. cat >> /etc/security/limits.conf << EOF
  32. * soft nofile 65535
  33. * hard nofile 65535
  34. EOF
  35. fi
  36. # 系统内核优化
  37. cat >> /etc/sysctl.conf << EOF
  38. net.ipv4.tcp_syncookies = 1
  39. net.ipv4.tcp_max_tw_buckets = 20480
  40. net.ipv4.tcp_max_syn_backlog = 20480
  41. net.core.netdev_max_backlog = 262144
  42. net.ipv4.tcp_fin_timeout = 20
  43. EOF
  44. # 减少SWAP使用
  45. echo "0" > /proc/sys/vm/swappiness
  46. # 安装系统性能分析工具及其他
  47. yum install gcc make autoconf vim sysstat net-tools iostat if

监控 100 台服务器磁盘利用率脚本

  1. #!/bin/bash
  2. HOST_INFO=host.info
  3. for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
  4. USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
  5. PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
  6. TMP_FILE=/tmp/disk.tmp
  7. ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE
  8. USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
  9. for USE_RATE in $USE_RATE_LIST; do
  10. PART_NAME=${USE_RATE%=*}
  11. USE_RATE=${USE_RATE#*=}
  12. if [ $USE_RATE -ge 80 ]; then
  13. echo "Warning: $PART_NAME Partition usage $USE_RATE%!"
  14. fi
  15. done
  16. done