Network - netcat (nc, ncat)

Card Puncher Data Processing

Network - netcat (nc, ncat)

About

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

  • You can see it as the equivalent of telnet
  • It emprunts its name from the cat command

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





Recommended Pages
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
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...
Bash Liste Des Attaques Ovh
Linux - How to check port locally and remotely ?

Port management on Linux When you install software (process), you need often to verify the available port in order to configure the component installation. To find available port numbers, on the...
Card Puncher Data Processing
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...
Card Puncher Data Processing
Network - Server

A network server is a process that listens on a socket for inbound connections Web Server listens for TCP connection on the HTTP/HTTPS port and expects to receive a HTTP request in a text format ...
Card Puncher Data Processing
Network - TCP (Transmission Control Protocol)

The Transmission Control Protocol (TCP) is a protocol of the Internet protocol suite (TCP/IP). It is a protocol for one-to-one connections. TCP ensures that messages between computers are transmitted:...
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...
Netstat Tcp Listening Socket
TCP - Connection

in TCP A TCP server may serve several clients concurrently with the same local socket address (ie local IP address and port number) by creating a child process for each client for each connection. ...
Telnet Installation Windows
Telnet (Protocol, Client)

TELNET (TELetype NETwork) is a network protocol used on the Internet or local area network LAN connections. It was developed in 1969. By extension, “telnet” also refers to the program which provides...
Yarn Hortonworks
Zookeeper - Service / Server

How to interact with the Zookeeper service/server Start Zookeeper in its own terminal. From a zookeeper archive From a kafka archive netcat wait function where run create a new...



Share this page:
Follow us:
Task Runner