Using Netcat to Create ad hoc Links Between Applications or Machines

Netcat is a simple Unix utility which reads and writes data across network connections using the TCP or UDP protocol. It is often described as the "Swiss-army knife for TCP/IP" because of its flexibility and is often used to debug and investigate network connections directly from the command line or through scripts. This article shows how easy it is to form simple connections between applications or machines for those times when it isn't worth setting up something more elaborate.

Netcat is normally executed by running nc. The following examples will all use port 3333 and two switches in particular: -l to listen and -p to specify a port. Be aware that while the examples given can be very useful, the communication is unencrypted and could lead to security problems if you are not careful.

A Simple Chat Application

To create a simple chat application run the following commands on two machines and then start typing. Each time you press return a line will be sent to the other machine. Press ^C to quit.

On the server

$ nc -l -p 3333

On the client

$ nc server.example.com 3333

or

$ telnet server.example.com 3333

Remote Control an Application

If you don't want to setup an ssh or telnet server, you can quickly remote control an application using the -e switch to execute a command and pipe its stdin/stdout via the network port.

Be careful with this though as it is a huge security risk if you are not careful. The application you are remote controlling may be able to give a user more access and may ultimately be as bad as doing:

$ nc -l -p 3333 -e /bin/sh

Interactively Control an Application

To remote control gdb, the Gnu Debugger:

On the server

$ nc -l -p 3333 -e /usr/bin/gdb

On the client

$ nc server.example.com 3333

or

$ telnet server.example.com 3333

Using Expect to Control an Application

As above to remote control gdb, but this time it will be controlled via an expect script. The example brings up the help text within gdb and then quits.

On the server

$ nc -l -p 3333 -e /usr/bin/gdb

On the client create the following expect script as control_remote_gdb.exp

#!/usr/bin/env expect
spawn telnet server.example.com 3333

expect "(gdb)"
send "help\r"

expect "(gdb)"
send "quit\r"

expect eof

Now run the expect script

$ ./control_remote_gdb.exp

Connecting an Emulated Serial Port to pppd

When emulating a Commodore 64 within Vice you can pipe the input/output of the emulated RS-232 port to a command. This can be used to create an internet connection from the emulator by setting the exec process within Vice to '|nc 127.0.0.1 3333' and on the host machine run:

# pppd 9600 :192.168.1.64 passive nodetach noauth local pty "nc -l -p 3333 127.0.0.1"

pppd here uses a pseudo terminal to communicate with, which is using netcat to create a link between it and the virtual RS-232 port of the emulator.

Quick and Dirty File Transfer

For these examples, there are alternatives such as scp, rsync, etc. However, when security is less of an issue and you have netcat to hand, this is a really easy solution.

Single File Transfer

To transfer a single file between the client and the server.

Receiver (Server)

$ nc -l -p 3333  > someFile.tar

Sender (Client)

$ cat someFile.tar | nc receiver.example.com 3333

Create and Transfer a Tar File on the Fly

To create a tar file from a directory and transfer it between the client and server. The single - below tells tar to send to stdout.

Receiver (Server)

$ nc -l -p 3333 | > someFile.tar.gz

Sender (Client)

$ tar -czvf - someDir | nc receiver.example.com 3333

Where now?

There are many places that nc can make things easier. You probably won't need it everyday, but keep it in the back of your mind. One day something may come up where you find that nc is the perfect answer.

Creative Commons License
Using Netcat to Create ad hoc Links Between Applications or Machines by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Share This Post

Feedback/Discuss

Related Articles

Using C-Kermit to Exchange Files With Telnet BBS's

Most BBSs that are still running now do so via telnet. In many ways this is great as it allows people from all around the world to access a BBS as if it were local to them. The problem comes though, ...   Read More

Connecting to a Remote Serial Port over TCP/IP

Most modern machines don't have a serial port as standard; you could use a USB to serial lead, however, if you have another machine available that does have a serial port you can access it remotely ove...   Read More

Getting Colour ANSI Emulation to Work Properly When Connecting to a BBS With Telnet Under Linux

I have noticed that the number of people interested in using telnet to access BBSs seems to be growing, which I'm really pleased to see. However lots of people seem to be having trouble getting colour...   Read More

Setting up a Beowulf Cluster Using Open MPI on Linux

I have been doing a lot of work recently on Linear Genetic Programming. This requires a great deal of processing power and to meet this I have been using Open MPI to create a Linux cluster. What fol...   Read More

Connecting a Parallel Printer to a Modern Linux Machine Using a LogiLink USB to Parallel Cable, D-SUB 25pin

I have a number of older printers that I would like to connect to my modern machine, but have been unable to do so because my computer doesn't have a parallel interface. After searching the internet f...   Read More