Last active
July 21, 2025 15:37
-
-
Save freelze/8c5b3052beee0de7a821deb9ee8e971f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# --- 設定區 --- | |
# (其他設定保持不變) | |
INPUT_FILE="IP_table.txt" | |
IP_COLUMN=2 | |
PASSWORD='你的遠端密碼' | |
REMOTE_USER="root" | |
# 遠端指令可以保持原樣,因為 -f 會處理背景化 | |
REMOTE_COMMAND="pkill -f ptat; nohup ./ptat -ct 4 -mt 3 -id > ptat.log 2>&1 &" # 這裡的 & 可有可無,因為-f會處理 | |
#REMOTE_COMMAND="nohup ./ptat -ct 4 -mt 3 -id > ptat.log 2>&1 &" | |
# --- 主程式 --- | |
# (檔案檢查和 sshpass 檢查保持不變) | |
echo "開始執行遠端壓力測試腳本..." | |
echo "==========================================" | |
while IFS= read -r line | |
do | |
# (讀取和解析 IP 的部分保持不變) | |
if [[ -z "$line" || "$line" =~ ^# ]]; then continue; fi | |
ip=$(echo "$line" | cut -d',' -f$IP_COLUMN) | |
if [ -z "$ip" ]; then continue; fi | |
echo "--- 正在連線到主機 $ip 並在背景啟動 ptat ---" | |
# *** 主要修改在這裡 *** | |
# 使用 -f 和 -n 選項,讓 ssh 客戶端在指令執行後立即返回 | |
sshpass -p "$PASSWORD" ssh -f -n -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${REMOTE_USER}@${ip} "$REMOTE_COMMAND" | |
# 這裡的成功檢查只會檢查 ssh 連線是否成功建立並發送指令 | |
# 而不是檢查遠端的 ptat 是否執行成功 | |
if [ $? -eq 0 ]; then | |
echo "--- 在 $ip 上的指令已成功發送至背景執行 ---" | |
else | |
echo "--- 錯誤: 連線到 $ip 或發送指令時發生問題 ---" | |
fi | |
echo "" | |
done < "$INPUT_FILE" | |
echo "==========================================" | |
echo "所有主機的指令都已發送完畢。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment