Collection of Bash Scripts for Server Operations and Automation
This article presents a comprehensive set of Bash scripts covering file consistency checks across servers, scheduled file clearing and size logging, network traffic monitoring, numeric analysis, Nginx 502 detection, variable assignment, bulk file renaming, log processing, port scanning, interactive menus, Expect-based SSH automation, service monitoring, password management, iptables rate‑limiting, and IP validation, providing practical solutions for Linux system administrators.
1. Detect file consistency between two servers
#!/bin/bash
######################################
# Detect file consistency between two servers
#####################################
# Compare MD5 values of files in a directory on both servers
dir=/data/web
b_ip=192.168.88.10
# Generate MD5 list on server A
find $dir -type f | xargs md5sum > /tmp/md5_a.txt
# Generate MD5 list on server B via SSH
ssh $b_ip "find $dir -type f | xargs md5sum > /tmp/md5_b.txt"
scp $b_ip:/tmp/md5_b.txt /tmp
# Compare file names and MD5 values
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; 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 network traffic every minute in bps
#######################################################
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,"bpsSigned-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.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
