- Send TCP/UDP Packets on Linux using /dev/tcp & /dev/udp Pseudo Devices
- Base Command
- UDP
- TCP
- 👉 Any questions? Please comment below.
- Share on
- Leave a comment
- You may also enjoy
- [SOLVED] Fix Kubernetes Flannel & CoreDNS Failure with Error: x509: certificate is valid for , , not
- JavaScript: Remove element from Array — by index / value / position / range / filter function (programmatically)
- [SOLVED] Fix Kubernetes Client Certificates Issue with Error: Part of the existing bootstrap client certificate is expired . failed to run Kubelet: unable to load bootstrap kubeconfig: stat /etc/kubernetes/bootstrap-kubelet.conf: no such file or directory
- [SOLVED] Fix npm ERR! code EINTEGRITY , integrity checksum failed , tarball data seems to be corrupted , npm ERR! shasum check failed , npm WARN registry Unexpected warning issues during npm install
- Как отправить TCP или UDP пакет в Linux?
- Как отправить TCP пакет на определенный ip:порт в Linux
- Как отправить UDP пакет на определенный ip:порт в Linux
- How To Use Netcat to Establish and Test TCP and UDP Connections
- General Syntax
- How To Use Netcat for Port Scanning
- How To Communicate through Netcat
- How To Send Files through Netcat
- How To Use Netcat as a Simple Web Server
- Conclusion
Send TCP/UDP Packets on Linux using /dev/tcp & /dev/udp Pseudo Devices
Pseudo tools like /dev/tcp & /dev/udp can be used to easily send an instant TCP/UDP packet on Linux terminal.
Here is a quick tutorial on sending UDP and TCP data on Linux terminal using /dev/udp and /dev/tcp .
Base Command
UDP
$ echo "This is my UDP message" > /dev/udp/127.0.0.1/30000
TCP
$ echo "This is my TCP message" > /dev/tcp/127.0.0.1/30100
👉 Any questions? Please comment below.
Updated: January 02, 2020
Share on
Leave a comment
You may also enjoy
[SOLVED] Fix Kubernetes Flannel & CoreDNS Failure with Error: x509: certificate is valid for , , not
Use the correct cluster configuration for your control plane when regenerating new certificates
JavaScript: Remove element from Array — by index / value / position / range / filter function (programmatically)
Many ways to remove items from a JavaScript array
[SOLVED] Fix Kubernetes Client Certificates Issue with Error: Part of the existing bootstrap client certificate is expired . failed to run Kubelet: unable to load bootstrap kubeconfig: stat /etc/kubernetes/bootstrap-kubelet.conf: no such file or directory
Renew the expiring/expired Kubernetes PKI (client certificates & keys)
[SOLVED] Fix npm ERR! code EINTEGRITY , integrity checksum failed , tarball data seems to be corrupted , npm ERR! shasum check failed , npm WARN registry Unexpected warning issues during npm install
Clean npm cache, node_modules, package-lock.json files to recover from all ‘npm ERR! code EINTEGRITY’ error messages during ‘npm install’
Как отправить TCP или UDP пакет в Linux?
Часто при тестировании каких-либо приложений может возникнуть необходимость проверить, доходят ли определенные пакеты по udp/tcp до адресата, например, при проверке функционирования фаервола или же проверки работоспособности проброса портов. В данной статье будет описан простой способ это сделать с помощью командой строки.
Как отправить TCP пакет на определенный ip:порт в Linux
Для отправки tcp пакета на определенный IP адрес и определенный порт, можно воспользоваться следующей командой:
echo -n "test" >/dev/tcp/1.2.3.4/12345
1.2.3.4 — это IP адрес, на который мы будем посылать наш tcp пакет.
12345 — это порт, на который мы будем посылать наш tcp пакет Альтернативным вариантом может быть использование утилиты nmap:
sudo nmap -sT -p 12345 1.2.3.4
Как отправить UDP пакет на определенный ip:порт в Linux
Для отправки udp пакета на определенный IP адрес и определенный порт, можно воспользоваться следующей командой:
echo -n "test" >/dev/udp/1.2.3.4/12345
1.2.3.4 — это IP адрес, на который мы будем посылать наш udp пакет.
12345 — это порт, на который мы будем посылать наш udp пакет Альтернативным вариантом может быть использование утилиты nmap:
sudo nmap -sU -p 12345 1.2.3.4
Проверить получение tcp и udp пакетов на определенный порт на удаленном компьютере можно с помощью различных утилит, например, с помощью tcpdump.
tcpdump -i eth0 port 12345 -vvv -X
How To Use Netcat to Establish and Test TCP and UDP Connections
Linux is known for having a great number of mature, useful command line utilities available out of the box in most distributions. Often, system administrators can do much of their work using the built-in tools without having to install additional software.
In this guide, we will discuss how to use the netcat utility. This versatile command can assist you in monitoring, testing, and sending data across network connections.
Netcat should be available on almost any modern Linux distribution. Ubuntu ships with the BSD variant of netcat, and this is what we will be using in this guide. Other versions may operate differently or provide other options.
General Syntax
By default, netcat operates by initiating a TCP connection to a remote host.
This will attempt to initiate a TCP connection to the defined host on the port number specified. This functions similarly to the old Linux telnet command. Keep in mind that your connection is entirely unencrypted.
If you would like to send a UDP packet instead of initiating a TCP connection, you can use the -u option:
You can specify a range of ports by placing a dash between the first and last:
This is generally used with some additional flags.
On most systems, we can use either netcat or nc interchangeably. They are aliases for the same command.
How To Use Netcat for Port Scanning
One of the most common uses for netcat is as a port scanner.
Although netcat is probably not the most sophisticated tool for the job (nmap is a better choice in most cases), it can perform simple port scans to easily identify open ports.
We do this by specifying a range of ports to scan, as we did above, along with the -z option to perform a scan instead of attempting to initiate a connection.
For instance, we can scan all ports up to 1000 by issuing this command:
Along with the -z option, we have also specified the -v option to tell netcat to provide more verbose information.
The output will look like this:
Outputnc: connect to domain.com port 1 (tcp) failed: Connection refused nc: connect to domain.com port 2 (tcp) failed: Connection refused nc: connect to domain.com port 3 (tcp) failed: Connection refused nc: connect to domain.com port 4 (tcp) failed: Connection refused nc: connect to domain.com port 5 (tcp) failed: Connection refused nc: connect to domain.com port 6 (tcp) failed: Connection refused nc: connect to domain.com port 7 (tcp) failed: Connection refused . . . Connection to domain.com 22 port [tcp/ssh] succeeded! . . .
As you can see, this provides a lot of information and will tell you for each port whether a scan was successful or not.
If you are actually using a domain name, this is the form you will have to use.
However, your scan will go much faster if you know the IP address that you need. You can then use the -n flag to specify that you do not need to resolve the IP address using DNS:
The messages returned are actually sent to standard error (see our I/O redirection article for more info). We can send the standard error messages to standard out, which will allow us to filter the results easier.
We will redirect standard error to standard output using the 2>&1 bash syntax. We will then filter the results with grep :
OutputConnection to 198.51.100.0 22 port [tcp/*] succeeded!
Here, we can see that the only port open in the range of 1–1000 on the remote computer is port 22, the traditional SSH port.
How To Communicate through Netcat
Netcat is not restricted to sending TCP and UDP packets. It also can listen on a port for connections and packets. This gives us the opportunity to connect two instances of netcat in a client-server relationship.
Which computer is the server and which is the client is only a relevant distinction during the initial configuration. After the connection is established, communication is exactly the same in both directions.
On one machine, you can tell netcat to listen to a specific port for connections. We can do this by providing the -l parameter and choosing a port:
This will tell netcat to listen for TCP connections on port 4444. As a regular (non-root) user, you will not be able to open any ports under 1000, as a security measure.
On a second server, we can connect to the first machine on the port number we chose. We do this the same way we’ve been establishing connections previously:
It will look as if nothing has happened. However, you can now send messages on either side of the connection and they will be seen on either end.
Type a message and press ENTER . It will appear on both the local and remote screen. This works in the opposite direction as well.
When you are finished passing messages, you can press CTRL-D to close the TCP connection.
How To Send Files through Netcat
Building off of the previous example, we can accomplish more useful tasks.
Because we are establishing a regular TCP connection, we can transmit just about any kind of information over that connection. It is not limited to chat messages that are typed in by a user. We can use this knowledge to turn netcat into a file transfer program.
Once again, we need to choose one end of the connection to listen for connections. However, instead of printing information onto the screen, as we did in the last example, we will place all of the information straight into a file:
The > in this command redirects all the output of netcat into the specified filename.
On the second computer, create a simple text file by typing:
We can now use this file as an input for the netcat connection we will establish to the listening computer. The file will be transmitted just as if we had typed it interactively:
We can see on the computer that was awaiting a connection, that we now have a new file called received_file with the contents of the file we typed on the other computer:
OutputHello, this is a file
As you can see, by piping things, we can easily take advantage of this connection to transfer all kinds of things.
For instance, we can transfer the contents of an entire directory by creating an unnamed tarball on-the-fly, transferring it to the remote system, and unpacking it into the remote directory.
On the receiving end, we can anticipate a file coming over that will need to be unzipped and extracted by typing:
The ending dash (-) means that tar will operate on standard input, which is being piped from netcat across the network when a connection is made.
On the side with the directory contents we want to transfer, we can pack them into a tarball and then send them to the remote computer through netcat:
This time, the dash in the tar command means to tar and zip the contents of the current directory (as specified by the * wildcard), and write the result to standard output.
This is then written directly to the TCP connection, which is then received at the other end and decompressed into the current directory of the remote computer.
This is just one example of transferring more complex data from one computer to another. Another common idea is to use the dd command to image a disk on one side and transfer it to a remote computer. We won’t be covering this here though.
How To Use Netcat as a Simple Web Server
We’ve been configuring netcat to listen for connections in order to communicate and transfer files. We can use this same concept to operate netcat as a very simple web server. This can be useful for testing pages that you are creating.
First, let’s make a simple HTML file on one server:
Here is some simple HTML that you can use in your file:
Level 1 header
Subheading
Normal text here
Without root privileges, you cannot serve this file on the default web port, port 80. We can choose port 8888 as a regular user.
If you just want to serve this page one time to check how it renders, you can run the following command:
Now, in your browser, you can access the content by visiting:
This will serve the page, and then the netcat connection will close. If you attempt to refresh the page, it will be gone:
We can have netcat serve the page indefinitely by wrapping the last command in an infinite loop, like this:
This will allow it to continue to receive connections after the first connection closes.
We can stop the loop by typing CTRL-C on the server.
This allows you to see how a page renders in a browser, but it doesn’t provide much more functionality. You should never use this for serving actual websites. There is no security and simple things like links do not even work correctly.
Conclusion
You should now have a pretty good idea as to what netcat can be used for. It is a versatile tool that can be useful to diagnose problems and verify that base-level functionality is working correctly with TCP/UDP connections.
Using netcat, you can communicate between different computers very easily for quick interactions. Netcat attempts to make network interactions transparent between computers by taking the complexity out of forming connections.
Easily secure your cloud resources within private, isolated networks with DigitalOcean’s virtual private cloud (VPC). Create a VPC network in seconds.