• 13 个比较实用 Shell 脚本


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

    #!/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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ② Linux 系统发送告警脚本

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

    ③ MySQL 数据库备份单循环

    #!/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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ④ MySQL 数据库备份多循环

    #!/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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ⑤ Nginx 访问访问日志按天切割

    #!/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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ⑥ Nginx 访问日志分析脚本

    #!/bin/bash
    # 日志格式: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"
    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
    • 15
    • 16
    • 17

    ⑦ 查看网卡实时流量脚本

    #!/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

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

    #/bin/bash
    # 设置时区并同步时间
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    if ! crontab -l |grep ntpdate &>/dev/null ; then
        (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
    fi
    
    # 禁用selinux
    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
    
    # SSH超时时间
    if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
        echo "export TMOUT=600" >> /etc/profile
    fi
    
    # 禁止root远程登录
    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
    
    # 减少SWAP使用
    echo "0" > /proc/sys/vm/swappiness
    
    # 安装系统性能分析工具及其他
    yum install gcc make autoconf vim sysstat net-tools iostat if
    
    • 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

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

    #!/bin/bash
    HOST_INFO=host.info
    for 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    10 根据PID过滤进程所有信息

     #! /bin/bash
    # Function: 根据用户输入的PID,过滤出该PID所有的信息
    read -p "请输入要查询的PID: " P
    n=`ps -aux| awk '$2~/^'$P'$/{print $11}'|wc -l`
    if [ $n -eq 0 ];then
     echo "该PID不存在!!"
     exit
    fi
    echo "--------------------------------"
    echo "进程PID: $P"
    echo "进程命令:`ps -aux| awk '$2~/^'$P'$/{print $11}'`"
    echo "进程所属用户: `ps -aux| awk '$2~/^'$P'$/{print $1}'`"
    echo "CPU占用率:`ps -aux| awk '$2~/^'$P'$/{print $3}'`%"
    echo "内存占用率:`ps -aux| awk '$2~/^'$P'$/{print $4}'`%"
    echo "进程开始运行的时刻:`ps -aux| awk '$2~/^'$P'$/{print $9}'`"
    echo "进程运行的时间:`ps -aux| awk '$2~/^'$P'$/{print $10}'`"
    echo "进程状态:`ps -aux| awk '$2~/^'$P'$/{print $8}'`"
    echo "进程虚拟内存:`ps -aux| awk '$2~/^'$P'$/{print $5}'`"
    echo "进程共享内存:`ps -aux| awk '$2~/^'$P'$/{print $6}'`"
    echo "--------------------------------"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    11 根据进程名过滤进程信息

    会显示出该进程名包含的所有线程

    #! /bin/bash
    # Function: 根据输入的程序的名字过滤出所对应的PID,并显示出详细信息,如果有几个PID,则全部显示
    read -p "请输入要查询的进程名:" NAME
    N=`ps -aux | grep $NAME | grep -v grep | wc -l` ##统计进程总数
    if [ $N -le 0 ];then
      echo "该进程名没有运行!"
    fi
    i=1
    while [ $N -gt 0 ]
    do
      echo "进程PID: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $2}'`"
      echo "进程命令:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"
      echo "进程所属用户: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $1}'`"
      echo "CPU占用率:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $3}'`%"
      echo "内存占用率:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $4}'`%"
      echo "进程开始运行的时刻:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $9}'`"
      echo "进程运行的时间:` ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"
      echo "进程状态:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $8}'`"
      echo "进程虚拟内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $5}'`"
      echo "进程共享内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $6}'`"
      echo "***************************************************************"
      let N-- i++
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    12 shier根据用户名查询该用户的相关信息

    #! /bin/bash
    # Function:根据用户名查询该用户的所有信息
    read -p "请输入要查询的用户名:" A
    echo "------------------------------"
    n=`cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}' | wc -l`
    if [ $n -eq 0 ];then
    echo "该用户不存在"
    echo "------------------------------"
    else
      echo "该用户的用户名:$A"
      echo "该用户的UID:`cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $3}'`"
      echo "该用户的组为:`id $A | awk {'print $3'}`"
      echo "该用户的GID为:`cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $4}'`"
      echo "该用户的家目录为:`cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $6}'`"
      Login=`cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $7}'`
      if [ $Login == "/bin/bash" ];then
      echo "该用户有登录系统的权限!!"
      echo "------------------------------"
      elif [ $Login == "/sbin/nologin" ];then
      echo "该用户没有登录系统的权限!!"
      echo "------------------------------"
      fi
    fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    13 加固系统的一些配置

    #! /bin/bash
    # Function:对账户的密码的一些加固
    read -p "设置密码最多可多少天不修改:" A
    read -p "设置密码修改之间最小的天数:" B
    read -p "设置密码最短的长度:" C
    read -p "设置密码失效前多少天通知用户:" D
    sed -i '/^PASS_MAX_DAYS/c\PASS_MAX_DAYS '$A'' /etc/login.defs
    sed -i '/^PASS_MIN_DAYS/c\PASS_MIN_DAYS '$B'' /etc/login.defs
    sed -i '/^PASS_MIN_LEN/c\PASS_MIN_LEN '$C'' /etc/login.defs
    sed -i '/^PASS_WARN_AGE/c\PASS_WARN_AGE '$D'' /etc/login.defs
     
    echo "已对密码进行加固,新用户不得和旧密码相同,且新密码必须同时包含数字、小写字母,大写字母!!"
    sed -i '/pam_pwquality.so/c\password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= difok=1 minlen=8 ucredit=-1 lcredit=-1 dcredit=-1' /etc/pam.d/system-auth
     
    echo "已对密码进行加固,如果输入错误密码超过3次,则锁定账户!!"
    n=`cat /etc/pam.d/sshd | grep "auth required pam_tally2.so "|wc -l`
    if [ $n -eq 0 ];then
    sed -i '/%PAM-1.0/a\auth required pam_tally2.so deny=3 unlock_time=150 even_deny_root root_unlock_time300' /etc/pam.d/sshd
    fi
     
    echo  "已设置禁止root用户远程登录!!"
    sed -i '/PermitRootLogin/c\PermitRootLogin no'  /etc/ssh/sshd_config
     
    read -p "设置历史命令保存条数:" E
    read -p "设置账户自动注销时间:" F
    sed -i '/^HISTSIZE/c\HISTSIZE='$E'' /etc/profile
    sed -i '/^HISTSIZE/a\TMOUT='$F'' /etc/profile
     
    echo "已设置只允许wheel组的用户可以使用su命令切换到root用户!"
    sed -i '/pam_wheel.so use_uid/c\auth required pam_wheel.so use_uid ' /etc/pam.d/su
    n=`cat /etc/login.defs | grep SU_WHEEL_ONLY | wc -l`
    if [ $n -eq 0 ];then
    echo SU_WHEEL_ONLY yes >> /etc/login.defs
    fi
     
    echo "即将对系统中的账户进行检查...."
    echo "系统中有登录权限的用户有:"
    awk -F: '($7=="/bin/bash"){print $1}' /etc/passwd
    echo "********************************************"
    echo "系统中UID=0的用户有:"
    awk -F: '($3=="0"){print $1}' /etc/passwd
    echo "********************************************"
    N=`awk -F: '($2==""){print $1}' /etc/shadow|wc -l`
    echo "系统中空密码用户有:$N"
    if [ $N -eq 0 ];then
     echo "恭喜你,系统中无空密码用户!!"
     echo "********************************************"
    else
     i=1
     while [ $N -gt 0 ]
     do
        None=`awk -F: '($2==""){print $1}' /etc/shadow|awk 'NR=='$i'{print}'`
        echo "------------------------"
        echo $None
        echo "必须为空用户设置密码!!"
        passwd $None
        let N--
     done
     M=`awk -F: '($2==""){print $1}' /etc/shadow|wc -l`
     if [ $M -eq 0 ];then
      echo "恭喜,系统中已经没有空密码用户了!"
     else
    echo "系统中还存在空密码用户:$M"
     fi
    fi
     
    echo "即将对系统中重要文件进行锁定,锁定后将无法添加删除用户和组"
    read -p "警告:此脚本运行后将无法添加删除用户和组!!确定输入Y,取消输入N;Y/N:" i
    case $i in
          [Y,y])
                chattr +i /etc/passwd
                chattr +i /etc/shadow
                chattr +i /etc/group
                chattr +i /etc/gshadow
                echo "锁定成功!"
    ;;
          [N,n])
                chattr -i /etc/passwd
                chattr -i /etc/shadow
                chattr -i /etc/group
                chattr -i /etc/gshadow
                echo "取消锁定成功!!"
    ;;
           *)
                echo "请输入Y/y or N/n"
    esac
    
    • 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
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
  • 相关阅读:
    自然人如何在浙江法院网上完成诉讼——具体流程
    短视频创作,变现的建议、变现方式和举例,建议收藏反复阅读-上
    Rust权威指南之通用集合类型
    基于Headless构建高可用spark+pyspark集群
    低代码平台协同OA升级,促进金融企业信息化建设
    基于单片机的车载太阳能板自动跟踪系统研究
    XAF中XPO与EFCore的探讨
    生物识别技术在汽车领域带来了巨大变革
    高精度气象模拟软件WRF(Weather Research Forecasting)实践技术
    【Unity】万人同屏, 从入门到放弃之——多线程RVO避障
  • 原文地址:https://blog.csdn.net/weixin_56477161/article/details/128115537