Languages
[Edit]
EN

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

3 points
Created by:
Khloe-Kaiser
398

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

Quick solution:

#!/bin/bash

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

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

Hints:

  • 127.0.0.1 should be changed to the scanned computer IP address,
  • to install nc command got to the description in the last section.

 

1. Reusable script example

Simple steps:

1. create 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

2. assign permissions to execute the script with:

chmod u+x port_scanner.sh

3. run port_scanner.sh script with the following command:

./port_scanner.sh 127.0.0.1

Where:

  • 127.0.0.1 should be changed to the scanned computer IP address.

Example output: 

Open port 22
Open port 80
Open port 111
Open port 3306
Open port 8005
Open port 8080

Note: click Ctrl+C to stop script.

2. netcat installation

Use the following commands to install nc command:

$ sudo apt-get update
$ sudo apt-get install netcat

Bash

Bash - scan open ports with netcat / nc in Debian Linux
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join