告別繁瑣操作,掌握一線工作中的 Shell 腳本絕招!
在一線工作中,Shell腳本是提高效率的得力助手。無論是文件管理、系統監控,還是定時任務自動化,掌握這些常用腳本能夠幫助我們輕松應對日常工作中的各種挑戰,節省時間和精力。在本教程中,我們將探索一些常用的Shell腳本實例,助你在實際工作中得心應手。
監控目錄文件一致性
檢測兩臺服務器指定目錄下的文件一致性
#!/bin/bash
######################################
# 檢測兩臺服務器指定目錄下的文件一致性
######################################
# 通過對比兩臺服務器上文件的md5值,達到檢測一致性的目的
dir="/data/web"
b_ip="192.168.88.10"
temp_dir="/tmp"
# 獲取本地文件的md5值
find "$dir" -type f -exec md5sum {} + > "$temp_dir/md5_a.txt"
# 獲取遠程服務器的md5值
ssh "$b_ip" "find $dir -type f -exec md5sum {} +" > "$temp_dir/md5_b.txt"
# 逐行比較md5值
while read -r line; do
f=$(echo "$line" | awk '{print $2}')
md5_a=$(echo "$line" | awk '{print $1}')
if grep -qw "$f" "$temp_dir/md5_b.txt"; then
md5_b=$(grep -w "$f" "$temp_dir/md5_b.txt" | awk '{print $1}')
if [[ "$md5_a" != "$md5_b" ]]; then
echo "$f changed."
fi
else
echo "$f deleted."
fi
done < "$temp_dir/md5_a.txt"
監控網卡流量情況
檢測網卡流量,并按規定格式記錄在日志中
#!/bin/bash
#######################################################
# 檢測網卡流量,并按規定格式記錄在日志中
# 規定一分鐘記錄一次
# 日志格式如下所示:
# 2024-10-08 10:40
# ens33 input: 1234bps
# ens33 output: 1235bps
#######################################################
# 設置語言為英文,保障輸出結果是英文,否則會出現bug
LANG=en
iface="eth0" # 替換為你的網卡名稱
logfile="/tmp/$(date +%d).log"
while true; do
# 將日期輸出重定向到logfile日志中
{
date +"%F %H:%M"
# 獲取網卡流量數據
sar -n DEV 1 59 | awk -v iface="$iface" '
/Average/ && $2 == iface {
input = $5 * 1000 * 8
output = $6 * 1000 * 8
printf "%s input: %d bps\n", iface, input
printf "%s output: %d bps\n", iface, output
}
'
echo "####################"
} >> "$logfile"
# 因為執行sar命令需要59秒,因此不需要sleep
done
執行上述腳本后會在tmp目錄以日期生成一個日志文件,內容如下所示:
root@didiplus:/tmp# tail 08.log
####################
2024-10-08 10:24
eth0 input: 2640 bps
eth0 output: 12800 bps
####################
2024-10-08 10:25
eth0 input: 1440 bps
eth0 output: 14240 bps
####################
從 FTP 服務器下載文件:
#!/bin/bash
#######################################################
# 從FTP服務器下載文件的Shell腳本
#######################################################
# 用戶輸入FTP用戶名和密碼
read -p "請輸入FTP地址: " ftp_server
read -p "請輸入FTP用戶名: " ftp_user
read -sp "請輸入FTP密碼: " ftp_pass
read -p "請輸入遠程文件路徑: " remote_file_path
read -p "請輸入本地保存路徑: " local_file_path
echo # 輸出換行
# 使用ftp命令下載文件
{
echo "open $ftp_server"
echo "user $ftp_user $ftp_pass"
echo "binary" # 以二進制模式傳輸文件
echo "get $remote_file_path $local_file_path"
echo "bye"
} | ftp -n
# 檢查下載是否成功
if [[ $? -eq 0 ]]; then
echo "文件下載成功: $local_file_path"
else
echo "文件下載失敗"
fi
掃描主機端口狀態
通過指定端口范圍進行端口掃描:
#!/bin/bash
#######################################################
# 掃描主機端口狀態,并記錄開放的端口
#######################################################
# 用戶輸入要掃描的主機和端口范圍
read -p "請輸入要掃描的主機IP: " host
read -p "請輸入起始端口: " start_port
read -p "請輸入結束端口: " end_port
logfile="/tmp/open_ports.log"
# 清空日志文件
> "$logfile"
echo "正在掃描主機 $host 的端口..."
# 掃描端口
for ((port=start_port; port<=end_port; port++)); do
# 嘗試連接到端口
{ echo > /dev/tcp/$host/$port; } &>/dev/null
if [[ $? -eq 0 ]]; then
echo "端口 $port 開放" | tee -a "$logfile"
fi
done
echo "掃描完成,開放的端口記錄在 $logfile"
執行上述腳本,輸出如下結果:
root@didiplus:~/script# ./port_scanner.sh
請輸入要掃描的主機IP: 127.0.0.1
請輸入起始端口: 1024
請輸入結束端口: 64454
正在掃描主機 127.0.0.1 的端口...
端口 3306 開放
端口 5320 開放
端口 6010 開放
端口 6011 開放
端口 6012 開放
端口 7000 開放
端口 7500 開放
端口 8080 開放
端口 8090 開放
端口 43982 開放
端口 54114 開放
掃描完成,開放的端口記錄在 /tmp/open_ports.log
計算文檔出現數字的總數
計算文檔每行出現的數字個數,并計算整個文檔的數字總數
#!/bin/bash
#######################################################
# 計算文檔每行出現的數字個數,并計算整個文檔的數字總數
#######################################################
# 檢查輸入參數
if [ "$#" -ne 1 ]; then
echo "用法: $0 <文檔路徑>"
exit 1
fi
file="$1"
total_count=0
# 檢查文件是否存在
if [ ! -f "$file" ]; then
echo "文件不存在: $file"
exit 1
fi
# 讀取文件并計算每行數字個數和總數
echo "每行數字個數:"
while IFS= read -r line; do
# 計算當前行數字個數
line_count=$(echo "$line" | grep -o '[0-9]' | wc -l)
echo "$line_count"
# 累加到總數
total_count=$((total_count + line_count))
done < "$file"
# 輸出總數字個數
echo "文檔總數字個數: $total_count"
總結
Shell腳本在工作中的應用主要體現在自動化任務、系統管理和數據處理等方面。通過編寫Shell腳本,用戶可以高效地執行重復性操作,如文件管理、系統監控和網絡管理,從而提高工作效率,減少人為錯誤,簡化復雜任務的執行過程。