Ncat - Testing Network Connections
Linux
    Network
  Ncat 1 …re-implementation of netcat:
- Swiss army knife for network debugging
- …reads and writes data across network connection
- …outbound and inbound connections
- …TCP/UDP to or from any ports …IPv4 & IPv6
- …tunneling …for ports/interfaces
- …build-in port scanner …for security auditing
# install required RPM package
sudo dnf install -y nmap-ncatGeneral syntax:
# attempt to initiate a TCP connection to the defined host on the port number 
ncat ${host:-localhost} ${port:-22}
# listen on port for incomming connections
ncat -v -l ${port:1234}
# …connection
ncat ${host:-localhost} ${port:-1234}Response
Command execution…
- --execcommand without shell interpreter
- --sh-execpass command to a system shell
- --lua-execrun a Lua program
# execute a command for response
ncat -l -p ${port:-1234} --send-only --exec "/bin/date"
# execute shell-script for response & keep connection alive
ncat -l --keep-open -p ${port:-1234} --sh-exec "echo -e hello from $(hostname -i)"Dummy HTTP Server…
cat > response.txt <<EOF
HTTP/1.0 200 OK
<html>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>
EOF
ncat -l -p 8080 < response.txtConnect mode vs listen mode:
- Listen mode …accept connection …run command …exit
- Connect mode …with option --keep-open- …accept multiple connections …one handler per connection
- …exit with ctrl+C
 
Receive
Print incoming data to stdout:
ncat -l -p ${port:-1234} --keep-open --verbose --recv-onlyData transfer…
# Write incomming data to a file
ncat -l -p ${port:-1234} > receive.txt
# Send data from stdin
echo Hello | ncat --send-only ${host:-localhost} ${port:-1234} 
# Send data from a file
echo "Hello from $(hostname -i)" > input.txt
ncat --send-only ${host:-localhost} ${port:-1234} < input.txtFootnotes
- Nact User Guide, Nmap Project 
 https://nmap.org/ncat/guide/index.html↩︎