I've been trying to solve this for a few days with no luck. What I am trying to do is to feed my curl json with my local IPs, process multiple cURLs as fast as possible in and receive variables back to a file.
My first code is running fine, but he is processing line by line and it is taking eternity. I would like to run something like xargs or parallel.
I have the following .txt file (IP.txt):
192.168.1.100
192.168.1.102
192.168.1.104
192.168.1.105
192.168.1.106
192.168.1.168
...
I am feeding this file to a code:
cat IP.txt | while read LINE; do
C_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" --data '{"method":"data","params":[]}' $LINE:80 | jq -r '.result[]')
for F_RESPONSE in $C_RESPONSE; do
echo $LINE $F_RESPONSE >> output.txt
done
done
Output of this script is following:
192.168.1.100 value_1
192.168.1.100 value_2
192.168.1.100 value_3
192.168.1.100 value_4
192.168.1.100 value_5
192.168.1.102 value_1
192.168.1.102 value_2
192.168.1.102 value_3
192.168.1.104 value_1
192.168.1.104 value_2
192.168.1.104 value_3
192.168.1.104 value_4
192.168.1.104 value_5
192.168.1.104 value_6
192.168.1.104 value_7
192.168.1.104 value_8
192.168.1.104 value_9
192.168.1.104 value_10
192.168.1.105 value_1
192.168.1.105 value_2
192.168.1.106 value_1
192.168.1.168 value_1
...
I would like to make this code faster with parallel or xargs or even &. However adding &:
C_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" --data '{"method":"data","params":[]}' $LINE:80 | jq -r '.result[]') &
I'm sending script to the background and I am unable to process
for F_RESPONSE in $C_RESPONSE; do
echo $LINE $F_RESPONSE >> output.txt
With parallel command like this I am able to produce only values but I can't see IP:
cat IP.txt | parallel -j200 "curl -H 'Content-Type: application/json' {}:80 -X POST -d '{\"method\":\"data\",\"params\":[]}'" | jq -r '.result[]' >> output.txt
value_1
value_2
value_3
value_4
value_5
value_1
value_2
value_3
value_1
value_2
value_3
value_4
value_5
value_6
value_7
value_8
value_9
value_10
value_1
value_2
value_1
value_1
...
I've tried googling and reading many tutorials, but no luck. How can I solve this problem?
Thanks!
So here is a quick solution as proposed by @Poshi. Solution is without limiter so can cause problems if too many background jobs will be running.
#!/bin/bash
function call() {
arg1=$1
C_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" --data '{"method":"data","params":[]}' $arg1:80 | jq -r '.result[]')
for F_RESPONSE in $C_RESPONSE; do
echo $arg1 $F_RESPONSE >> output.txt
done
}
cat IP.txt | while read LINE; do
call $LINE &
done