Suppose I have three friends Rupesh Parag and Rizwan having their laptops then
the following script will help me to check whether they are online or offline and notify me if any change in the status
save this script as notification
and set cron to run this script every minute
#!/bin/bash
rupesh=192.168.100.102
parag=192.168.100.101
rizwan=192.168.100.110
rupeshstatus=/tmp/rupeshstatus
paragstatus=/tmp/paragstatus
rizwanstatus=/tmp/rizwanstatus
start_time=$(date +%s)
######### Checking for Rupesh ##########################3
if [[ ! -f $rupeshstatus ]];
then
echo offline > $rupeshstatus
fi
ping -c1 $rupesh
rustatus=`echo $?`
#echo sstatus is $rustatus
if [ "$rustatus" -eq 0 ];
then
rup=online
else
rup=offline
fi
#echo $rup
ru=`cat $rupeshstatus`
#echo $ru
#echo "$rup" -ne "$s"
if [[ "$rup" != "$ru" ]];
then
#xmessage -center "Rupesh $rup" -display :0
sshpass -p a ssh -o StrictHostKeyChecking=no akash.l@192.168.100.199 "xmessage -timeout 2 -c "Rupesh is $rup" -display :0 & "
echo $rup > $rupeshstatus
fi
if [[ ! -f $rupeshstatus ]];
then
echo offline > $rupeshstatus
fi
############ checking for Parag #############################################
if [[ ! -f $paragstatus ]];
then
echo offline > $paragstatus
fi
ping -c1 $parag
pstatus=`echo $?`
#echo sstatus is $pstatus
if [ "$pstatus" -eq 0 ];
then
prg=online
else
prg=offline
fi
#echo $prg
p=`cat $paragstatus`
#echo $v
#echo "$prg" -ne "$v"
if [[ "$prg" != "$p" ]];
then
#xmessage -center "Parag is $prg" -display :0
sshpass -p a ssh -o StrictHostKeyChecking=no akash.l@192.168.100.199 "xmessage -timeout 2 -c Parag is $prg -display :0 &"
echo $prg > $paragstatus
fi
######## checking for Rizwan ##########################
if [[ ! -f $rizwanstatus ]];
then
echo offline > $rizwanstatus
fi
ping -c1 $rizwan
ristatus=`echo $?`
#echo sstatus is $sstatus
if [ "$ristatus" -eq 0 ];
then
riz=online
else
riz=offline
fi
#echo $rup
ri=`cat $rizwanstatus`
#echo $s
#echo "$rup" -ne "$s"
if [[ "$riz" != "$ri" ]];
then
#xmessage -center "Rizwan is $riz" -display :0
sshpass -p a ssh -o StrictHostKeyChecking=no akash.l@192.168.100.199 "xmessage -timeout 2 -c Rizwan is $riz -display :0 &"
echo $riz > $rizwanstatus
fi
finish_time=$(date +%s)
echo "Time duration: $((finish_time - start_time)) secs."