Explore

Loading

Tuesday, April 24, 2012

8 Practical Linux Netcat NC Command Examples


Netcat or nc is a networking utility for debugging and investigating the network.
This utility can be used for creating TCP/UDP connections and investigating them. The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.
In this article we will learn about the netcat command by some practical examples.  
1. Netcat in a Server-Client Architecture
The netcat utility can be run in the server mode on a specified port listening for incoming connections.
$ nc -l 2389
Also, it can be used in client mode trying to connect on the port(2389) just opened
$ nc localhost 2389
Now, if we write some text at the client side, it reaches the server side. Here is the proof :
$ nc localhost 2389
HI, server
On the terminal where server is running :
$ nc -l 2389
HI, server
So we see that netcat utility can be used in the client server socket communication.

2. Use Netcat to Transfer Files

The netcat utility can also be used to transfer files. At the client side, suppose we have a file named ‘testfile’ containing :
$ cat testfile
hello test
and at the server side we have an empty file ‘test’
Now, we run the server as :
$ nc -l 2389 > test
and run the client as :
cat testfile | nc localhost 2389
Now, when we see the ‘test’ file at the server end, we see :
$ cat test
hello test
So we see that the file data was transfered from client to server.

3. Netcat Supports Timeouts

There are cases when we do not want a connection to remain open forever. In that case, through ‘-w’ switch we can specify the timeout in a connection. So after the seconds specified along with -w flag, the connection between the client and server is terminated.
Server :
nc -l 2389
Client :
$ nc -w 10 localhost 2389
The connection above would be terminated after 10 seconds.
NOTE : Do not use the -w flag with -l flag at the server side as in that case -w flag causes no affect and hence the connection remains open forever.

4. Netcat Supports IPV6 Connectivity

The flag -4 or -6 specifies that netcat utility should use which type of addresses. -4 forces nc to use IPV4 address while -6 forces nc to use IPV6 address.
Server :
$ nc -4 -l 2389
Client :
$ nc -4 localhost 2389
Now, if we run the netstat command, we see :
$ netstat | grep 2389
tcp        0      0 localhost:2389          localhost:50851         ESTABLISHED
tcp        0      0 localhost:50851         localhost:2389          ESTABLISHED
The first field in the above output would contain a postfix ’6′ in case the IPV6 addresses are being used. Since in this case it is not, so a connection between server and client is established using IPV4 addresses.
Now, If we force nc to use IPV6 addresses
Server :
$ nc -6 -l 2389
Client :
$ nc -6 localhost 2389
Now, if we run the netstat command, we see :
$ netstat | grep 2389
tcp6       0      0 localhost:2389          localhost:33234         ESTABLISHED
tcp6       0      0 localhost:33234         localhost:2389          ESTABLISHED
So now a postfix ’6′ with ‘tcp’ shows that nc is now using IPV6 addresses.

5. Disable Reading from STDIN in Netcat

This functionality can be achieved by using the flag -d. In the following example, we used this flag at the client side.
Server :
$ nc -l 2389
Client :
$ nc -d localhost 2389
Hi
The text ‘Hi’ will not be sent to the server end as using -d option the read from stdin has been disabled.

6. Force Netcat Server to Stay Up

If the netcat client is connected to the server and then after sometime the client is disconnected then normally netcat server also terminates.
Server :
$ nc -l 2389
Client :
$ nc localhost 2389
^C
Server :
$ nc -l 2389
$
So, in the above example we see that as soon as the client got disconnected the server was also terminated.
This behavior can be controlled by using the -k flag at the server side to force the server to stay up even after the client has disconnected.
Server :
$ nc -k -l 2389
Client :
$ nc localhost 2389
^C
Server :
$ nc -k -l 2389
So we see that by using the -k option the server remains up even if the client got disconnected.

7. Configure Netcat Client to Stay Up after EOF

Netcat client can be configured to stay up after EOF is received. In a normal scenario, if the nc client receives an EOF character then it terminates immediately but this behavior can also be controlled if the -q flag is used. This flag expects a number which depicts number of seconds to wait before client terminates (after receiving EOF)
Client should be started like :
nc  -q 5  localhost 2389
Now if the client ever receives an EOF then it will wait for 5 seconds before terminating.

8. Use Netcat with UDP Protocol

By default all the sockets that nc utility creates are TCP protocols but this utility also works with UDP protocol. To enable UDP protocol the -u flag is used.
Server :
$ nc -4 -u -l 2389
Client :
$ nc -4 -u localhost 2389
Now, both the server and client are configured to use UDP protocol. This can be confirmed by the following netstat command. So we see that this connection is now using the UDP protocol.
$ netstat | grep 2389
udp        0      0 localhost:42634         localhost:2389          ESTABLISHED

How to set up Raspberry Pi

