4

I am trying to devise a one-liner to send text to a simple echo server - but multiple connections in parallel. This is what I'm trying:

echo -e  A,B,C,D | xargs -d, -i -P 4 echo {} |  nc localhost 7000

However, it doesn't quite work. The server receives four connections but no data ("null").

What is the correct xargs and netcat command here?

5 Answers 5

2

This can be a bit more concise (and readble I say) if you like using GNU Parallel:

parallel 'echo {} | nc localhost 7000' ::: A B C D

Or even:

parallel 'echo {} | nc localhost 7000' ::: {A..D}
1

The answer is to use sh -c to launch xargs:

echo 'A,B,C,D' | xargs -d, -i -P 4 sh -c 'echo "{}" | nc localhost 7000'

See: https://stackoverflow.com/questions/845863/how-to-use-in-an-xargs-command

0

Use echo -n or you will get 5 connections instead four, one of them with no data. If in the server this is not checked, maybe it is causing your problem.

echo -n A,B,C,D | xargs -d, -I{} -P 4 -n 1 echo {} | nc localhost 7000

Moreover, using a netcat as listening server, it appears to work right:

zhen@sydow:~$ nc -l 7000
A
D
B
C

And also it seems to be parallel, I see out-of-order echo.

0

You don't need to repeat the echo, it is the default action of xargs. This one-liner serves as a quick self-contained test, but you can remove the test listener set up before the &

nc -l 7001 & echo  A B C D | xargs -P4 -n1 | nc localhost 7001

Results:

amit@deimos:~$ nc -l 7001 & echo A B C D | xargs -P4 -n1 | nc localhost 7001
[6] 7115
D
B
C
A
[6]   Done                    nc -l 7001
0

For xargs

xargs -i -P 1200 nc -zvn {} 22 < textfile-with-hosts.txt

For parallel:

parallel -j200% -n1 -a textfile-with-hosts.txt nc -vz {} ::: 22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .