#!/bin/bash
######################################################################
#
#   Bash script that pings a device
#    - arg1: IP address/hostname of the device
#
######################################################################

IP=$1
cnt=0
err=0
max_err=45 


while [ $cnt -lt 5 ]; do
    ping -c 1 -t 2 $IP 1> /dev/null
        
    # If ping successful print count
    if [ $? -eq 0 ]; then
        let cnt+=1
        echo -ne "\rPing sucessfull: $cnt"
    # If not print error count
    else
        let err+=1
        echo -ne "\r$err errors pinging   "
    fi 

    # If err greater then max, report and exit
    if [ $err -gt $max_err ]; then
        echo
        echo $IP not available after $err retries... 
        echo
        exit
    fi 
    sleep 0.4
done

# Exit when pinging suceeded
echo -e "\nPinging $IP succeded\n"

