Network - netcat (nc, ncat)

Map Of Internet 1973

About

netcat 1) is a net client/server command line tool for TCP or UDP protocol.

It can:

  • reads and writes data across network connections
  • acts as a client but also as a server
  • You can see it as the equivalent of telnet
  • It borrows its name from:

Example

Testing a port

An utility function to control that the service is up by controlling that we can make a tcp connection to the service port

Simple if statement

TOMCAT_PORT=6006
if [[ $(nc -z localhost ${TOMCAT_PORT}) -eq 0 ]]; then
	echo "Tomcat is up"
else
	echo "Tomcat is shutdown"
fi

While Statement

  • 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

Wait function with timeout

# usage
# wait_for_service service_name port
wait_for_service() {
  local SERVICE_NAME=$1
  local PORT=$2
  SERVICE_WAIT_TIMEOUT_SEC=20
  echo "Waiting for $SERVICE_NAME to start..."
  local CURRENT_WAIT_TIME=0

  while [[ $(echo | nc -w1 localhost $PORT >/dev/null 2>&1 ;echo $?) -ne 0 ]]; do
      printf '.'
      sleep 1
      if [ $((++CURRENT_WAIT_TIME)) -eq $SERVICE_WAIT_TIMEOUT_SEC ]; then
        printf "\nError: timed out while waiting for $SERVICE_NAME to start.\n"
        exit 1
      fi
  done
  printf '\n'
  echo "$SERVICE_NAME has started";
}

Wait for it

For a full functional example, see wait-for-it.sh

Send a HTTP request to a process

After having created a tcp connection, an application:

As netcat creates a tcp connection, you can for instance send HTTP get request. Example:

GET / HTTP/1.1
Host: gerardnico.com

we can send it as standard input with echo to a connection created with netcat

For SSL connection, you need to use another utility such as openssl because netcat does not support it. For HTTPS, you may use CURL

echo -e "GET / HTTP/1.1\nHost: gerardnico.com\n" | nc gerardnico.com 80

and you should get a redirection response telling you that you need to go to https://gerardnico.com/

HTTP/1.1 301 Moved Permanently
Date: Mon, 13 Apr 2020 12:26:26 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Mon, 13 Apr 2020 13:26:26 GMT
Location: https://gerardnico.com/
Server: cloudflare
CF-RAY: 58352ceb1ba9bf78-AMS

Send a directory between two computers

  • On the source server, pipe the result of a tar (archive) to a connection created witn nc
tar -cz . | nc -l -p $PORT
# l for listen
# p to define the port
  • On the remote server, read the connection and untar.
nc -w 10 $REMOTE_HOST $PORT | tar -xz

Syntax

After a connection is established, the standard input is sent to the host, and anything that comes back across the connection is sent to your standard output.

Client

To create a TCP connection:

  • Linux
nc host port
  • Windows
ncat host port

Server

Netcat can also function as a server, by listening for inbound connections on arbitrary ports

  • Linux
nc -l -p port

Why not telnet

  • Telnet has the “standard input EOF” problem. ie it will shut everything down and exit after an end-of-file on the standard input.
  • 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.

Installation

Windows

  • install nmap on Windows

Linux

apt-get install -y netcat
# centos, Redhat
yum install nmap





Discover More
Opendkim Test Mailer
Email - How to test if your server email configuration was done correctly ?

This article will show different server configuration testing in order to validate your email server configuration
Bash Liste Des Attaques Ovh
How to check a port? locally and remotely on Linux

This page is Port management on Linux. When you install a software (process), you need often to verify the available port in order to configure the component installation. with netstat, you can check...
Kafka Commit Log Messaging Process
Kafka - Installation Standalone / Open Source (Single Broker)

This page shows you how to install kafka from the open source package with a single broker (a single node) Kafka is working with zookeeper to store its data. A zookeeper server must be running before...
Map Of Internet 1973
Net Client

This page is clients that send packet to a socket. You can talk directly to a server that implements a protocol where the packet body is made of text such as: SMTP. HTTP This client can also...
Map Of Internet 1973
Network - (Internet) Socket (Network identifier)

A socket is session identifier for a connection between 2 processes via a particular protocol. List of sockets and their transport protocols: netsocket: The net socket is the most well-known and...
Map Of Internet 1973
Network - Open Port

How to know from a localhost which port are open on a remote host (ie that a firewall is not used or that the process is up and listen) When the scanner sends a SYN packet to open a TCP connection...
Map Of Internet 1973
Network - Server

A network server is a service that listens on a socket for inbound connections. This server receives packets and returns packets A Web/HTTP Server listens for TCP connection on the HTTP/HTTPS port...
Map Of Internet 1973
Network - TCP (Transmission Control Protocol)

The Transmission Control Protocol (TCP) is a protocol of the Internet protocol suite (TCP/IP). It originated in the initial network implementation in which it complemented the Internet Protocol (IP)....
Postfix - SMTP Banner

A SMTP banner is the protocol banner of SMTP (ie the response that a client will get when it connects to a SMTP server) With Telnet, we connect to the local smtp server submission port We receive...
Card Puncher Data Processing
Python Web - Web Server

in Python Python 2: Linux Bash syntax for Python 3+ After running the server you can connect to it from arbitrary IP addresses with netcat (nc) The server will report the connection indeed...



Share this page:
Follow us:
Task Runner