Jump to content

Shell script for checking "is server online?" with dependant action


Recommended Posts

Guest cmonty
Posted

Hello!

 

I'm asking for your support to create the correct syntax of a bash script that should provide the following function:

  • every 30s it should check if a server (identified by its IP) is online
  • starting with the first check call a information box should be displayed with a countdown
  • this online-check should be repeated x times
  • if the server is offline after x checks, then a corresponding error message should be displayed
  • if the server is online after x-1 checks, there should be a specific action: opening Remmina and synchronizing a server-directory with local directory

 

 

I have "build" the following bash script, but I'm not sure if it will work as intended:

Code:

 

#!/bin/bash

#

#set -x

 

. /etc/vpn-connection.conf

 

logit() {

logger -p local0.notice -s -- VPN-Connect: $*

}

 

StartRemmina() {

remmina -c /home/${SYNC_USER}/.remmina/${REMMINA_CONFIG}

logit Remmina started with config file

}

 

SyncDirectory() {

rsync --delete \

-avzbe ssh ${SYNC_USER}${REMOTE_SERVER}:${SOURCE_DIR}/ ${DESTINATION_DIR}/ \

--backup-dir=${BACKUP_DIR}

logit Sync of directory Steuer-Sparbuch executed

}

 

DisplayMessage() {

COUNT=CHECK_REPEAT*CHECK_WAITTIME

START=${COUNT} # Set a start point

 

until [ "${COUNT}" -eq "0" ]; do # Countdown loop.

((COUNT-=1)) # Decrement seconds.

PERCENT=$((100-100*COUNT/START)) # Calc percentage.

echo "#Verbleibende Zeit$(echo "obase=60;$COUNT" | bc)" # Convert to H:M:S.

echo ${PERCENT} # Outut for progbar.

sleep 1

done | zenity --title "Countdown Start Remote-Server" \

--progress \

--percentage=0 \

--text=""\

--window-icon=${ICON} \

--auto-close # Progbar/time left.

if [ $? = 1 ]; then

exit $?;

fi

notify-send -i $ICON "Countdown > ## Zeit ist abgelaufen ##" # Attention finish!

#/usr/bin/canberra-gtk-play --volume 4 -f ${SOUND} # Ding-dong finish!

zenity --notification \

--window-icon="${ICON}" \

--text "Countdown > ## Zeit ist abgelaufen ##" # Indicate finished!

}

 

# Checking if Remote-Server is online

repeat=${CHECK_REPEAT}

 

while :; do

# If online then start Remmina and sync directory

nc -4nz -w 1 ${1:-192.168.178.10} ${2:-22} && \

SyncDirectory && \

sleep 2 && \

StartRemmina

DisplayMessage

repeat=$((repeat - 1))

test $repeat -gt 0 || logit Remote-Server is offline && exit

sleep ${CHECK_WAITTIME}

done

I would appreciate your input to complete this script.

 

THX

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...