nc

From RaySoft

The nc (or netcat) utility is used for just about anything under the sun involving TCP or UDP. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike telnet, nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet does with some.[1]

Documentation

Syntax

nc [PARAMETER ...] [HOST] [PORT]

Parameters

-4
Forces nc to use IPv4 addresses only.
-6
Forces nc to use IPv6 addresses only.
-i INTERVAL
Specifies a delay time INTERVAL between lines of text sent and received. Also causes a delay time between connections to multiple ports.
-l
Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host. It is an error to use this option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored.
-p PORT
Specifies the source PORT nc should use, subject to privilege restrictions and availability. It is an error to use this option in conjunction with the -l option.
-s ADDRESS
Specifies the ADDRESS of the interface which is used to send the packets. It is an error to use this option in conjunction with the -l option.
-u
Use UDP instead of the default option of TCP.
-v
Have nc give more verbose output.
-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, i.e. nc will listen forever for a connection, with or without the -w flag. The default is no timeout.
-z
Specifies that nc should just scan for listening daemons, without sending any data to them. It is an error to use this option in conjunction with the -l option.

Examples

Logging to syslog
echo "<133>${0##*/}[$$]: Start script" \
| nc -w1 -u 'localhost' 514
Send commands to an SMTP server with delays
(
   sleep '1'
   echo  'HELO oxygen.raysoft.loc'
   
   sleep '1'
   echo  'MAIL FROM: <alex@raysoft.loc>'

   sleep '1'
   echo  'RCPT TO: <melman@raysoft.loc>'

   sleep '1'
   echo  'DATA'

   sleep '1'
   echo  'From: Alex the Lion <alex@raysoft.loc>'
   echo  'To: Melman the Giraffe <melman@raysoft.loc>'
   echo  'Subject: Who are you?'
   echo
   echo  'Hi Melman,'
   echo  'How are you?'
   echo
   echo  'C U,'
   echo  'Alex'
   echo  '.'

   sleep '1'
   echo  'QUIT'
) | nc -i 1 'mail.raysoft.loc' 25

Output:

220 mail.raysoft.loc ESMTP
250 mail.raysoft.loc Hello oxygen.raysoft.loc [10.0.0.101]
250 <alex@raysoft.loc> is syntactically correct
250 <melman@raysoft.loc> verified
250 OK
221 mail.raysoft.loc closing connection
Setting up a one-shot webserver on port 8080 to present the content of a file[2]
provide_file() {
   if [[ $# -ne 1 ]]; then
      echo "Usage: ${FUNCNAME} FILE"
      return 1
   fi
   
   if [[ ! -f "$1" ]]; then
      my_carp "'$1' does not exist"
      return 1
   fi

   echo "The file '$1' is available on ${HOSTNAME}:8080 or localhost:8080"
   echo 'Close the service with Ctrl-C'

   while true; do
      {
         echo -ne 'HTTP/1.0 200 OK\r\n'
         echo -ne "Content-Length: $(wc -c < "$1")\r\n\r\n";
         cat "$1"
      } | nc -l 8080
   done
}

Output:

GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0

References