7 Fundamental Use Cases of Netcat

A guide to Netcat — TCP/IP Swiss Army knife

Sahitya Maruvada
Jun 16, 2020 · 5 min read
Image for post
Image for post
Photo by NASA on Unsplash

Introduction:

Installation:

apt-get install netcat

Use Cases:

# nc -zn <test_ip_address> <port/s or port-range>
$ nc -zn 192.168.1.148 8080
(UNKNOWN) [192.168.1.148] 8080 (?) open
nc -l -p 2002 > file.txt
nc receiver_host.com 2002 < file.txt
while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; sh test.sh;  } | nc -l 8080; done# test.sh
echo "Output of test Netcat server"
# host_1
nc -l -v -p 2000
# host_2
nc <ip_address_host_1> 2000
printf "GET /index.html HTTP/1.1\r\nUser-Agent: netcat/0.0.1\r\nHost: <Server_IP_Address>\r\nAccept: */*\r\n\r\n" | nc <Server_IP_Address> 80
printf "GET / HTTP/1.0\r\n\r\n" | nc <Server_IP_Address> 80
# Creating a named pipe
mknod mypipe p
# Listener-to-Listener Relay
nc -l -p 2222 0<mypipe | nc -l -p my_test_host.com 443 | tee mypipe
# Listener to Client Relay
nc -l -p 8080 0<mypipe | nc my_test_host.com 80 | tee mypipe
nc host_1 port_1 0<mypipe | nc host_2 port_2 | tee mypipe
# running a shell command
nc -l -p <port_number> -c 'echo $(pwd)'
# running a script
nc -l -p <port_number> -e '/usr/local/bin/my_scrpt'

Final Words:

100 Days of Linux

All about exploring Cool Linux features!!

Sahitya Maruvada

Written by

Tech and Travel Enthusiast!! Software Engineer by profession!! Writing on Medium is my way of giving back to the dev community 😃

100 Days of Linux

Linux is a ubiquitous Operating System. It has so many useful features. This publication is all about Linux and its tools. It will also include articles about Linux Networking Stack.

Sahitya Maruvada

Written by

Tech and Travel Enthusiast!! Software Engineer by profession!! Writing on Medium is my way of giving back to the dev community 😃

100 Days of Linux

Linux is a ubiquitous Operating System. It has so many useful features. This publication is all about Linux and its tools. It will also include articles about Linux Networking Stack.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store