Eight practical Linux Netcat command examples

Source: Internet
Author: User
Document directory
  • 1. Use Netcat in the server-client architecture
  • 2. Use Netcat to transfer files
  • 3. Netcat supports timeout Control
  • 5. Disable reading data from standard input in Netcat
  • 6. Force the Netcat server to be started
  • 7. configuring the Netcat client will not exit because of EOF
  • 8. Use Netcat to process UDP protocol

Netcat or NC is a network tool kit for debugging and checking in Linux. It can be used to create TCP/IP connections. The maximum purpose is to process TCP/UDP sockets.

Here we will use some instances to learn the Netcat command.

1. Use Netcat in the server-client architecture

The Netcat tool runs in server mode and listens to specified ports.

1 $ nc -l 2389

Then you can connect to port 2389 in client mode:

1 $ nc localhost 2389

Now, if you enter some text, it will be sent to the server:

1 $ nc localhost 2389
2 HI, oschina

The following content is displayed in the terminal window of the server:

1 $ nc -l 2389
2 HI, oschina

 

2. Use Netcat to transfer files

The Netcat tool can also be used to transmit files. on the client side, assume that we have a testfile file:

1 $ cat
testfile
2 hello oschina

On the server side, there is an empty file named test.

Run the following command to enable the server:

1 $ nc -l 2389 > test

Then run the client:

1 cat testfile | nc localhost 2389

Then you stop the server. You can check that the content of test is the content of the testfile File Uploaded by the client just now:

1 $ cat
test
2 hello oschina

 

3. Netcat supports timeout Control

In most cases, we do not want to keep the connection. We can use the-W parameter to specify the idle timeout time for the connection. this parameter is followed by a value, which indicates the number of seconds, if the connection exceeds the specified time, the connection will be terminated.

Server:

1 nc -l 2389

Client:

1 $ nc -w 10 localhost 2389

The connection will be interrupted in 10 seconds.

Note: Do not use the-W and-l parameters on the server, because the-W parameter will have no effect on the server.

4. Netcat supports IPv6

The-4 and-6 parameters of Netcat are used to specify the IP address type, namely IPv4 and IPv6:

Server:

1 $ nc -4 -l 2389

Client:

1 $ nc -4 localhost 2389

Then we can use the netstat command to view the network conditions:

1 $ netstat
| grep
2389
2 tcp        0      0 localhost:2389          localhost:50851         ESTABLISHED
3 tcp        0      0 localhost:50851         localhost:2389          ESTABLISHED

Next, let's look at the IPv6 situation:

Server:

1 $ nc -6 -l 2389

Client:

1 $ nc -6 localhost 2389

Run the netstat command again:

1 $ netstat
| grep
2389
2 tcp6       0      0 localhost:2389          localhost:33234         ESTABLISHED
3 tcp6       0      0 localhost:33234         localhost:2389          ESTABLISHED

If the prefix is tcp6, the IPv6 address is used.

 

5. Disable reading data from standard input in Netcat

This function uses the-D parameter. See the following example:

Server:

1 $ nc -l 2389

Client:

1 $ nc -d localhost 2389
2 Hi

The Hi text you entered will not be sent to the server.

 

6. Force the Netcat server to be started

If the client connected to the server is disconnected, the server also exits.

Server:

1 $ nc -l 2389

Client:

1 $ nc localhost 2389
2 ^C

Server:

1 $ nc -l 2389
2 $

In the preceding example, the server exits immediately when the client is disconnected.

We can use the-k parameter to control the server from exiting due to client disconnection.

Server:

1 $ nc -k -l 2389

Client:

1 $ nc localhost 2389
2 ^C

Server:

1 $ nc -k -l 2389

 

7. configuring the Netcat client will not exit because of EOF

The Netcat client can use the-Q Parameter to control how long it will exit after receiving the EOF. The unit of this parameter is seconds:

Start the client as follows:

1 nc  -q 5  localhost 2389

Now, if the client receives the EOF, it will wait 5 seconds before exiting.

 

8. Use Netcat to process UDP protocol

Netcat uses the TCP protocol by default, but also supports UDP. You can use the-u parameter to enable UDP communication.

Server:

1 $ nc -4 -u -l 2389

Client:

1 $ nc -4 -u localhost 2389

In this way, both the client and the server use the UDP protocol, which can be viewed using the netstat command:

1 $ netstat
| grep
2389
2 udp        0      0 localhost:42634         localhost:2389   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.