A simple bash script to check if specific ports are open on a host.
This script checks the status of specified ports on a host (defaulting to localhost) to determine if they are open or closed. It uses the nc (netcat) command to perform the checks.
-
Download the Script:
Save the script to a file, e.g.,
check_ports.sh. -
Make the Script Executable:
chmod +x check_ports.sh
-
Run the Script:
./check_ports.sh
This function checks if a specific port is open on a specified host.
-
Parameters:
$1: The port number to check.$2(optional): The host to check (defaults tolocalhost).
-
Example:
check_port 80 check_port 80 example.com
The script includes a predefined list of ports to check:
ports=(80 443 22 53)You can modify this list to include any ports you want to check.
Port 80 is open on localhost!
Port 443 is open on localhost!
Port 22 is open on localhost!
Port 53 is open on localhost!
-
Change Ports:
Edit the
portsarray to include the ports you want to check:ports=(80 443 22 53 8080)
-
Change Host:
To check ports on a different host, modify the script to pass the host as a second argument:
./check_ports.sh example.com
-
nc(netcat): Ensurencis installed on your system. You can install it using your package manager, for example:-
On Debian/Ubuntu:
sudo apt-get install netcat
-
On CentOS/RHEL:
sudo yum install nc
-
On macOS:
brew install netcat
-
ns or suggestions!