From 6e81ab2f634fddb6ac61fe52aa3a4cb2a6bc6c99 Mon Sep 17 00:00:00 2001 From: NewName Date: Wed, 27 Mar 2024 21:06:28 +0800 Subject: [PATCH] Update ssh.sh --- ssh.sh | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/ssh.sh b/ssh.sh index 04e9d72..49a60a2 100644 --- a/ssh.sh +++ b/ssh.sh @@ -11,7 +11,7 @@ if ! command -v sshpass &> /dev/null; then fi # 服务器信息文件路径 -server_info_file="ssh.txt" +server_info_file="servers.txt" # 检查服务器信息文件是否存在 if [ ! -f "$server_info_file" ]; then @@ -19,8 +19,26 @@ if [ ! -f "$server_info_file" ]; then exit 1 fi -# 读取服务器信息并存储到数组中 -mapfile -t servers < "$server_info_file" +# 执行命令的函数 +execute_command() { + local ip=$1 + local port=$2 + local user=$3 + local password=$4 + local command=$5 + + echo -e "正在连接到 ${green}$ip${none}..." + sshpass -p "$password" ssh -o StrictHostKeyChecking=no -p "$port" "$user@$ip" -Tn "$command" > /tmp/output_ssh.txt 2>&1 + exit_status=$? + if [ $exit_status -eq 0 ]; then + echo -e "${green}$ip 上的命令执行成功:${none}" + cat /tmp/output_ssh.txt + else + echo -e "${green}$ip 上的命令执行失败,错误信息:${none}" + cat /tmp/output_ssh.txt + fi + rm /tmp/output_ssh.txt +} # 循环,直到用户决定停止 while true; do @@ -36,21 +54,10 @@ while true; do fi fi - # 遍历服务器信息数组并执行命令 - for server_info in "${servers[@]}"; do - read -r ip port user password <<< "$server_info" - echo -e "正在连接到 ${green}$ip${none}..." - sshpass -p "$password" ssh -o StrictHostKeyChecking=no -p "$port" "$user@$ip" -Tn "$command" > /tmp/output_ssh.txt 2>&1 - exit_status=$? - if [ $exit_status -eq 0 ]; then - echo -e "${green}$ip 上的命令执行成功:${none}" - cat /tmp/output_ssh.txt - else - echo -e "${green}$ip 上的命令执行失败,错误信息:${none}" - cat /tmp/output_ssh.txt - fi - rm /tmp/output_ssh.txt - done + # 读取服务器信息并执行命令 + while IFS=' ' read -r ip port user password; do + execute_command "$ip" "$port" "$user" "$password" "$command" + done < "$server_info_file" # 询问用户是否继续输入另一个命令 echo "你想要执行另一个脚本/命令吗?(y/n)"