USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Bash Server Ping Checking Script

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 11833
Location: Iowa

PostPosted: Fri Sep 25, 2024 2:23 pm    Post subject: Bash Server Ping Checking Script Reply with quote

Posted this on my blog yesterday, but thought i'd share it here too.
http://archlinux.me/crouse/2009/09/24/bash-server-ping-checking-script-written-by-dave-crouse/

I had alot of fun writing this little ping testing utility. It works/looks best if you have a color enabled terminal.
Could be easily modified to email you if the server failed. Anyway, just thought I’d share Smile

Code:
#!/bin/bash
# Bash Server Ping Checking Script-written by Dave Crouse
# 09-24-2009 Crouse
# Pretty simple, add servers/ip's/Ln (line numbers) in the format shown in the two column examples.
# Tested with over 50 servers, worked for what I needed, thought i'd share :) ~~Crouse.

# Ideas  --- ad another array for Headers. so Hn could print headers with bold/etc. Might be neat.
declare -a Sn # Server Name
declare -a Ip # IP address of server.
declare -a Ln # Line number to display on in Row1.
declare -a Sn2 # Server Name                                                                                                       
declare -a Ip2 # IP address of server.                                                                                             
declare -a Ln2 # Line number to display on in Row2.
############################################################################### Server List Row 1
###############################################################################
Sn[1]="usalug.org"
Ip[1]="67.15.6.98"
Ln[1]="6"

Sn[2]="archlinux.me"
Ip[2]="208.92.232.122"
Ln[2]="7"
###############################################################################
###############################################################################

############################################################################### Server List Row 2
###############################################################################
Sn2[1]="usalug.org"
Ip2[1]="67.15.6.98"
Ln2[1]="6"

Sn2[2]="archlinux.me"
Ip2[2]="208.92.232.122"
Ln2[2]="7"
################################################################################                                                   
################################################################################                                                   
tput civis # Hide the cursor
maxcount=${#Sn[*]}  #should report number of Servers listed in Sn variable
maxcount2=${#Sn2[*]}  #should report number of Servers listed in Sn2 varialbe
clear
#tput setf 2; #echo -e '\e[1;33m'
echo " "
tput setf 0;tput setb 2;
echo "       Bash Server Ping Checking Utility           version 2.0         Crouse.   "; #tput rmso;

tput setf 7;  tput setb 0; # List out servers now.
     for (( loop=1; $loop < $maxcount+1; loop++ ))
     do
        ( tput cup ${Ln[$loop]} 10; echo "${Sn[$loop]}"; tput rc)
     done
     for (( loop2=1; $loop2 < $maxcount2+1; loop2++ ))
     do
        ( tput cup ${Ln2[$loop2]} 50; echo "${Sn[$loop2]}"; tput rc)
     done
# Set traps so when we exit the big loop stuff is normal again.
gracefulexit () {
tput cnorm;
tput setb 0;
echo "Stopping Application"
tput setf 2;
exit
}
trap gracefulexit INT TERM EXIT
# Begin big loop
while true
do
    #First Row
    for (( loop=1; $loop < $maxcount+1; loop++ ))
    do
        (tput sc ;tput setb 0; tput setf 0; tput cup 4 10 ;
        echo "........................................................................";tput rc)
        (tput sc ;tput setb 0; tput setf 6; tput cup 4 10 ;
        echo "Checking ${Sn[$loop]} at IP: ${Ip[$loop]}" ; tput rc)
        (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln[$loop]} 1 ; echo "........." ; tput rc)
        (tput sc ;tput setb 0; tput setf 6; tput cup ${Ln[$loop]} 1 ; echo "TESTING" ; tput rc)
        count=$(ping -c 5 ${Ip[$loop]} | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
        (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln[$loop]} 1 ; echo "........." ; tput rc)
        if [[ "$count" == [45] ]]; then
            (tput sc ;tput setf 0; tput setb 2;tput cup ${Ln[$loop]} 5 ; echo " OK " ; tput rc)
        else
            if [[ "$count" == [123] ]]; then
                (tput sc ;tput setf 0; tput setb 6; tput cup ${Ln[$loop]} 4 ; echo "ALERT" ; tput rc)
        else       
                (tput sc ;tput setf 7; tput setb 4; tput cup ${Ln[$loop]} 2 ; echo "WARNING" ; tput rc)
            fi
        fi
    done
    #Second Row
    for (( loop2=1; $loop2 < $maxcount2+1; loop2++ ))
    do
        (tput sc ;tput setb 0; tput setf 0; tput cup 4 10 ;
        echo "........................................................................";tput rc)
        (tput sc ;tput setb 0; tput setf 6; tput cup 4 10 ;
        echo "Checking ${Sn2[$loop2]} at IP: ${Ip2[$loop2]}" ; tput rc)
        (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln2[$loop2]} 41 ; echo "........." ; tput rc)
        (tput sc ;tput setb 0; tput setf 6; tput cup ${Ln2[$loop2]} 41 ; echo "TESTING" ; tput rc)
        count2=$(ping -c 5 ${Ip2[$loop2]} | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
        (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln2[$loop2]} 41 ; echo "........." ; tput rc)
        if [[ "$count2" == [45] ]]; then
            (tput sc ;tput setf 0; tput setb 2;tput cup ${Ln2[$loop2]} 45 ; echo " OK " ; tput rc)
        else
            if [[ "$count2" == [123] ]]; then
                (tput sc ;tput setf 0; tput setb 6; tput cup ${Ln2[$loop2]} 44 ; echo "ALERT" ; tput rc)
        else
                (tput sc ;tput setf 7; tput setb 4; tput cup ${Ln2[$loop2]} 42 ; echo "WARNING" ; tput rc)
            fi
        fi
    done
done



_________________
Veronica - Arch Linux 64-bit -- Kernel 2.6.33.4-1
Archie/Jughead - Arch Linux 32-bit -- Kernel 2.6.33.4-1
Betty/Reggie - Arch Linux (VBox) 32-bit -- Kernel 2.6.33.4-1
BumbleBee - OpenSolaris-SunOS 5.11
Back to top
View user's profile Send private message Visit poster's website AIM Address
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Fri Sep 25, 2024 3:29 pm    Post subject: Reply with quote

Nice script, Dave!



_________________
Brian Masinick
Distros: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 11833
Location: Iowa

PostPosted: Sat Sep 26, 2024 9:27 pm    Post subject: Reply with quote

Thanks, it's actually pretty cool when you run it with about 20 servers to keep checking that they are alive. Smile



_________________
Veronica - Arch Linux 64-bit -- Kernel 2.6.33.4-1
Archie/Jughead - Arch Linux 32-bit -- Kernel 2.6.33.4-1
Betty/Reggie - Arch Linux (VBox) 32-bit -- Kernel 2.6.33.4-1
BumbleBee - OpenSolaris-SunOS 5.11
Back to top
View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse