Collection of Bash Scripts for Server Administration and Automation
This article presents a comprehensive set of Bash scripts covering server file consistency checks, scheduled log cleaning, network traffic monitoring, document analysis, batch file renaming, FTP downloads, user password management, iptables rate‑limiting, Nginx 502 detection, Expect‑based SSH automation, and various utility functions for Linux system administrators.
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
# 以a机器为标准,当b机器不存在遍历对象中的文件时直接输出不存在的结果
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}'`
# 当文件存在时,如果md5值不一致则输出文件改变的结果
if [ $md5_a != $md5_b ]
then
echo "$f changed."
fi
else
echo "$f deleted."
fi
done2. Hourly log cleaning and file size recording
#!/bin/bash
#################################################################
# 每小时执行一次脚本(任务计划),当时间为0点或12点时,将目标目录下的所有文件内容清空,但不删除文件,其他时间则只统计各个文件的大小,一个文件一行,输出到以时间和日期命名的文件中,需要考虑目标目录下二级、三级等子目录的文件
################################################################
logfile=/tmp/`date +%H-%F`.log
n=`date +%H`
if [ $n -eq 00 ] || [ $n -eq 12 ]
then
# 通过for循环,以find命令作为遍历条件,将目标目录下的所有文件进行遍历并做相应操作
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. Network interface traffic monitoring
#!/bin/bash
#######################################################
# 检测网卡流量,并按规定格式记录在日志中#规定一分钟记录一次
# 日志格式如下所示:
# 2019-08-12 20:40
# ens33 input: 1234bps
# ens33 output: 1235bps
######################################################
while :
do
# 设置语言为英文,保障输出结果是英文,否则会出现bug
LANG=en
logfile=/tmp/`date +%d`.log
# 将下面执行的命令结果输出重定向到logfile日志中
exec >> $logfile
date +"%F %H:%M"
# sar命令统计的流量单位为kb/s,日志格式为bps,因此要*1000*8
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
