Linux.com

Feature

CLI Magic: netcat

By Joe 'Zonker' Brockmeier on November 14, 2005 (8:00:00 AM)

Share    Print    Comments   

The response to my recent sysadmin toolbox article has been overwhelming. By far, readers' number one suggestion was to replace Telnet with netcat. Here then is an introduction to netcat for Linux users who may not be familiar with the "TCP/IP Swiss Army knife."

In the simplest terms, netcat is a utility that reads and writes data across the network. As you probably know already, you can write to a file or read from a file on your local machine using the cat utility. By running cat filename > filename2 , you can write the contents of a file to another file. By using cat > filename , you can write directly to a file from standard input.

The netcat utility works on the same principle as the cat utility, but over the network. This can be very useful in a number of situations, such as testing remote services, or for use in scripts, or just to copy files over the network. According to one source, you can even clone a hard drive over the network using netcat and dd.

Most *nix-type operating systems should already have a package for netcat. The most popular version of netcat is available from the SecurityFocus Web site. This is the version that ships with most Linux distros, and the most recent release seems to have been in 1996, so it should be current even if you're running an older version of Linux. There is also a version called GNU Netcat in the works, though I believe it lacks some of the functionality of the original. There are even versions of netcat available for Windows.

The syntax for netcat is pretty simple: netcat hostname port will connect you to a server on the port specified, and allow you to send input to whatever service answers on that port. For example, if you use netcat mailserver.mydomain.com 25, you'll connect to the SMTP daemon running on mailserver.mydomain.com -- assuming one is running, of course.

You can then send input to that host, and you'll see any response from the host. This is perfect for testing services like SMTP, IMAP, POP3, and HTTP interactively. You can do this with Telnet, but (as many readers pointed out) there can be problems with using Telnet. Telnet can interfere with some tests by sending additional data, the Telnet client can't be set up to listen for incoming connections, and Telnet doesn't easily lend itself to use in a script. Telnet is also limited in that it has support only for TCP and not UDP.

Using netcat

The netcat utility can also be used to transmit a file over the network, much as cat can be used to write the contents of a file to another file locally. Note that there are other methods that are usually more efficient, but there are occasions when netcat might come in handy.

To transmit a file, you use netcat on the host that's receiving the file and the host that's sending the file. On the receiving host, run netcat -l -p 1234 > filename. The -l option tells netcat to listen, and the -p option tells netcat the port number to listen on. You can replace the port number (1234) with any port number you'd like, though you need to run as root to bind to ports below 1024.

To send the file, run cat filename | netcat hostname 1234 -q 10. This sends the file to netcat, which then sends the data to the host you specify, on port 1234. The -q option tells netcat to quit 10 seconds after the end of the file (EOF).

You can also use netcat to copy the output of a command to a remote server. For instance, if you want to do a quick and dirty backup to another host, you can pipe the output of the tar command on your local server to a remote server. On the remote machine, run netcat -l -p 1234 > filename.tgz . This will tell netcat to listen on port 1234, and to output the data it receives to a file called filename.tgz.

On the local server, run tar -zcf - file | netcat -w 10 hostname 1234 . This will run the tar command against the file specified on the command line. This will work with a directory as well; just replace file with the name of the directory. The "-" after the tar options tells tar to send its output to standard out, rather than to a file. The pipe symbol redirects the output from tar to netcat, which copies it to the host specified on the command line. The -w option tells netcat to wait for 10 seconds after it has copied the data, and then close the connection.

If you want to glean a little more information about what's going on with netcat, the -v option makes netcat more verbose. If you use -v with netcat in listening mode, it will tell you when a connection is made, and from where, like this:

$ netcat -l -p 3333 -v > filename.tgz
listening on [any] 3333 ...
connect to [10.0.0.26] from dhoffryn [10.0.0.15] 57450

If that's not enough information for you, turn on extra verbosity with -vv:

$ netcat -l -p 3333 -vv > filename.tgz
listening on [any] 3333 ...
connect to [10.0.0.26] from dhoffryn [10.0.0.15] 57451
sent 0, rcvd 133120

This has the added bonus of showing how much data was sent and received. This works either way, so you can turn on verbosity when using netcat to send data or act as a client for remote services, and netcat will show the host and port it's connecting to, and how much data has been sent or received.

