How to chat and transfer files with users on the same network with linux terminal
Linux is amazing if you are a networking Geek and here are some few tips on Linux Terminal that will help to daily life.
Well you need to install netcat ?
What is Netcat?
nc, also known as the TCP/IP swiss army knife is a feature rich network utility which can be used to read and write data to network connections using TCP or UDP.
What can you do with netcat?
According to the description in the manual page, the nc tool can be used about anything under the sun involving TCP or UDP. You can use it to do port scanning, transfer files, create a listener or stream media. Is this all you can do with netcat? No, but these are the things you guys will learn from our tutorial series for netcat.
In this article, I will explain how to install netcat in your linux machine and after the installation is completed you will build a simple chat.
How To Install netcat In Ubuntu Linux
sudo apt-get install netcat
To create a simple chat we need two instances of netcat, one to listen for incoming connections (the server) and another one to start the connection.
1. Run netcat with the ‘-l’ option in order to operate in listening mode. You should also specify the listening port, I prefer 1300.
nc -l -p 1300
Then, run another ‘netcat’ which will initiate the connection by connecting to the server.
nc localhost 1300
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:
netcat -l 4444 > received_file
On the second computer, create a simple text file by typing:
netcat domain.com 4444 < original_file
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:
cat received_file Hello, 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:
netcat -l 4444 | tar xzvf -
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:
tar -czf - * | netcat domain.com 4444
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
A Bonus for the Developers “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:
nano index.html
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 one page, one time to check how it renders, you can type something like this:
netcat -l 8888 < index.html
Now, in your browser, you can access the content by visiting:
http://server_IP:8888
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:
while true; do nc -l 8888 < index.html; done
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.
Please take some time to comment on this article this will help us to improve our articles.
No comments:
Post a Comment