fbpx

Bash Script to Check if Port is Open

When connecting to an Oracle database, it’s essential to ensure that the necessary network ports are open and accessible.

To establish a connection, it’s important to verify whether Port 1521 is open, as this default port is used for communication with the Oracle database.

Checking the port can help identify any network issues that may prevent the connection, such as firewalls or blocked ports.

To access the Oracle database without interruptions or security breaches, make sure the network connection is secure, and the port is open.

Here is a short script that checks if port 1521 is open or not.

#!/bin/bash

# Check if at least one IP address is provided
if [ $# -eq 0 ]; then
  echo "Usage: $0 <ip1> [ip2] [ip3] ..."
  exit 1
fi

# Port to check
port="1521"

# Timeout value in seconds
timeout_duration=1

# Loop through IPs provided as command-line arguments
for ip in "$@"; do
  echo "Checking port $port for IP: $ip"
  
  # Check if port is open using /dev/tcp and the timeout variable
  timeout "$timeout_duration" bash -c "echo >/dev/tcp/$ip/$port" 2>/dev/null
  
  # Check the exit status of the last command
  if [ $? -eq 0 ]; then
    echo "Port $port is open on $ip"
  else
    echo "Port $port is closed on $ip"
  fi
done

You give it execution permission.

chmod +x check_ports.sh

And you then execute the script by giving the IPs as parameters.

./check_ports.sh 192.168.1.1 192.168.1.2 192.168.1.3

Leave a Reply

Your email address will not be published. Required fields are marked *