Languages
[Edit]
EN

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

3 points
Created by:
Marcin
15680

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
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join