The Raspberry Pi is an exciting device that will hopefully bring computer programming (and Linux) to the masses. The low-priced, single-board computer features 256MB of RAM, a 700MHz ARM processor and a powerful GPU capable of 1080p video playback at 30fps and 3D graphics performance comparable to that of the original Xbox games console.
The idea behind the Raspberry Pi is to provide a computing environment where children can have the freedom to program without having to worry about the consequences of possibly breaking the family computer.
The device has also gained a lot of attention from the hacker community. Want a low-powered, low-cost web server, media centre, or SSH client? Then this could be exactly what you’re looking for. Throw in a network switch and you could have a cluster! The possibilities are virtually endless.
As you might imagine, the Raspberry Pi is a specialised hardware platform, similar to that of a smartphone. For this reason, operating systems are provided as images that are flashed to an SD card. There are currently three Linux flavours for the device: Debian, Fedora and Arch Linux. Debian is our recommended distribution out of the three on offer, and will be the one used in these tutorials.
By this point, you’re probably dying to rip the Pi out of the box and get started, so let’s do it!

01 Download the latest Debian image

Head over to the Downloads section of RaspberryPi.org and download the latest Debian root file system. The image can be downloaded via HTTP download or torrent. The Debian image is roughly 800MB and about 2GB when extracted, so make sure you have enough space free available.

02 Download the SHA-1 checksum

We want to verify the image we just downloaded before we carry on. You need to select the direct download option on the download page to get to the load balancer page. Once you’re there, right click on the SHA‑1 checksum hyperlink and select the option to save the link. Save the file in the same location you saved the Debian ZIP file.

03 Start your terminals

Check your current location using the pwd command. To list the files in the current directory, use the ls command. Use the cd command to navigate to where you saved the image. You can use cd.. to go back a directory. Once you’re in the correct directory, use ls to list the files and check they are both present.

04 Verifying the image

Use the sha1sum command with the following syntax: ‘sha1sum –check [sha1 file]’. Our file was debian6-17-02-2012.zip.sha1. The command may take a few minutes to run and you’ll get a result when it’s finished. If the result is okay, you can carry on. If not, go back to step 1!

05 Extract the Debian image

We now need to extract the image from the ZIP file. To do this, we’ll be using a tool called unzip. You may need to use your package manager to install it if it’s not already on your system. To extract the image, use the unzip command with the following syntax: ‘unzip [zip file]’. In our case, the command was ‘unzip debian6-17-02-2012.zip’. Once the command has finished, use ls to verify that a new folder has been created. This folder will contain the image file.

06 Connect your card reader

Insert your SD card into your card reader and connect it up to your PC if you’re using a USB reader rather than an internal reader. Decline any offers to view the contents of the SD card as we’re still working from the terminal.

07 Locate your SD card

We now need to find the path to your SD card because it could be disastrous if dd was used on the wrong device. We’ll be using the command ‘fdisk -l’. Fdisk requires root privileges so you’ll either need to use the command ‘sudo fdisk -l’ or switch to root using the su command before running ‘fdisk -l’. The ‘fdisk -l’ command simply lists each disk attached to your computer. The SD card we used was 8GB in capacity. There is a disk in our ‘fdisk –l’ output with a capacity of 8068MB, so that must be our SD card. The path to our SD card is /dev/sdb. Make note of yours as you’ll be using it in the next couple of steps.

08 Writing the image to your SD card

Start by using the cd command to change into the directory where the Debian image was extracted. This next part can break your system if you write to the wrong storage device with dd, so check and check again! As with fdisk, the dd command requires root privileges so you either need to start the following commands with sudo or switch to root using su before running them. Use the dd command with the syntax:
dd bs=1M if=[debian .img file] of=[path to your SD card]
In our case, the command was: ‘dd bs=1M if=debian6-17-02-2012.zip of=/dev/sdb’. dd doesn’t display a progress bar but no news is good news in this case. You’ll see a summary of the transfer once the command is finished.

09 Disconnect and reconnect

Before continuing, disconnect and then reconnect your SD card reader to ensure that the kernel has reloaded the newly flashed partition structure.

10 Start up GParted

The Debian image currently only takes up 2GB of space on the SD card, so we need to use GParted to resize the partitions so that we can make use of the full capacity on the card. You may need to install GParted using your package manager if you don’t already have it on your system. Start GParted and make sure that your SD card is selected in the combo box on the top right of the window.

11 Resize your partitions

Use the Resize/Move tool to move the linux-swap partition to the end of the SD card and then resize the main ext4 partition to fill up the space all the way up to the linux-swap partition. You should have two pending operations once you have done this.

12 Apply the changes

 

Full Story: http://www.linuxuser.co.uk/tutorials/how-to-set-up-raspberry-pi/

 By  Liam Fraser