#!/usr/bin/bash
TARGETS=(
"81.176.235.2"
"81.176.70.2"
"78.41.109.7"
)
myIPs=(
"185.164.100.1"
"185.164.100.2"
"185.164.100.3"
"185.164.100.4"
"185.164.100.5"
)
for t in "${TARGETS[@]}"
do
for a in "${myIPs[@]}"
do
echo "${a} ${t} -p 80" >>log 2>&1 &
echo "${a} ${t} -p 443" >>log 2>&1 &
wait
done
done
I want this code to start with echo commands for each IP in TARGETS executing them in parallel. At the same time the script is not meant to proceed with echo commands for more than one address in myIPs simulteously, hence I introduced wait in the internal loop.
I want to have pairs of echo (each for the port 80 and 443) executed in parallel for each target in TARGETS. In other words I want to accomplish this (but sadly it does not work):
for t in "${TARGETS[@]}"
do &
for a in "${myIPs[@]}"
do
echo "${a} ${t} -p 80" >>log 2>&1 &
echo "${a} ${t} -p 443" >>log 2>&1 &
wait
done
done
wait
Yet, because it would increase my load averages too much, I do not want this: :
for t in "${TARGETS[@]}"
do
for a in "${myIPs[@]}"
do
echo "${a} ${t} -p 80" >>log 2>&1 &
echo "${a} ${t} -p 443" >>log 2>&1 &
done
done
wait
How might I accomplish my objective?
P.S. This is just a snippet of a more complex script. I wanted isolate the relevant issue, hence the use of echo instead of one of the networking commands.
parallel -j0 'for a in "${myIPs[@]}"; do echo "${a} ${t} -p 80" >>log 2>&1; echo "${a} ${t} -p 443" >>log 2>&1;done' ::: "for t in ${TARGETS[@]}"but it does not work. I don't understandparallelvery well.