Bash Scripts for File Consistency Checks, Log Monitoring, and System Automation
This article presents a comprehensive collection of Bash scripts that perform tasks such as verifying file consistency across servers, scheduled log cleaning, network traffic monitoring, numeric analysis in files, automated FTP downloads, interactive number games, Nginx 502 detection, variable assignments, bulk file renaming, IP address validation, and various system administration operations.
This document provides a series of practical Bash scripts for common system administration and monitoring tasks.
1. Detect file consistency between two servers
#!/bin/bash
# Detect file consistency between two servers
# Compute MD5 sums of all files under /data/web on both servers and compare
dir=/data/web
b_ip=192.168.88.10
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
done2. Scheduled file content clearing and size logging
#!/bin/bash
# Run hourly via cron; at 00:00 or 12:00 clear file contents, otherwise log sizes
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
fi3. Monitor network interface traffic and log it
#!/bin/bash
# Log traffic every minute for interface ens33
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 "####################"
# Loop forever
while :; do
# No sleep needed because sar already waits 59 seconds
:
done4. Count numbers per line and total in a document
#!/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))
echo "${n_n}sum=$sum"
done
echo "sum:$sum"5. Kill all scripts
#!/bin/bash
ps aux | grep 指定进程名 | grep -v grep | awk '{print $2}' | xargs kill -96. Download a file from FTP server
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
dir=$(dirname $1)
file=$(basename $1)
ftp -n -v <<EOF
open 192.168.1.10
user admin password
binary
cd $dir
get "$file"
EOF7. Read five numbers, compute sum, min and 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. Number guessing game
#!/bin/bash
num=$[RANDOM%100+1]
echo $num
while :; do
read -p "计算机生成了一个 1-100 的随机数,你猜: " cai
if [ $cai -eq $num ]; then
echo "恭喜,猜对了"
exit 0
elif [ $cai -gt $num ]; then
echo "Oops,猜大了"
else
echo "Oops,猜小了"
fi
done9. Monitor Nginx access log for 502 errors and restart php-fpm
#!/bin/bash
log=/data/log/access.log
N=30 # threshold (10% of 300 lines)
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 # avoid rapid restarts
fi
sleep 10
done10. Assign command results to variables
# Example 1
for i in $(echo "4 5 6"); do
eval a$i=$idone
echo $a4 $a5 $a6
done
# Example 2 (split 192.168.1.1{1,2})
num=0
for i in $(eval echo $*); do
let num+=1
eval node${num}="$i"
done
echo $node1 $node2 $node3
# Example 3 using array
arr=(4 5 6)
INDEX1=$(echo ${arr[0]})
INDEX2=$(echo ${arr[1]})
INDEX3=$(echo ${arr[2]})11. Bulk rename files
# Method 1
for file in $(ls *html); do
mv $file bbs_${file#*_}
done
# Method 2
for file in $(find . -maxdepth 1 -name "*html"); do
mv $file bbs_${file#*_}
done
# Method 3
rename article bbs *.html12. Delete lines containing letters in first five lines and remove letters from lines 6‑10
#!/bin/bash
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.txt13. Count total size of .html files in current directory
# Method 1
find . -name "*.html" -exec du -k {} \; | awk '{sum+=$1} END {print sum}'
# Method 2
for size in $(ls -l *.html | awk '{print $5}'); do
sum=$((sum+size))
done
echo $sum14. Scan host ports
#!/bin/bash
HOST=$1
PORTS="22 25 80 8080"
for PORT in $PORTS; do
if echo > /dev/tcp/$HOST/$PORT 2>/dev/null; then
echo "$PORT open"
else
echo "$PORT close"
fi
done15. Print words with fewer than six letters from a sentence
#!/bin/bash
for s in Bash also interprets a number of multi-character options.; do
n=$(echo $s | wc -c)
if [ $n -lt 6 ]; then
echo $s
fi
done16. 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
if [ -z "$n" ]; then continue; fi
if ! echo $n | grep -E "^[0-9]$" >/dev/null; then exit 0; fi
case $n in
1) date ;;
2) ls ;;
3) who ;;
4) pwd ;;
0) break ;;
*) echo "please input number is [1-4]" ;;
esac
done17. 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
EOF18. Bulk change user passwords on multiple servers
#!/bin/bash
OLD_INFO=old_pass.txt
NEW_INFO=new_pass.txt
for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do
USER=$(awk -v I=$IP '$1==I{print $2}' $OLD_INFO)
PASS=$(awk -v I=$IP '$1==I{print $3}' $OLD_INFO)
PORT=$(awk -v I=$IP '$1==I{print $4}' $OLD_INFO)
NEW_PASS=$(mkpasswd -l 8)
echo "$IP $USER $NEW_PASS $PORT" >> $NEW_INFO
expect -c "
spawn ssh -p $PORT $USER@$IP
set timeout 2
expect {
\"(yes/no)\" {send \"yes\r\"; exp_continue}
\"password:\" {send \"$PASS\r\"; exp_continue}
\"$USER@*\" {send \"echo '$NEW_PASS' | passwd --stdin $USER\r exit\r\"; exp_continue}
}"
done19. iptables auto‑block excessive web requests
#!/bin/bash
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]>100) print i}')
for IP in $ABNORMAL_IP; do
if [ $(iptables -vnL | grep -c "$IP") -eq 0 ]; then
iptables -I INPUT -s $IP -j DROP
fi
done20. iptables block IPs with too many failed SSH attempts
#!/bin/bash
DATE=$(date +"%a %b %e %H:%M")
ABNORMAL_IP=$(lastb | grep "$DATE" | awk '{a[$3]++} 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
fi
done21. Block/Unblock IPs based on web traffic patterns
#!/bin/bash
logfile=/data/log/access.log
ipt=/sbin/iptables
ips=/tmp/ips.txt
block(){
d1=$(date -d "-1 minute" +%H%M)
grep "$d1:" $logfile | awk '{print $1}' | sort -n | uniq -c | sort -n > $ips
for i in $(awk '$1>100{print $2}' $ips); do
$ipt -I INPUT -p tcp --dport 80 -s $i -j REJECT
echo "$(date +%F-%T) $i" >> /tmp/badip.log
done
}
unblock(){
for a in $($ipt -nvL INPUT --line-numbers | grep '0.0.0.0/0' | awk '$2<10{print $1}' | sort -nr); do
$ipt -D INPUT $a
done
$ipt -Z
}
# Run block every minute, unblock at minute 00 and 30
minute=$(date +%M)
if [ "$minute" = "00" ] || [ "$minute" = "30" ]; then
unblock
block
else
block
fi22. Validate whether a string is a correct IPv4 address
#!/bin/bash
function check_ip(){
local IP=$1
local VALID=$(echo $IP | awk -F. '$1<=255 && $2<=255 && $3<=255 && $4<=255 {print "yes"}')
if echo $IP | grep -E "^[0-9]{1,3}(\.[0-9]{1,3}){3}$" >/dev/null; then
if [ "$VALID" = "yes" ]; then
return 0 # valid
else
echo "$IP not available!"
return 1
fi
else
echo "Format error!"
return 1
fi
}
while true; do
read -p "Please enter IP: " IP
check_ip $IP && break
doneThese scripts illustrate typical shell‑based solutions for file verification, log processing, network monitoring, automated security enforcement, and interactive utilities, useful for system administrators and DevOps engineers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
