How to Automate MySQL Port Checking Using Telnet in Shell Scripts


2 views

When automating server checks in shell scripts, verifying MySQL availability via port 3306 is a common requirement. The interactive nature of telnet makes automation tricky, as we need to handle both successful connections and timeouts programmatically.

Here's the simplest way to check port availability:

echo -e "\\x1dclose\\x1d" | telnet 10.10.10.24 3306 2>&1 | grep "Connected"

For reliable scripting, consider these approaches:

Timeout Handling

timeout 5 bash -c 'echo -e "\\x1dclose\\x1d" | telnet 10.10.10.24 3306 2>&1' | grep -q "Connected" && echo "MySQL UP" || echo "MySQL DOWN"

Complete Check Script

#!/bin/bash

HOST="10.10.10.24"
PORT=3306
TIMEOUT=5

(echo -e "\\x1dclose\\x1d"; sleep 1) | telnet $HOST $PORT 2>&1 | {
    IFS=' ' read -r -a array <<< "$(grep 'Connected\|refused\|No route')"
    case "${array[0]}" in
        "Connected")
            echo "SUCCESS: MySQL running on $HOST:$PORT"
            exit 0
            ;;
        "Connection")
            echo "ERROR: Connection refused - MySQL not running?"
            exit 1
            ;;
        *)
            echo "ERROR: Host unreachable or timeout"
            exit 2
            ;;
    esac
}

While telnet works, modern alternatives include:

# Using netcat
nc -zv 10.10.10.24 3306

# Using bash built-ins
timeout 5 bash -c '</dev/tcp/10.10.10.24/3306 && echo "Port open"' || echo "Port closed"

Consider these improvements:

  • Add maximum retry attempts
  • Implement exponential backoff
  • Log all connection attempts
  • Combine with MySQL client checks when port is open

When working with MySQL servers, checking port connectivity is a common task for database administrators and developers. The telnet command provides a simple way to verify if MySQL's default port (3306) is accessible:

telnet 10.10.10.24 3306
^] (Ctrl+] to exit)

The manual approach works fine for interactive sessions, but automation requires handling several technical aspects:

  • Non-interactive execution in scripts
  • Proper exit handling without user intervention
  • Return code interpretation
  • Timeout control

Here's a simple but effective implementation:

#!/bin/bash

SERVER="10.10.10.24"
PORT=3306
TIMEOUT=5

(echo > /dev/tcp/${SERVER}/${PORT}) >/dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "MySQL port ${PORT} on ${SERVER} is open"
else
    echo "MySQL port ${PORT} on ${SERVER} is closed"
fi

For environments where /dev/tcp isn't available, use this telnet-based approach:

#!/bin/bash

check_mysql_port() {
    local host=$1
    local port=$2
    local timeout=$3
    
    if ! command -v telnet &> /dev/null; then
        echo "telnet command not found" >&2
        return 127
    fi
    
    if timeout ${timeout} telnet ${host} ${port} &1 | \
       grep -q "Connected"; then
        return 0
    else
        return 1
    fi
}

# Usage example
if check_mysql_port "10.10.10.24" 3306 5; then
    echo "MySQL connection successful"
else
    echo "MySQL connection failed"
fi

While telnet works, modern systems often prefer these alternatives:

# Using netcat (nc)
nc -zv 10.10.10.24 3306

# Using nmap
nmap -p 3306 10.10.10.24

# Using specialized MySQL client
mysqladmin -h 10.10.10.24 -P 3306 ping

For production scripts, consider adding:

  • Proper logging
  • Retry mechanisms
  • Notification systems
  • Performance metrics
  • Security considerations (avoid hardcoding credentials)