Another option that may come in handy is the -c option, which tells netcat to execute a command with /bin/sh after it connects -- sending the output to the other side of the connection. This can be used on either side of the connection. To send data from a command to a remote host, you could use netcat -c '/bin/command' hostname port . When netcat connects to the service on the remote host, it will attempt to send the output of /bin/command. If you use netcat -l -p 1234 -c '/bin/command', it will send the output of /bin/command to the first client that connects to port 1234, and then close the connection.

Learning more about netcat

I've only scratched the surface of netcat's capabilities. Be sure to read the netcat man page for additional information, and netcat's README file as well, which is located under /usr/share/doc/netcat/README.gz on Ubuntu and Debian, and /usr/share/doc/packages/netcat/README on SUSE 9.3.

The regular distribution of netcat also includes a set of sample scripts that use netcat for probing remote hosts, copying files over the network, or grabbing Web pages. These are somewhat useful in their own right, but also provide excellent examples of what netcat can do. Netcat is very powerful utility, and well worth trying out.

Share    Print    Comments   

Comments

on CLI Magic: netcat

Note: Comments are owned by the poster. We are not responsible for their content.

Great article!

Posted by: Anonymous Coward on November 14, 2005 09:51 PM
Thanks for a great article, I think that it was very good.<nobr> <wbr></nobr>:)

netcat can be really useful if you want to transfer a file between two hosts but dont want to download, install and configure an FTP server which might take long time if all you want todo is to quickly transfer a little file.

But I think that maybe its not a good to run it on a server for backup scripts since it can overwrite that file and other people can connect to the netcat too and write their data or fill up the diskspace unless that port is filtered to a specific IP addr.
For backup scripts and production-use a FTP server (preferably over SSL) would be a better and more reliable solution.

Netcat is cool, and the -c option is very cool too but maybe people should take care when use it.

#

And if you don't have netcat

Posted by: Anonymous Coward on November 15, 2005 06:05 AM
...you can use bash (possibly to get netcat). E.g.,

<tt>cat </dev/tcp/127.0.0.1/daytime</tt>



(I have yet to find a way to get bash to listen though.)

#

lower back pain

