ArticleDirectory
- 1. Netcat in a server-client architecture
- 2. Use Netcat to transfer files
- 3. Netcat supports timeouts
- 4. Netcat supports IPv6 connectivity
- 5. Disable reading from stdin in Netcat
- 6. Force Netcat server to stay up
- 7. Configure Netcat client to stay up after EOF
- 8. Use Netcat with UDP protocol
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 2389hi, Server
On the terminal where server is running:
$ Nc-l 2389hi, 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' ining:
$ Cat testfilehello Test
And at the server side we have an empty file 'test'
Now, we run the server:
$ Nc-l 2389> Test
And run the client:
Cat testfile | NC localhost 2389
Now, when we see the 'test' file at the server end, we see:
$ Cat testhello 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 abve wocould 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 effect and hence the connection remains open forever.
4. Netcat supports IPv6 connectivity
The flag-4 or-6 specifies that Netcat utility shocould 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 2389tcp 0 0 localhost: 2389 localhost: 50851 establishedtcp 0 0 localhost: 50851 localhost: 2389 established
The first field in the above output wocould 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 local host 2389
Now, if we run the netstat command, we see:
$ Netstat | grep 2389tcp6 0 0 localhost: 2389 localhost: 33234 establishedtcp6 0 0 local host: 33234 localhost: 2389 established
So now a postfix '6' with 'tcp 'shows that NC is now using IPV6 ses SSEs.
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 2389hi
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
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 local host 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 already ed. in a normal scenario, if the NC client has es 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 processing ing EOF)
Client shoshould be started like:
NC-Q 5 local host 2389
Now if the client ever has es 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 2389udp 0 0 localhost: 42634 localhost: 2389 established