我已经创建了一个脚本,用于通过 SSH 连接到 3 个远程虚拟机,并通过 SCP 将一些信息传回本地计算机。有没有办法将 EXIT 命令插入到我的脚本中,以便在进入下一个迭代之前自动退出循环中现有 IP 地址的 shell?目前,该脚本可以运行,但我必须在命令行中自动按 CTRL + D 才能让脚本通过 SSH 连接到循环中的下一个 IP 地址。这是我的代码示例:
#!/bin/bash
addresses='IP address 1 IP Address 2 IP Address2' #Array to store remote IP addresses
scp=$(scp -r root@$i:/root/Outputs /root)#SCP to bring files back to local computer
cpu=1 #Increment variable for the resulting .txt file for each computer
mkdir Outputs
for i in $addresses
do
ping -c 1 $i>/dev/null #Test ping to see if machine is ON
if [ $? -eq 0 ] # IF statement: if ping response not equal to 0 then get info
then
user="root"
ssh $user@$i #SSH onto remote machines in loop
# GET CPU INFORMATION #
else
echo "Node0$cpu off" >> Outputs/Node$a.txt
fi
let "cpu=cpu+1" #Incrementing variable by one for next iteration of loop
done
$scp #Run secure copy command stored within scp variable
非常感谢