Operations 23 min read

Essential Bash Scripts for Linux Server Automation and Monitoring

This article compiles a comprehensive set of Bash scripts that automate common Linux server tasks such as file consistency checks, log cleanup, network traffic monitoring, port scanning, user management, firewall rules, and service health checks, providing sysadmins with ready‑to‑use solutions for efficient operations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Essential Bash Scripts for Linux Server Automation and Monitoring

This article provides a collection of Bash scripts for common Linux server administration tasks, ranging from file consistency verification to automated firewall blocking.

1. Detect file consistency between two servers

#!/bin/bash
######################################
# 检测两台服务器指定目录下的文件一致性
#####################################
# 通过对比两台服务器上文件的md5值,达到检测一致性的目的
dir=/data/web
b_ip=192.168.88.10
# 将指定目录下的文件全部遍历出来并作为md5sum命令的参数,进而得到所有文件的md5值,并写入到指定文件中
find $dir -type f | xargs md5sum > /tmp/md5_a.txt
ssh $b_ip "find $dir -type f | xargs md5sum > /tmp/md5_b.txt"
scp $b_ip:/tmp/md5_b.txt /tmp
# 将文件名作为遍历对象进行一一比对
for f in `awk '{print $2}' /tmp/md5_a.txt`; do
    if grep -qw "$f" /tmp/md5_b.txt; then
        md5_a=`grep -w "$f" /tmp/md5_a.txt | awk '{print $1}'`
        md5_b=`grep -w "$f" /tmp/md5_b.txt | awk '{print $1}'`
        if [ $md5_a != $md5_b ]; then
            echo "$f changed."
        fi
    else
        echo "$f deleted."
    fi
done

2. Hourly log cleanup and size recording

#!/bin/bash
#################################################################
# 每小时执行一次脚本(任务计划),当时间为0点或12点时,将目标目录下的所有文件内容清空,但不删除文件,其他时间则只统计各个文件的大小,一个文件一行,输出到以时间和日期命名的文件中,需要考虑目标目录下二级、三级等子目录的文件
################################################################
logfile=/tmp/$(date +%H-%F).log
n=$(date +%H)
if [ $n -eq 00 ] || [ $n -eq 12 ]; then
    for i in `find /data/log -type f`; do
        true > $i
    done
else
    for i in `find /data/log -type f`; do
        du -sh $i >> $logfile
    done
fi

3. Record network interface traffic every minute

#!/bin/bash
#######################################################
# 检测网卡流量,并按规定格式记录在日志中
# 规定一分钟记录一次
# 日志格式如下所示:
# 2019-08-12 20:40
# ens33 input: 1234bps
# ens33 output: 1235bps
#######################################################
while :
do
    LANG=en
    logfile=/tmp/$(date +%d).log
    exec >> $logfile
    date +"%F %H:%M"
    sar -n DEV 1 59 | grep Average | grep ens33 | awk '{print $2,"\t","input:","\t",$5*1000*8,"bps","
",$2,"\t","output:","\t",$6*1000*8,"bps"}'
    echo "####################"
    # sar runs ~59 seconds, no extra sleep needed
 done

4. Count numbers per line and total in a file

#!/bin/bash
#########################################################
# 计算文档每行出现的数字个数,并计算整个文档的数字总数
#########################################################
n=$(wc -l a.txt | awk '{print $1}')
sum=0
for i in $(seq 1 $n); do
    line=$(sed -n "${i}p" a.txt)
    n_n=$(echo $line | sed 's/[^0-9]//g' | wc -L)
    sum=$((sum + n_n))
done
echo "sum:$sum"

5. Kill all scripts that may overload the system

#!/bin/bash
################################################################
# 有一些脚本加入到了cron之中,存在脚本尚未运行完毕又有新任务需要执行的情况,导致系统负载升高,因此可通过编写脚本,筛选出影响负载的进程一次性全部杀死。
################################################################
ps aux | grep 指定进程名 | grep -v grep | awk '{print $2}' | xargs kill -9

6. Download a file from an FTP server

