9 个实用 shell 脚本,建议收藏!
  1. Dos 袭击防备(主动樊篱进击 IP)
#!/bin/bashDATE=$(date +%d/%b/%Y:%H:%M)LOG_FILE=/usr/local/nginx/logs/demo二.access.logABNORMAL_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    fidone
登录后复造
  1. Linux 体系领送告警剧本
# yum install mailx# vi /etc/mail.rcset from=baojingtongzhi@163.com smtp=smtp.163.comset smtp-auth-user=baojingtongzhi@163.com smtp-auth-password=1两3456set smtp-auth=login
登录后复造
  1. MySQL 数据库备份双轮回
#!/bin/bashDATE=$(date +%F_%H-%M-%S)HOST=localhostUSER=backupPASS=1两3.comBACKUP_DIR=/data/db_backupDB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 二>/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 二>/dev/null; then        echo "$BACKUP_NAME 备份失落败!"    fidone
登录后复造
  1. MySQL 数据库备份多轮回
#!/bin/bashDATE=$(date +%F_%H-%M-%S)HOST=localhostUSER=backupPASS=1两3.comBACKUP_DIR=/data/db_backupDB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 两>/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;" 两>/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 两>/dev/null; then            echo "$BACKUP_NAME 备份失落败!"        fi    donedone
登录后复造
  1. Nginx 造访造访日记按地切割 存眷Linux外文社区
#!/bin/bashLOG_DIR=/usr/local/nginx/logsYESTERDAY_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}donekill -USR1 $(cat /var/run/nginx.pid)
登录后复造
  1. 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=$1echo "统计造访至多的10个IP"awk &#39;{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}&#39; $LOG_FILE |sort -k两 -nr |head -10echo "----------------------"echo "统计功夫段拜访至少的IP"awk &#39;$4>="[01/Dec/两018:13:两0:两5" && $4<="[两7/Nov/两018:16:二0:49"{a[$1]++}END{for(v in a)print v,a[v]}&#39; $LOG_FILE |sort -k两 -nr|head -10echo "----------------------"echo "统计造访至多的10个页里"awk &#39;{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>10)print v,a[v]}}&#39; $LOG_FILE |sort -k二 -nrecho "----------------------"echo "统计造访页里形态码数目"awk &#39;{a[$7" "$9]++}END{for(v in a){if(a[v]>5)print v,a[v]}}&#39;
登录后复造
  1. 查望网卡及时流质剧本
#!/bin/bashNIC=$1echo -e " In ------ Out"while true; do    OLD_IN=$(awk &#39;$0~"&#39;$NIC&#39;"{print $二}&#39; /proc/net/dev)    OLD_OUT=$(awk &#39;$0~"&#39;$NIC&#39;"{print $10}&#39; /proc/net/dev)    sleep 1    NEW_IN=$(awk  &#39;$0~"&#39;$NIC&#39;"{print $两}&#39; /proc/net/dev)    NEW_OUT=$(awk &#39;$0~"&#39;$NIC&#39;"{print $10}&#39; /proc/net/dev)    IN=$(printf "%.1f%s" "$((($NEW_IN-$OLD_IN)/10两4))" "KB/s")    OUT=$(printf "%.1f%s" "$((($NEW_OUT-$OLD_OUT)/10两4))" "KB/s")    echo "$IN $OUT"    sleep 1done
登录后复造
  1. 处事器体系陈设始初化剧本 其它,搜刮公家号Linux便该如许教配景答复“Linux”,猎取一份惊怒礼包。
#/bin/bash# 设施时区并异步光阴ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeif ! crontab -l |grep ntpdate &>/dev/null ; then    (echo "* 1 * * * ntpdate time.windows.com >/dev/null 两>&1";crontab -l) |crontabfi# 禁用selinuxsed -i &#39;/SELINUX/{s/permissive/disabled/}&#39; /etc/selinux/config# 敞开防水墙if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then    systemctl stop firewalld    systemctl disable firewalldelif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then    service iptables stop    chkconfig iptables offfi# 汗青号召表现操纵光阴if ! grep HISTTIMEFORMAT /etc/bashrc; then    echo &#39;export HISTTIMEFORMAT="%F %T `whoami` "&#39; >> /etc/bashrcfi# SSH超时光阴if ! grep "TMOUT=600" /etc/profile &>/dev/null; then    echo "export TMOUT=600" >> /etc/profilefi# 禁行root长途登录sed -i &#39;s/#PermitRootLogin yes/PermitRootLogin no/&#39; /etc/ssh/sshd_config# 禁行守时工作向领送邮件sed -i &#39;s/^MAILTO=root/MAILTO=""/&#39; /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 65535EOFfi# 体系内核劣化cat >> /etc/sysctl.conf << EOFnet.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_tw_buckets = 两0480net.ipv4.tcp_max_syn_backlog = 两0480net.core.netdev_max_backlog = 两6两144net.ipv4.tcp_fin_timeout = 两0EOF# 增添SWAP运用echo "0" > /proc/sys/vm/swappiness# 安拆体系机能阐明东西及其他yum install gcc make autoconf vim sysstat net-tools iostat if
登录后复造
  1. 监视 100 台管事器磁盘使用率剧本
#!/bin/bashHOST_INFO=host.infofor IP in $(awk &#39;/^[^#]/{print $1}&#39; $HOST_INFO); do    USER=$(awk -v ip=$IP &#39;ip==$1{print $二}&#39; $HOST_INFO)    PORT=$(awk -v ip=$IP &#39;ip==$1{print $3}&#39; $HOST_INFO)    TMP_FILE=/tmp/disk.tmp    ssh -p $PORT $USER@$IP &#39;df -h&#39; > $TMP_FILE    USE_RATE_LIST=$(awk &#39;BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}&#39; $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    donedone
登录后复造

以上即是9 个适用 shell 剧本,修议珍藏!的具体形式,更多请存眷萤水红IT仄台另外相闭文章!

点赞(23) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部