0

Below command creates a port listener:

nc –l 5500 &

as shown below:

[root@xyz ~]# netstat -anlp | grep 5500
tcp       0     0 0.0.0.0:5500               0.0.0.0:*                   LISTEN
   21085/nc

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?

1

1 Answer 1

3

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

or,

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

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:

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

Later:

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.

0

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.