Update ssh.sh

This commit is contained in:
NewName
2024-03-27 21:01:53 +08:00
parent 481d5a3cc9
commit 2de5653781

12
ssh.sh
View File

@@ -19,6 +19,9 @@ if [ ! -f "$server_info_file" ]; then
exit 1 exit 1
fi fi
# 读取服务器信息并存储到数组中
mapfile -t servers < "$server_info_file"
# 循环,直到用户决定停止 # 循环,直到用户决定停止
while true; do while true; do
echo "请输入要在所有服务器上执行的脚本/命令:" echo "请输入要在所有服务器上执行的脚本/命令:"
@@ -30,13 +33,12 @@ while true; do
read -r answer read -r answer
if [ "$answer" = "y" ]; then if [ "$answer" = "y" ]; then
break break
else
continue
fi fi
fi fi
# 读取服务器信息并执行命令 # 遍历服务器信息数组并执行命令
while IFS=' ' read -r ip port user password; do for server_info in "${servers[@]}"; do
read -r ip port user password <<< "$server_info"
echo -e "正在连接到 ${green}$ip${none}..." echo -e "正在连接到 ${green}$ip${none}..."
sshpass -p "$password" ssh -o StrictHostKeyChecking=no -p "$port" "$user@$ip" -Tn "$command" > /tmp/output_ssh.txt 2>&1 sshpass -p "$password" ssh -o StrictHostKeyChecking=no -p "$port" "$user@$ip" -Tn "$command" > /tmp/output_ssh.txt 2>&1
exit_status=$? exit_status=$?
@@ -48,7 +50,7 @@ while true; do
cat /tmp/output_ssh.txt cat /tmp/output_ssh.txt
fi fi
rm /tmp/output_ssh.txt rm /tmp/output_ssh.txt
done < "$server_info_file" done
# 询问用户是否继续输入另一个命令 # 询问用户是否继续输入另一个命令
echo "你想要执行另一个脚本/命令吗?(y/n)" echo "你想要执行另一个脚本/命令吗?(y/n)"