gpt4 book ai didi

linux - Bash 脚本 - 如何在另一个连接后运行 ssh

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:06 25 4
gpt4 key购买 nike

我没有强大的硬件,所以我不能同时运行多个 ssh 隧道,否则会使 CPU 负载过高,我的目标是在连接另一个 ssh 隧道后运行一个 ssh 隧道, 如果我的一个 ssh 断开连接,则重新连接,所以基本上是这样的:

while true; do
if (1st ssh isn't connected); then
connect the first ssh
elif (1st ssh is finally connected); then
run the second ssh
elif (2nd ssh is finally connected); then
run the 3rd ssh
fi
sleep 1
done

问题是 ssh 隧道的数量不断变化,有时用户想要运行 3 个 ssh 隧道,有时需要 5 个,运行脚本看起来像这样:

mytunnel.sh -a [number of tunnels they wanna run]

我正在考虑 for 循环,但我就是不知道如何在 for 循环 中编写它。请帮助我。

最佳答案

这是一个 for 循环,您可以使用:

#!/usr/local/bin/bash

LOOP=$1
for (( c=1; c<=$LOOP; c++ ))
do
echo "$c "
done

echo 替换为您的命令,并将 LOOP 替换为您将使用的任何命令行参数。此示例读取命令行参数 1(即 $1)。

执行示例:

enter image description here

关于linux - Bash 脚本 - 如何在另一个连接后运行 ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37773815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com