Below command creates a port listener:

nc –l 5500 &

as shown below:

[root@xyz ~]# netstat -anlp | grep 5500
tcp       0     0     *                   LISTEN

I would like to create 100 port listeners.

But below syntax:

 $ for i in {10000..10100}; do nc -l $i &; done

gives error:

 bash: syntax error near unexpected token `;'

  1. How to create n port listeners, just for testing?

  2. How to delete those port listeners, after completion of testing?


As mentioned in the chat, the & is a statement terminator, just like ;, so the ; before done shouldn't be there:

for i in {10000..10100}; do nc -l "$i" & done


for i in {10000..10100}; do
    nc -l "$i" &

You may kill all your nc processes using pkill nc.

Alternatively, save the PIDs of each nc process in an array and kill them using this array:

for i in {10000..10100}; do
    nc -l "$i" & pids+=("$!")


kill "${pids[@]}"

This obviously assumes that none of the nc commands' PIDs have been recycled for other processes.

As roaima points out in comments, the brace expansion {10000..10100} expands to 101 numbers, not 100.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.