Posted by: Anonymous Coward on May 28, 2006 01:55 PM
[URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]
[URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]
[URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.com] Pain relief [/URL]
[URL=http://nervepainrelief.jeeran.com/painrelief<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]

#

relief joint

Posted by: Anonymous Coward on May 28, 2006 01:58 PM
[URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]

  [URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]

  [URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.com] Pain relief [/URL]
[URL=http://nervepainrelief.jeeran.com/painrelief<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]

#

Excellent!

Posted by: Anonymous Coward on November 14, 2005 10:29 PM
I also agree that this was very well written. I discovered good 'ol "nc" a couple of years ago, and it's come in handy many, many times.


Particularly at work, where my team all has identical machines (and the company has opted for somewhat unreliable/cheap computers from a large computer company which had merged with another large computer company a couple years ago), this has come in handy. When we have a hardware failure (and our team of 5 developers have had 8 of them in 18 months), we simply clone one of our working machines to the replacement. It's as simple as booting Knoppix on both and doing:


on the new machine "nc -l -p 1234 | dd of=/dev/sda" (or<nobr> <wbr></nobr>/dev/hda or whatever is your hard drive)


on the old machine "dd if=/dev/sda | nc IP_ADDRESS 1234" (substitute IP_ADDRESS for the machine you're sending to)


You can throw in a pipe to gzip if you want to compress before sending, or you can use a similar command to make an image of your entire hard disk to a file (which, after so many failures, we have done so we don't need to take another machine down to make a copy).

#

Already invented?

Posted by: Anonymous Coward on November 15, 2005 06:53 PM
"Everything that can be invented, is already invented" -- Charles H. Duell, 1899

I had to backup some partitions over a network few months ago. I didn't knew about netcat so i wrote my own utility with similar features, called... catnet<nobr> <wbr></nobr>:-)

Next time i'll search before "inventing" anything.

#

Addition: netcat + telnet client = telnet server

Posted by: Anonymous Coward on November 15, 2005 07:56 PM
<a href="http://www.sns.ias.edu/~jns/?pagename=Security" title="ias.edu">http://www.sns.ias.edu/~jns/?pagename=Security</a ias.edu>

Check "A telnet client can be used as a server"

Anyway, a good article. Thanks!

#

Securing netcat &amp; transferring files

Posted by: Anonymous Coward on November 15, 2005 11:02 PM
I was taught this by Hobbit who wrote netcat.

ssh into a system (a.com) and forward a port:

      ssh a.com -L 51001:127.0.0.1:51001

Now you can transfer files:

    On a.com, nc -lvnp 51001 127.0.0.1 > file

    On your system, nc -v -w 2 127.0.0.1 51001 file

The file will get transferred, securely, with less overhead then scp, with error correction (via TCP).

#

Re:Securing netcat &amp; transferring files

Posted by: Anonymous Coward on November 15, 2005 11:05 PM
How about transfering from a.com to b.com when they can't talk direct, but you can ssh into both?


    ssh into a.com as above.

    ssh b.com -L 51002:127.0.0.1:51002

      On a.com:nc -lvnp 51001 127.0.0.1 file

      On your system, connect the pipes:

          nc -v -w 2 127.0.0.1 51001 \

              | nc -v -w2 127.0.0.1 51002

#

Re:Securing netcat &amp; transferring files

Posted by: Anonymous Coward on November 28, 2005 03:56 AM
While it's true you can do this, it's really the "hard way."

A much easier method is:

cat file | ssh a.com "cat - ><nobr> <wbr></nobr>/path/to/file"

#

alternatives

Posted by: Anonymous Coward on November 16, 2005 12:22 AM
If you want something with a lot more capabilities, and less bizarre behaviour, check out socat.

<a href="http://freshmeat.net/projects/socat/" title="freshmeat.net">http://freshmeat.net/projects/socat/</a freshmeat.net>

#

netcat is limiting...

Posted by: Anonymous Coward on December 08, 2005 07:11 AM
at least because when it is in listen mode, it only accepts one connection and then dies. Sure, you can put some other proxy there to spawn netcat or other connections but that is not trivial command line use, especially to make it robust, user friendly, and safe.

Hope "netcat++" is or will be up to the task.

#

PartImage

Posted by: Anonymous Coward on November 16, 2005 10:29 PM
As for backing up HD images over the network I would recommend partimage
( <a href="http://www.partimage.org/" title="partimage.org">http://www.partimage.org/</a partimage.org> )



Also sysresccd (also from the same people as partimage) has become extremely useful to me



and yes, I used to use that exact same dd and nc trick (from the same website no less!) a few years back, but if you have a lot of empty space on the partition, it will get sent as well -- partimage won't send the free space on the partition.

#

Re:PartImage

Posted by: Anonymous Coward on November 18, 2005 02:45 AM
I came up with a good solution for not having nc+dd copy empty disk space. Use DD on the live machine to fill up unallocated space with zeros, and then pipe the copy through gzip with --fast. So

live machine$ dd if=/dev/zero of=/DELETEME
live machine$ rm<nobr> <wbr></nobr>/DELETEME
[repeat per partition]

then,

knoppix# dd if=/dev/hda | gzip --fast | nc<nobr> <wbr></nobr>...

It takes a while, but it's good for making compressed disk images.

#

unencrypted

Posted by: Anonymous Coward on November 26, 2005 08:59 PM
As someone mentioned, backup routines shold probably by done over ssh tunnel to avoid unencrypted data transfer. Ofcourse, if it is trusted network then it is up to you, but being a liittle bit paranoid does not harm.

#

Why not regular cat?

Posted by: Anonymous Coward on December 13, 2005 12:49 AM
I've always wondered why network devices were not simply device files like everything else. Requiring special tools to work with a network device solely because it isn't a file seems, to me, to break the unix philosophy.

#

Re:Why not regular cat?

Posted by: Anonymous Coward on December 22, 2005 05:33 AM
The network stack is one of those things that needs to run in kernelspace for speed. It would be very cool if I could chmod 700<nobr> <wbr></nobr>/dev/eth0 though

#

Excellent work!

Posted by: Administrator on November 15, 2005 12:30 PM
I was one of those that asked for you to do it in netcat and I appreciate you doing that . These instructiosn are easy to and thanks for that as well. Great job.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya