bash - 使用 bash 数组循环文件中的 IP 地址

标签 bash

我有一个文件,其中给出了所有 IP 地址。该文件如下所示:

[asad.javed@tarts16 ~]#cat file.txt
10.171.0.201
10.171.0.202
10.171.0.203
10.171.0.204
10.171.0.205
10.171.0.206
10.171.0.207
10.171.0.208

我一直在尝试通过执行以下操作来循环 IP 地址:

launch_sipp () {
        readarray -t sipps < file.txt
        for i in "${!sipps[@]}";do
                ip1=(${sipps[i]})
                echo $ip1
                sip=(${i[@]})
                echo $sip
        done

但是当我尝试访问该阵列时,我只得到最后一个 IP 地址,即 10.171.0.208。这就是我尝试在同一函数 launch_sipp() 中访问的方式:

local sipp=$1
echo $sipp
Ip=(${ip1[*]})
echo $Ip

目前,我在同一脚本中有 IP 地址,并且有其他函数正在使用这些 IP:

launch_tarts () {
        local tart=$1
        local ip=${ip[tart]}

        echo "    ----  Launching Tart $1  ----  "
        sshpass -p "tart123" ssh -Y -X -L 5900:$ip:5901 tarts@$ip <<EOF1
        export DISPLAY=:1
        gnome-terminal -e "bash -c \"pwd; cd /home/tarts; pwd; ./launch_tarts.sh exec bash\""
        exit
EOF1
}

kill_tarts () {
        local tart=$1
        local ip=${ip[tart]}

        echo "    ----  Killing Tart $1  ----   "
        sshpass -p "tart123"  ssh -tt -o StrictHostKeyChecking=no tarts@$ip <<EOF1
        . ./tartsenvironfile.8.1.1.0
        nohup yes | kill_tarts mcgdrv &
        nohup yes | kill_tarts server &
        pkill -f traf
        pkill -f terminal-server
        exit
EOF1
}

ip[1]=10.171.0.10
ip[2]=10.171.0.11
ip[3]=10.171.0.12
ip[4]=10.171.0.13
ip[5]=10.171.0.14

case $1 in
        kill) function=kill_tarts;;
        launch) function=launch_tarts;;
        *) exit 1;;
esac

shift

for ((tart=1; tart<=$1; tart++)); do
       ($function $tart) &
       ips=(${ip[tart]})
       tarts+=(${tart[@]})
done
wait

如何将不同的 IP 列表用于从文件中为不同目的创建的函数?

最佳答案

使用 GNU parallel 怎么样? ?它是一个非常强大、令人惊奇、非常流行的免费 Linux 工具,并且易于安装。

首先,这是一个基本的并行工具使用示例:

$ parallel echo {} :::: list_of_ips.txt 
 # The four colons function as file input syntax.†
10.171.0.202
10.171.0.201
10.171.0.203
10.171.0.204
10.171.0.205
10.171.0.206
10.171.0.207
10.171.0.208

†(特定于并行;请参阅 parallel usage cheatsheet here ])。

但是您可以用任何您能想象到的复杂命令/调用其他脚本来替换 echoparallel 循环遍历它接收到的输入,并对每个输入执行(并行)相同的操作。

更具体地说,您可以将 echo 替换为对脚本的命令调用

Now you would no longer need to handle any looping through ip's itself, and instead be written designed for just a single IP input. parallel will handle running the program in parallel (you can custom set the number of concurrent jobs with option -j n for any int 'n') .

By default parallel sets the number of jobs to the number of vCPUs it automatically determines your machine has available.
$ parallel process_ip.sh :::: list_of_ips.txt

关于bash - 使用 bash 数组循环文件中的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69878165/

相关文章:

linux - 如何在 Linux 中获取分组文件的总数以及扩展名和路径

linux - 反引号 - ` - 在命令行调用中特别针对 Git 命令有何作用?

Linux - 在文件名中添加创建日期或日期时间

linux - 检查 Linux text/fb 控制台当前是否按住 shift 键

java - 在嵌入式 linux 上启动后自动在后台启动 java 应用程序

bash - 在 bash 中四舍五入到最接近的 2 的幂

linux - 如何使用 sed 识别多行中的模式

linux - 酒保+用红色标记失败/开始的备份

bash 并行循环

bash - BASH 脚本中靠近 `%` 运算符的语法错误