Bash Scripts to Check if a Domain or URL is Ping-able or Accessible


bash-shellshock Bash Scripts to Check if a Domain or URL is Ping-able or Accessible bash script domain linux shell network

bash-shellshock

How to Check if a Domain can be Ping-able?

Creating a bash script to check if a domain is reachable via ping can be very useful for basic network troubleshooting. The script will attempt to ping the domain, and if it fails, it will retry up to 3 times before reporting that the domain is not reachable. Here’s a sample script you can use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
 
# Check if a domain name was provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 domain_name"
    exit 1
fi
 
DOMAIN=$1
RETRIES=3
DELAY=5 # Time in seconds between retries
 
for ((i=1; i<=RETRIES; i++)); do
    echo "Pinging $DOMAIN. Attempt $i of $RETRIES"
    
    # Ping the domain with a timeout of 5 seconds
    if ping -c 1 -W 5 $DOMAIN > /dev/null 2>&1; then
        echo "$DOMAIN is reachable."
        exit 0
    else
        echo "$DOMAIN is not reachable. Retrying in $DELAY seconds..."
        sleep $DELAY
    fi
done
 
echo "After $RETRIES attempts, $DOMAIN is not reachable."
exit 1
#!/bin/bash

# Check if a domain name was provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 domain_name"
    exit 1
fi

DOMAIN=$1
RETRIES=3
DELAY=5 # Time in seconds between retries

for ((i=1; i<=RETRIES; i++)); do
    echo "Pinging $DOMAIN. Attempt $i of $RETRIES"
    
    # Ping the domain with a timeout of 5 seconds
    if ping -c 1 -W 5 $DOMAIN > /dev/null 2>&1; then
        echo "$DOMAIN is reachable."
        exit 0
    else
        echo "$DOMAIN is not reachable. Retrying in $DELAY seconds..."
        sleep $DELAY
    fi
done

echo "After $RETRIES attempts, $DOMAIN is not reachable."
exit 1

Save this script to a file, for instance, ping_test.sh, and don’t forget to make it executable using the command:

1
chmod +x ping_test.sh
chmod +x ping_test.sh

You can run the script followed by the domain you want to check, like so:

1
./ping_test.sh example.com
./ping_test.sh example.com

This script pings the domain and if it fails, waits for a specified delay before retrying, up to a maximum number of retries. If all attempts fail, it prints a failure message and exits with a non-zero status code. Remember that you might need superuser permissions to execute ping on some systems.

How to Check if a URL is accessible?

If you want to check the availability of a full URL instead of just a domain, using ping won’t work because ping works with IP addresses or domain names only, not URLs. You need a different tool that can handle HTTP requests. For this purpose, you can use curl, which is a powerful command-line tool used to transfer data from or to a server. Here’s how you could adjust the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
 
# Check if a URL was provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 url"
    exit 1
fi
 
URL=$1
RETRIES=3
DELAY=5 # Time in seconds between retries
 
for ((i=1; i<=RETRIES; i++)); do
    echo "Checking $URL. Attempt $i of $RETRIES"
 
    # Use curl to check if the URL is reachable. 
    # -s for silent mode (don't show progress)
    # -o for redirecting the output to /dev/null (we don't care about the output here, just the status)
    # -w for printing the HTTP code
    # --connect-timeout for the maximum time in seconds that you allow the connection to the server to take
    HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 $URL)
 
    if [ $HTTP_CODE -ge 200 ] && [ $HTTP_CODE -lt 400 ]; then
        echo "$URL is reachable."
        exit 0
    else
        echo "$URL is not reachable (HTTP status: $HTTP_CODE). Retrying in $DELAY seconds..."
        sleep $DELAY
    fi
done
 
echo "After $RETRIES attempts, $URL is not reachable."
exit 1
#!/bin/bash

# Check if a URL was provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 url"
    exit 1
fi

URL=$1
RETRIES=3
DELAY=5 # Time in seconds between retries

for ((i=1; i<=RETRIES; i++)); do
    echo "Checking $URL. Attempt $i of $RETRIES"

    # Use curl to check if the URL is reachable. 
    # -s for silent mode (don't show progress)
    # -o for redirecting the output to /dev/null (we don't care about the output here, just the status)
    # -w for printing the HTTP code
    # --connect-timeout for the maximum time in seconds that you allow the connection to the server to take
    HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 $URL)

    if [ $HTTP_CODE -ge 200 ] && [ $HTTP_CODE -lt 400 ]; then
        echo "$URL is reachable."
        exit 0
    else
        echo "$URL is not reachable (HTTP status: $HTTP_CODE). Retrying in $DELAY seconds..."
        sleep $DELAY
    fi
done

echo "After $RETRIES attempts, $URL is not reachable."
exit 1

Make sure you have curl installed on your system to use this script. You can usually install curl using your system's package manager. For instance, on Ubuntu, you would use sudo apt-get install curl.

Save this script to a file, like url_test.sh, and make it executable:

1
chmod +x url_test.sh
chmod +x url_test.sh

You can run the script followed by the URL you want to check:

1
./url_test.sh https://example.com:8080
./url_test.sh https://example.com:8080

This script sends an HTTP request to the given URL and checks the HTTP status code returned by the server. If the status code indicates success (2xx and 3xx codes), the script reports the URL as reachable. Otherwise, it waits for a delay and retries, up to a maximum number of attempts. If all attempts fail, it reports the URL as unreachable and exits with a non-zero status code.

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
792 words
Last Post: What are the Differences between uBPF and eBPF?
Next Post: How to Kill a Process That Opens a TCP/UDP Port?

The Permanent URL is: Bash Scripts to Check if a Domain or URL is Ping-able or Accessible

Leave a Reply