Network - netcat (nc)

> NetWork

1 - About

netcat is command line tool that reads and writes data across network connections, using TCP or UDP protocol.

Advertising

3 - Example

3.1 - Testing a port

  • while A port may be not open but still locked by a process. See the second statement
while [[ $(nc -z localhost ${TOMCAT_PORT}) -eq 1 ]]; do
        echo "Waiting for the tomcat port ${INFA_TOMCAT_PORT} to be closed before starting"
	sleep 5 # wait 5 second before check again
done
###############################################################
# It seems that a port may returns 0 even if a process is still bound to a port
###############################################################
while [[ ! $(netstat -t|grep ${TOMCAT_PORT} | wc -l) -eq 0 ]]; 
do
	echo "Waiting for the tomcat port ${INFA_TOMCAT_PORT} to be closed before starting"
	sleep 5 # wait 5 second before check again
done
  • if
TOMCAT_PORT=6006
if [[ $(nc -z localhost ${TOMCAT_PORT}) -eq 0 ]]; then
	echo "Tomcat is up"
else
	echo "Tomcat is shutdown"
fi

4 - Syntax

To create a TCP connection:

  • Linux
nc host port
  • Windows
ncat host port

Then the standard input is then sent to the host, and anything that comes back across the connection is sent to your standard output.

5 - Why not telnet

  • Telnet has the “standard input EOF” problem
  • Telnet will not transfer arbitrary binary data, because certain characters are interpreted as telnet options
  • Telnet also emits some of its diagnostic messages to standard output, where netcat keeps such things separated from its *output*
  • Telnet is incapable of listening for inbound connections, or using UDP instead.
Advertising

6 - Management

6.1 - Installation

  • install nmap on Windows
  • Linux
apt-get install -y netcat
network/netcat.txt · Last modified: 2019/10/09 11:35 by gerardnico