Difference between revisions of "Linux netcat"

From Ever changing code
Jump to navigation Jump to search
(Created page with "'''Netcat''' in slow translation is the network version of '''cat''' command. Start listening for a connection. The command below listen on TCP port 4444 for a new connection...")
 
Line 1: Line 1:
'''Netcat''' in slow translation is the network version of '''cat''' command.
'''Netcat''' in slow translation is the network version of '''cat''' command.
== Send a string over a network ==


Start listening for a connection. The command below listen on TCP port 4444 for a new connections and displays any incoming data to a screen (default STDOUT) then it stops when  the connection closes. Use <code>-k</code> to continue listening after a transfer completes.
Start listening for a connection. The command below listen on TCP port 4444 for a new connections and displays any incoming data to a screen (default STDOUT) then it stops when  the connection closes. Use <code>-k</code> to continue listening after a transfer completes.
Line 15: Line 17:
  listen@server1# nc -kl 80 < index.html
  listen@server1# nc -kl 80 < index.html


cat index.html
This is a simple index.html file
<html>
<syntaxhighlight lang="bash" line>
        <head>
cat index.html
                <title>Test Page</title>
<html>
        </head>
        <head>
        <body>
                <title>Test Page</title>
                <h1>Level 1 header</h1>
        </head>
                <h2>Subheading</h2>
        <body>
                <p>Normal text here</p>
                <h1>Level 1 header</h1>
        </body>
                <h2>Subheading</h2>
</html>
                <p>Normal text here</p>
        </body>
</html>
</syntaxhighlight>


Please be aware that <tt>nc</tt> is not aware of HTTP1.1 specification and is not sending 200 Ok, therefore it will not work out of box in curl, wget or lynx. Therefore please try this in a full flagged web browser like Chrome.
Please be aware that <tt>nc</tt> is not aware of HTTP1.1 specification and is not sending 200 Ok, therefore it will not work out of box in curl, wget or lynx. Therefore please try this in a full flagged web browser like Chrome.

Revision as of 09:54, 29 September 2016

Netcat in slow translation is the network version of cat command.

Send a string over a network

Start listening for a connection. The command below listen on TCP port 4444 for a new connections and displays any incoming data to a screen (default STDOUT) then it stops when the connection closes. Use -k to continue listening after a transfer completes.

listen@server1# nc -l 4444 

Send string to the listening server by redirecting the string(data) into nc

send@server2# echo "Hello Tom!" | nc server1.example.com 4444

Send Files through Netcat

This example will redirect any incoming data to a file

listen@server1# nc -l 4444 > file1.txt

Simple web server

This creates a simple web server

listen@server1# nc -kl 80 < index.html

This is a simple index.html file

cat index.html
<html>
        <head>
                <title>Test Page</title>
        </head>
        <body>
                <h1>Level 1 header</h1>
                <h2>Subheading</h2>
                <p>Normal text here</p>
        </body>
</html>

Please be aware that nc is not aware of HTTP1.1 specification and is not sending 200 Ok, therefore it will not work out of box in curl, wget or lynx. Therefore please try this in a full flagged web browser like Chrome.

Connect using a web browser

client@server2 lynx http://server1.example.com:80