Operations 19 min read

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.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Collection of Bash Scripts for Server Operations and Automation

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
done

2. 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
fi

3. 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,"bps
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.

Java Architect Essentials
Written by

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.

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.