Sunday 18 March 2012

Netcat Command Examples

The netcat command is used for an awful lot when it comes to TCP(Transfer Control Protocol) or UDP (User Datagram Protocol). It's mainly used to open TCP connections and send UDP packages to remote computers, although it can also be used for port scanning. In English the netcat command can be used to send data over a network.

Below I have listed a few basic commands to get you familiar with the program. Netcat can be abbreviated to nc in the shell.

Display Information sent by Browser
nc -l 8000
have netcat listen on port 8000, point browser to http://localhost:8000/ and see the information sent. Netcat will terminate once the browser has been closed.
-l = means that Netcat will listen to a connection rather then try and create one.

Sharing a file with netcat through port 80
nc -v -l 80 < file.txt
With this command you can share a file across a network, from a second machine, open a browser and go to http://ip-address-of-first-machine/ to access the file.
-v = Have nc give out a more verbose output(more detail).
-l = listen and do not create (as mentioned above).
80 = the HTTP port.
< file.txt = send the file over port 80.

Tail a log file over a network
tail -f error_log | nc -l 1234
This will serve a log file over a network through port 1234. This can be very useful if you have a big office.
to view this point a browser to the specified server/port (eg. 192.168.0.1:1234) and you can view the log file updates in real time! which is very cool.
tail is a command used to show the bottom of a file.
-f = this will allow the file to amended in real time.

Netcat as a Port-scanner
nc -v -n -z -w 1 AnIPaddress 22-1000
This should only be used on your own network with any permission required.
-v = Verbose (as explained above).
-n = Do not do a DNS or service look up on the specified address, host name or port.
-z = This is to specify that nc should just scan for listening daemons and not to send data to them (this means to look for open ports and not send anything to them).
-w 1 = this option sets a time-out period, if the connection is idle for more then 1 second then the connection is silently closed.
22-1000 = This will scan ports 22 to 1000 with the intent to find an available port.

Check the server is up, if not mail me
nc -zw2 www.example.com 80 || echo http service is down | mail -s 'http is down' admin@example.com
This is something I use whilst on 24 hour support. It's long but easy to understand.
-zw2 = This switch is a combination of -z and -w which are explained above. The switch will scan for listening daemons (without sending data to them) and have a time-out range of 2 seconds.
The command checks the server using port 80 (HTTP), if it received a time-out message it will send an email with the subject 'http is down' and 'http service is down' as the content to the specified address. Simple!

I hope this gives you a decent grounding for understanding Netcat, I find the best way to understand a program is to mess about with the commands and read through the man files. Any questions feel free to comment.

No comments:

Post a Comment