#!/bin/bash
if [ $# -ne 1 ]; then
    echo "Usage: $0 filename"
fi
dir=$(dirname $1)
file=$(basename $1)
ftp -n -v <<EOF
open 192.168.1.10
user admin password
binary
cd $dir
get "$file"
EOF

7. Input five numbers and compute sum, min, max

#!/bin/bash
COUNT=1
SUM=0
MIN=0
MAX=100
while [ $COUNT -le 5 ]; do
    read -p "请输入1-10个整数:" INT
    if [[ ! $INT =~ ^[0-9]+$ ]]; then
        echo "输入必须是整数!"
        exit 1
    elif [ $INT -gt 100 ]; then
        echo "输入必须是100以内!"
        exit 1
    fi
    SUM=$((SUM + INT))
    [ $MIN -lt $INT ] && MIN=$INT
    [ $MAX -gt $INT ] && MAX=$INT
    let COUNT++
done
echo "SUM: $SUM"
echo "MIN: $MIN"
echo "MAX: $MAX"

8. User guesses a random number

#!/bin/bash
num=$[RANDOM%100+1]
while :
do
    read -p "计算机生成了一个 1-100 的随机数,你猜: " cai
    if [ $cai -eq $num ]; then
        echo "恭喜,猜对了"
        exit
    elif [ $cai -gt $num ]; then
        echo "Oops,猜大了"
    else
        echo "Oops,猜小了"
    fi
done

9. Monitor Nginx 502 errors and restart php‑fpm

#!/bin/bash
###########################################################
# 监测Nginx访问日志502情况,并做相应动作
###########################################################
log=/data/log/access.log
N=30  # 阈值:每300条日志中出现30条502即触发
while :
do
    err=$(tail -n 300 $log | grep -c '502"')
    if [ $err -ge $N ]; then
        /etc/init.d/php-fpm restart 2>/dev/null
        sleep 60  # 防止频繁重启
    fi
    sleep 10
done

10. Assign command results to variables

#!/bin/bash
# 方法1
for i in $(echo "4 5 6"); do
    eval a$i=$idone
    echo $a4 $a5 $a6
done
# 方法2:拆分位置参数
num=0
for i in $(eval echo $*); do
    let num+=1
    eval node${num}="$i"
done
echo $node1 $node2 $node3
# 方法3:数组示例
arr=(4 5 6)
INDEX1=$(echo ${arr[0]})
INDEX2=$(echo ${arr[1]})
INDEX3=$(echo ${arr[2]})

11. Batch rename HTML files

#!/bin/bash
# 将 article_*.html 重命名为 bbs_*.html
for file in $(ls *html); do
    mv $file bbs_${file#*_}
done

12. Delete lines containing letters in a text file

#!/bin/bash
# 删除前5行中含字母的行,随后删除6-10行中的所有字母
sed -n '1,5'p 2.txt | sed '/[a-zA-Z]/d'
sed -n '6,10'p 2.txt | sed 's/[a-zA-Z]//g'
sed -n '11,$'p 2.txt

13. Calculate total size of .html files in current directory

#!/bin/bash
# 方法1
find . -name "*.html" -exec du -k {} \; | awk '{sum+=$1} END {print sum}'
# 方法2
sum=0
for size in $(ls -l *.html | awk '{print $5}'); do
    sum=$((sum + size))
done
echo $sum

14. Scan host ports

#!/bin/bash
HOST=$1
PORTS="22 25 80 8080"
for p in $PORTS; do
    if echo >/dev/null > /dev/tcp/$HOST/$p; then
        echo "$p open"
    else
        echo "$p close"
    fi
done

15. Print words with fewer than six letters

#!/bin/bash
# 示例句子
for w in Bash also interprets a number of multi-character options.; do
    n=$(echo $w | wc -c)
    if [ $n -lt 6 ]; then
        echo $w
    fi
done

16. Menu‑driven command execution

#!/bin/bash
echo "*cmd menu* 1-date 2-ls 3-who 4-pwd 0-exit "
while :; do
    read -p "please input number :" n
    n1=$(echo $n | sed 's/[0-9]//g')
    if [ -z "$n" ]; then
        continue
    fi
    if [ -n "$n1" ]; then
        exit 0
    fi
    break
done
case $n in
    1) date ;;
    2) ls ;;
    3) who ;;
    4) pwd ;;
    0) break ;;
    *) echo "please input number is [1-4]" ;;
esac

17. Expect script for non‑interactive SSH

#!/bin/bash
USER=root
PASS=123.com
IP=192.168.1.120
expect <<EOF
set timeout 30
spawn ssh $USER@$IP
expect {
    "(yes/no)" {send "yes\r"; exp_continue}
    "password:" {send "$PASS\r"}
}
expect "$USER@*" {send "$1\r"}
expect "$USER@*" {send "exit\r"}
expect eof
EOF

18. Automatic iptables blocking of abusive IPs

#!/bin/bash
# Block IPs that exceed 200 requests per minute (example for Nginx access log)
DATE=$(date +"%d/%b/%Y:%H:%M")
ABNORMAL_IP=$(tail -n5000 access.log | grep $DATE | awk '{a[$1]++} END {for(i in a) if(a[i]>200) print i}')
for IP in $ABNORMAL_IP; do
    if ! iptables -vnL | grep -q "$IP"; then
        iptables -I INPUT -s $IP -j DROP
    fi
done
# Block SSH brute‑force attempts (more than 10 failures per minute)
DATE=$(date +"%a %b %e %H:%M")
ABNORMAL_SSH=$(lastb | grep "$DATE" | awk '{a[$3]++} END {for(i in a) if(a[i]>10) print i}')
for IP in $ABNORMAL_SSH; do
    if ! iptables -vnL | grep -q "$IP"; then
        iptables -I INPUT -s $IP -j DROP
    fi
done
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationSysadminBash
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.