好久没更新了,实在不知道写点什么好,就在网上找了一些资源。正文是杜老师整理的 9 个实用 Shell 脚本,供有需要的小伙伴参考。需要注意的是,这些脚本为杜老师收集,并没有测试过,小伙伴们使用之前要先测试。
写在最前 常来的小伙伴应该发现杜老师说近两周没有更新了,过年期间确实有太多的事情需要处理,各种亲戚走动等等。目前已经处理差不多了,工作上的节奏也已慢慢稳定,近期开始补上之前拖更文章。开头也祝愿来访的小伙伴们龙年大吉,博客访问蒸蒸日上,身体和服务器健健康康,心想和收入皆遂意!
DoS 攻击防范自动屏蔽攻击 IP 1 2 3 4 5 6 7 8 9 10 #!/bin/bash DATE=$(date +%d/%b/%Y:%H:%M) LOG_FILE=/usr/local/nginx/logs/demo2.access.log ABNORMAL_IP=$(tail -n5000 $LOG_FILE | grep $DATE | awk '{a[$1]++}END{for(i in a)if(a[i]>10)print i}' ) for IP in $ABNORMAL_IP ; do if [ $(iptables -vnL | grep -c "$IP " ) -eq 0 ]; then iptables -I INPUT -s $IP -j DROP echo "$(date +'%F_%T') $IP " >>/tmp/drop_ip.log fi done
Linux 系统发送告警邮件的脚本 1 2 3 4 5 set from =baojingtongzhi@163.com smtp =smtp.163.comset smtp-auth-user =baojingtongzhi@163.com smtp-auth-password =123456set smtp-auth =login
MySQL 数据库的单循环备份 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash DATE=$(date +%F_%H-%M-%S) HOST=localhost USER=backup PASS=123.com BACKUP_DIR=/data/db_backup 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" ) for DB in $DB_LIST ; do BACKUP_NAME=$BACKUP_DIR /${DB} _${DATE} .sql if ! mysqldump -h$HOST -u$USER -p$PASS -B $DB >$BACKUP_NAME 2>/dev/null; then echo "$BACKUP_NAME 备份失败!" fi done
MySQL 数据库的多循环备份 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash DATE=$(date +%F_%H-%M-%S) HOST=localhost USER=backup PASS=123.com BACKUP_DIR=/data/db_backup 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" ) for DB in $DB_LIST ; do BACKUP_DB_DIR=$BACKUP_DIR /${DB} _${DATE} [ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/null TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB ;show tables;" 2>/dev/null) for TABLE in $TABLE_LIST ; do BACKUP_NAME=$BACKUP_DB_DIR /${TABLE} .sql if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE >$BACKUP_NAME 2>/dev/null; then echo "$BACKUP_NAME 备份失败!" fi done done
Nginx 的访问日志按天切割 1 2 3 4 5 6 7 8 9 10 11 12 #!/bin/bash LOG_DIR=/usr/local/nginx/logs YESTERDAY_TIME=$(date -d "yesterday" +%F) LOG_MONTH_DIR=$LOG_DIR /$(date +"%Y-%m" ) LOG_FILE_LIST="default.access.log" for LOG_FILE in $LOG_FILE_LIST ; do [ ! -d $LOG_MONTH_DIR ] && mkdir -p $LOG_MONTH_DIR mv $LOG_DIR /$LOG_FILE $LOG_MONTH_DIR /${LOG_FILE} _${YESTERDAY_TIME} done kill -USR1 $(cat /var/run/nginx.pid)
Nginx 访问日志的分析脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/bin/bash LOG_FILE=$1 echo "统计访问最多的 10 个 IP" awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}' $LOG_FILE | sort -k2 -nr | head -10 echo "----------------------" echo "统计时间段访问最多 IP" 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 echo "----------------------" echo "统计访问最多 10 个页面" 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 echo "----------------------" echo "统计访问页面状态码的数量" awk '{a[$7" "$9]++}END{for(v in a){if(a[v]>5)print v,a[v]}}'
查看网卡实时流量脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash NIC=$1 echo -e " In ------ Out" while true ; do OLD_IN=$(awk '$0~"' $NIC '"{print $2}' /proc/net/dev) OLD_OUT=$(awk '$0~"' $NIC '"{print $10}' /proc/net/dev) sleep 1 NEW_IN=$(awk '$0~"' $NIC '"{print $2}' /proc/net/dev) NEW_OUT=$(awk '$0~"' $NIC '"{print $10}' /proc/net/dev) IN=$(printf "%.1f%s" "$((($NEW_IN - $OLD_IN) / 1024))" "KB/s" ) OUT=$(printf "%.1f%s" "$((($NEW_OUT - $OLD_OUT) / 1024))" "KB/s" ) echo "$IN $OUT " sleep 1 done
服务器系统配置初始化脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeif ! crontab -l | grep ntpdate &>/dev/null; then ( echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1" crontab -l ) | crontab fi sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then systemctl stop firewalld systemctl disable firewalld elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then service iptables stop chkconfig iptables off fi if ! grep HISTTIMEFORMAT /etc/bashrc; then echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >>/etc/bashrc fi if ! grep "TMOUT=600" /etc/profile &>/dev/null; then echo "export TMOUT=600" >>/etc/profile fi sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then cat >>/etc/security/limits.conf <<EOF * soft nofile 65535 * hard nofile 65535 EOF fi cat >>/etc/sysctl.conf <<EOF net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_tw_buckets = 20480 net.ipv4.tcp_max_syn_backlog = 20480 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_fin_timeout = 20 EOF echo "0" >/proc/sys/vm/swappinessyum install gcc make autoconf vim sysstat net-tools iostat if
监控 100 台服务器磁盘利用率脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 HOST_INFO =host.infofor IP in $(awk '/^[^#]/{print $1}' $HOST_INFO ); do USER =$(awk -v ip =$IP 'ip==$1{print $2}' $HOST_INFO ) PORT =$(awk -v ip =$IP 'ip==$1{print $3}' $HOST_INFO ) TMP_FILE =/tmp/disk.tmp ssh -p $PORT $USER @$IP 'df -h' >$TMP_FILE USE_RATE_LIST =$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE ) for USE_RATE in $USE_RATE_LIST ; do PART_NAME =${USE_RATE%=*} USE_RATE =${USE_RATE#*=} if [ $USE_RATE -ge 80 ]; then echo "Warning: $PART_NAME Partition usage $USE_RATE %!" fi done done
条评论