EN
Bash - how to scan open ports with netcat / nc in Debian Linux?
3 points
Using Bash it is possible to scan open ports with netcat (nc
command) in Debian family Linux in the following way.
Quick solution:
xxxxxxxxxx
1
2
3
for (( i = 1; i < 65535; ++i ))
4
do
5
nc -z -w 1 "127.0.0.1" "$i" < /dev/null;
6
[ $? -eq 0 ] && echo "Open port $i";
7
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.
Simple steps:
1. create port_scanner.sh
script file:
xxxxxxxxxx
1
2
3
broken=0;
4
5
function break_script {
6
broken=1;
7
}
8
9
trap break_script SIGINT;
10
11
for (( i = 1; i < 65535; ++i ))
12
do
13
nc -z -w 1 "$1" "$i" < /dev/null;
14
[ $? -eq 0 ] && echo "Open port $i";
15
[ $broken -eq 1 ] && break;
16
done
2. assign permissions to execute the script with:
xxxxxxxxxx
1
chmod u+x port_scanner.sh
3. run port_scanner.sh
script with the following command:
xxxxxxxxxx
1
./port_scanner.sh 127.0.0.1
Where:
127.0.0.1
should be changed to the scanned computer IP address.
Example output:
xxxxxxxxxx
1
Open port 22
2
Open port 80
3
Open port 111
4
Open port 3306
5
Open port 8005
6
Open port 8080
Note: click
Ctrl
+C
to stop script.
Use the following commands to install nc
command:
xxxxxxxxxx
1
$ sudo apt-get update
2
$ sudo apt-get install netcat