Advertisement

Shell Script For Network Connectivity Checker Script

Linux Shell Script For Network Connectivity Checker Script 

Network Connectivity Checker

#!/bin/bash
host="example.com"
# Check network connectivity by pinging a host
if ping -c 1 "$host" &>/dev/null; then
echo "Network is up."
else
echo "Network is down."
fi

Shell script to check network connectivity can be quite useful for troubleshooting or ensuring systems have internet access before proceeding with certain operations.

#!/bin/bash

# Function to check internet connectivity
check_internet() {
    wget -q --spider http://example.com

    if [ $? -eq 0 ]; then
        echo "Internet connection is active."
    else
        echo "Internet connection is not active."
    fi
}

# Function to check connectivity to a specific host
check_host() {
    HOST=$1
    ping -c 1 $HOST > /dev/null 2>&1

    if [ $? -eq 0 ]; then
        echo "Connectivity to $HOST is active."
    else
        echo "Cannot reach $HOST."
    fi
}

# Check internet connectivity
echo "Checking internet connectivity..."
check_internet

# Check connectivity to a specific host (e.g., google.com)
echo
echo "Checking connectivity to google.com..."
check_host google.com

# Check connectivity to another specific host (e.g., github.com)
echo
echo "Checking connectivity to github.com..."
check_host github.com

# Add more checks as needed

echo "Network connectivity check complete."

Explanation:

  1. Shebang: #!/bin/bash specifies that this script should be interpreted using the Bash shell.

  2. check_internet Function: Uses wget --spider to check connectivity to a known URL (http://example.com in this case). If the command returns 0 ($? holds the exit status of the last command), it indicates internet connectivity.

  3. check_host Function: Uses ping to check connectivity to a specified host. -c 1 instructs ping to send only one ICMP echo request. If the command returns 0, connectivity to the host is confirmed.

  4. Execution: The script sequentially checks internet connectivity and connectivity to specific hosts (google.com, github.com in this example). You can add more checks by calling check_host with different hostnames.

Usage:

  • Save this script to a file, e.g., check_network.sh.
  • Make it executable with chmod +x check_network.sh.
  • Run it with ./check_network.sh.

Notes:

  • Customization: Modify the URLs or hostnames in the check_internet and check_host functions to suit your environment or specific requirements.

  • Error Handling: The script assumes basic error handling. You may want to enhance it with more robust error checking and reporting based on your use case.

  • Security: Avoid relying solely on external URLs or hostnames that may change. Use reliable endpoints or local network resources for more accurate checks.

This script provides a foundation for checking basic network connectivity in a Linux environment. Depending on your needs, you can expand it to include more sophisticated checks, logging, or integration with other monitoring tools.


Post a Comment

0 Comments