Languages
[Edit]
EN

Bash - how to scan open ports with netcat / nc in Debian Linux?

3 points
Created by:
Marcin
2444

Using Bash it is possible to scan ports with netcat (nc command) in Debian family Linux in following way.

1. Netcat bash script example

port_scanner.sh script file:

#!/bin/bash

broken=0;

function break_script
{
	broken=1;
}

trap break_script SIGINT;

for (( i = 1; i < 65535; ++i ))
do
	nc -z -w 1 "$1" "$i" < /dev/null;

	[ $? -eq 0 ] && echo "Open port $i";
	[ $broken -eq 1 ] && break;
done

Running:

$ ./port_scanner.sh 127.0.0.1
Open port 22
Open port 80
Open port 111
Open port 3306
Open port 8005
Open port 8080

Note: if nc command does not exist use following commands:

$ sudo apt-get update
$ sudo apt-get install netcat
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.