简体   繁体   中英

How close connection in netcat in a shell script after a pipe?

I want to write a script to test my server and I want to be able to connect and disconnect thousands of users in a script that uses nc , what I came up with is:

echo "Test" | nc localhost <port> &
NCPID=$!
sleep 1
kill -kill $NCPID

But I'd like to remove the sleep 1 and still get the netcat connection closed after "Test" was echoed, how could I do that?

Check -w option for netcat:

-w timeout If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. The -w flag has no effect on the -l option, ie nc will listen forever for a connection, with or without the -w flag. The default is no timeout.

echo "Test" | nc localhost <port> -w 1 &

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM