netcat
is a very useful tool for testing/debugging TCP/IP and UDP networking. In this cheetsheet I am sharing some useful examples of netcat usage for everyday purpose.
To check if a TCP port is open
nc -v google.com 80
# Connection to google.com port 80 [tcp/http] succeeded!
To check if a UDP port is open, simple add option -u
nc -vu google.com 53
# Connection to google.com port 53 [udp/domain] succeeded!
Scan UDP ports
nc -vzu google.com 1-65535
To scan TCP ports simply remove the -u
nc -vz google.com 1-65535
Open a server that listens to a particular port
nc -l 2389
Open another client connecting to that port
nc localhost 2389
Now you can (insecurely) chat between the 2 terminals.
On the remote server, open a port which output anything it receives to the target file
nc -l 2389 > test
On the local host, send the file
cat testfile | nc remotehost 2389
On receiver host
nc -l 5000 | tar xvf -
On sender host
tar cvf - /path/to/dir | nc remotehost.com 5000
On receiver host
nc -lp 5000 | sudo dd of=/backup/sdb.img.gz
On sender host
dd if=/dev/sdb | gzip -c | nc remote_server.com 5000
The following commands redirect all incoming TCP/5000 connections to http://www.google.com
mkfifo proxypipe
while true; do nc -l 5000 0<proxypipe | nc www.google.com 80 1> proxypipe; done
On remote host
nc -lp 5000 -e /bin/bash
On localhost host
nc remotehost 5000