Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»Check Network Connectivity using Ping Command in Shell Scripts

    Check Network Connectivity using Ping Command in Shell Scripts

    By RahulApril 23, 20232 Mins Read

    Understanding how to assess network connectivity is crucial for troubleshooting and monitoring purposes. The ping command is a popular tool used to test the connectivity between two devices on a network. In this article, we will explore how to use conditional bash scripting to ping IP addresses and check the status of network connections using if-else statements.

    Advertisement

    1. Basics of the ping command

    The ping command sends Internet Control Message Protocol (ICMP) echo request packets to a specified IP address or hostname, waiting for an ICMP echo reply. It measures the round-trip time between the source device and the target device and provides valuable information about the network connection, such as packet loss and latency.

    The syntax for the ping command is:

    1
    ping [options] [IP_address_or_hostname]

    2. Creating a bash script for conditional ping checks

    To create a simple bash script that pings an IP address and checks the status using if-else statements, follow these steps:

    Step 1: Open a text editor and create a new file called `ping_check.sh`.

    Step 2: Insert the following code into the file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #!/bin/bash
     
    IP="8.8.8.8" # Replace with the IP address you want to ping
    COUNT=1 # Number of ping attempts
     
    if ping -c $COUNT $IP > /dev/null 2>&1; then
      echo "Ping to $IP was successful."
    else
      echo "Ping to $IP failed."
    fi

    Step 3: Replace 8.8.8.8 with the IP address you want to ping, and 1 with the number of ping attempts you want to perform.

    Step 4: Save the file and close the text editor.

    Step 5: Make the script executable using the following command:

    chmod +x ping_check.sh 
    

    Step 6: Run the script:

    ./ping_check.sh 
    

    The script will ping the specified IP address, and depending on the result, it will print either “Ping to IP was successful” or “Ping to IP failed”.

    3. Customizing the script for multiple IP addresses

    If you want to test multiple IP addresses, you can modify the script to include an array of IP addresses and loop through them. Here’s an example of how to do this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #!/bin/bash
     
    IP_ADDRESSES=("8.8.8.8" "8.8.4.4" "208.67.222.222") # Replace with your desired IP addresses
    COUNT=1 # Number of ping attempts
     
    for IP in "${IP_ADDRESSES[@]}"; do
      if ping -c $COUNT $IP > /dev/null 2>&1; then
        echo "Ping to $IP was successful."
      else
        echo "Ping to $IP failed."
      fi
    done

    Conclusion

    Using conditional bash scripting with the ping command is a powerful way to assess network connectivity and troubleshoot potential issues. By incorporating if-else statements into your scripts, you can automate the process of pinging multiple IP addresses, determine the status of network connections, and generate useful reports on your system’s network performance.

    command ping
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    free Command in Linux (Check Memory Uses)

    A Practical Guide to Extracting Compressed Files in Linux

    TR Command in Linux: A Guide with Practical Examples

    TR Command in Linux: A Guide with Practical Examples

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Git Switch vs. Checkout: A Detailed Comparison with Examples
    • How To Block Specific Keywords Using Squid Proxy Server
    • How To Block Specific Domains Using Squid Proxy Server
    • A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)
    • Understanding Basic Git Workflow: Add, Commit, Push
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.