0
0
Bash Scriptingscripting~5 mins

Port scanning basics in Bash Scripting

Choose your learning style9 modes available
Introduction

Port scanning helps you find open doors (ports) on a computer. This shows which services are ready to talk.

Checking if a web server is running on a computer.
Finding open ports before connecting to a remote device.
Testing your own computer's security by seeing which ports are open.
Learning how network services work by exploring ports.
Troubleshooting network problems by checking port availability.
Syntax
Bash Scripting
for port in {start_port..end_port}; do
  nc -zv target_ip $port
  sleep 0.1
 done

nc is a tool called Netcat used to check ports.

-z means scan without sending data, -v means verbose (show details).

Examples
Check if port 80 (usually web) is open on 192.168.1.10.
Bash Scripting
nc -zv 192.168.1.10 80
Scan ports 20 to 25 on 192.168.1.10 one by one.
Bash Scripting
for port in {20..25}; do nc -zv 192.168.1.10 $port; done
Check if port 443 (secure web) is open on google.com.
Bash Scripting
nc -zv google.com 443
Sample Program

This script scans ports 80, 81, and 82 on your own computer. It shows only open ports.

Bash Scripting
#!/bin/bash
# Simple port scanner for ports 80 to 82 on localhost
for port in {80..82}; do
  nc -zv 127.0.0.1 $port 2>&1 | grep -E 'succeeded|open'
done
OutputSuccess
Important Notes

Some ports may be filtered by firewalls and show no response.

Running port scans on computers you don't own may be illegal or unwanted.

Netcat must be installed on your system to use nc.

Summary

Port scanning finds open network ports on a computer.

Use nc -zv to check if a port is open.

Scan a range of ports with a simple loop in bash.