Realtime monitor your friends are online / offline status in office (notification)

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."


 

Image to pdf

#!/bin/bash
 #tiff-to-pdf is 6 to 7 times faster script than this script
 start_time=`date +%s`
 ########## Section 1 ##########
 # This command can convert any image to PDF document, version 1.3 ( very slow as compared to tiff2pdf ) but slightly faster than section 2

mogrify -format pdf *

######### Section 2 ##########
 # this command can convert any image to PDF document, version 1.3

#ls  | while read filename; do
 #file=`echo $filename |sed 's/\(.*\)\..*/\1/'`
 #convert $filename $file.pdf
 #done

finish_time=`date +%s`

echo "Time duration: `expr $finish_time - $start_time` secs."

Check up/down hosts in your network

To check how many systems are up and how many systems are down in network.use below script

I used nohup to avoid ping output on screen

for i in {1..254} ; do ip=192.168.0.$i && nohup ping -c1 $ip 2> /dev/null && echo "$ip online" || echo "$ip offline" ; done

tiff to pdf

#!/bin/bash
# this file converts tif image to pdf file in present directory and all subdirectoris.
start_time=$(date +%s)

find . -type f -iname "*.tif*" | while read tif; do
 pdf=`echo $tif |sed 's/\(.*\)\..*/\1/'`
 if [[ ! -f "$pdf.pdf" ]];
 then
 tiff2pdf -o "$pdf.pdf" "$tif"
 else
   tstamp=`date +%N`
   pdf="$pdf-n$tstamp"
   if [[ ! -f "$pdf.pdf" ]];
   then
   tiff2pdf -o "$pdf.pdf" "$tif"
   else 
   tstamp=`date +%S`
   pdf="$pdf-n$tstamp"
   tiff2pdf -o "$pdf.pdf" "$tif"
   fi
fi
done
finish_time=$(date +%s)
echo done
echo "Time duration: $((finish_time - start_time)) secs."

Compress pdf files

 

#!/bin/bash
# For this script ghostscript package is needed. either install it or download static compiled binary for it.
echo "This script will Compress all pdf files with prefix Compressed-"
echo "do you wish to run the script (y/N)"
read ans
if [[ $ans == y ]] || [[ $ans == Y ]]
then
for i in *.pdf ;do
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=Compressed-$i $i
done
else
echo Exiting..
fi

 

Disk Check with Alert

This script checks the disk usage of servers and emails. sshpass is used only for pfsense for the rest servers ssh keys are used to connect.

#!/bin/bash
 DATE=`date +%Y-%m-%d`
 logfolder=/var/log/disk_usage/
 logfile=$logfolder/$DATE-disk-usage.log
 alertlogfile=$logfolder/$DATE-disk-usage-alert.log
 key=/example/keys/id_rsa_nagios
 sendemail=/example/scripts/sendEmail.pl
 SENDEMAIL=/example/scripts/sendEmail.pl
 MAIL_FROM="My Domain <automation@example.com>"
 MAIL_TO="admin@example.com"
 MAIL_HOST='example.com:587'
 MAILUSER=emailuser@example.com
 MAILPASS='emailuserpass'
 subject=$DATE-disk-usage-alert
 if [ ! -d $logfolder ]
 then
 mkdir -p $logfolder
 fi
 #addspace=`echo "" >> $logfile`
 ALERT=80
 IP=(192.168.0.10 192.168.0.20 192.168.0.30 192.168.0.40 192.168.0.50 192.168.0.60 192.168.0.10 192.168.0.70 192.168.0.10 192.168.0.80) # my server ips

function alert
 {
 grep -vE '^Filesystem|tmpfs|cdrom|devfs' | awk '{ print $5 " " $1 " " $6 }' | while read output;
 do
 #    echo output is $output for $Host
 usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f 1 )
 partition=$(echo $output | awk '{ print $2 }' )
 usem=$(echo $output | awk '{print $3}')
 # echo usep=$usep  usem=$usem partiton=$partition
 if [ $usep -ge $ALERT ]; then
 #echo "Running out of space \"$partition ($usep%)\" on $Host as on $DATE" |
 #mail -s "Alert: Almost out of disk space $usep" $ADMIN
 echo "Running out of space  \"$partition ($usep%) $usem\""  >> $alertlogfile
 fi
 done

}

## only for pfsense
 #:'
 echo "" >> $logfile
 Host=`sshpass -p PASSWD ssh -o StrictHostKeyChecking=no  -o 'KbdInteractiveDevices no'  root@192.168.0.1 hostname`
 echo "Hostname is $Host"
 echo "            $Host" >> $logfile
 echo "" >> $logfile
 sshpass -p PASSWD ssh -o StrictHostKeyChecking=no -o 'KbdInteractiveDevices no'  root@192.168.0.1 df -P -h >> $logfile
 sshpass -p PASSWD ssh -o StrictHostKeyChecking=no -o 'KbdInteractiveDevices no' root@192.168.0.1 df -P -h | alert
 echo "-----------------------------------------------------------------------" >> $logfile
 #'

#exit

function disk-check_local
 {
 ip=$1
 echo $ip
 echo "" >> $logfile
 Host=`ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip hostname`
 echo "Hostname is $Host"
 echo "            $Host" >> $logfile
 echo "            $Host" >> $alertlogfile
 echo "" >> $logfile
 echo "" >> $alertlogfile
 ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip df -P -h >> $logfile
 ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip df -P -h | alert
 echo "-----------------------------------------------------------------------" >> $logfile
 echo "-----------------------------------------------------------------------" >> $alertlogfile
 }

for i in ${IP[@]}; do
 echo "processing $ip"
 disk-check_local $i
 #  echo "done with $ip"
 done

#exit
 cat $logfile
 cat $alertlogfile
 report=`cat $alertlogfile`

$SENDEMAIL -f "$MAIL_FROM" -t $MAIL_TO -s $MAIL_HOST -xu $MAILUSER -xp $MAILPASS -u "$subject" -m "$report" -a $logfile

Monitor Server Status and Notify when up/down

#!/bin/bash
## Global variables
TO=admin@example.com
CC=admin@example.com
FROM="Your Server Remote Monitor <automation@example.com>"
user=gmailuser
pass=gmailuserpass
SMTP=smtp.gmail.com:587
nitstatus=/tmp/nit

###### Check line up or down 
ping -c3 example.com ; [ $? == 0 ] && checkstatus=up || status is checkstatus=down ; clear ; echo status is  $checkstatus |tail -1

if [[  ! -f $nitstatus ]];
then
echo up > $nitstatus
fi
oldstatus=`cat /tmp/nit`
echo old status is $oldstatus
echo $checkstatus
if [[ $checkstatus == "up" ]]
then
newstatus=up
elif [[ $checkstatus == "down" ]]
then
newstatus=down
fi
###### Email notification ###

echo new status is $newstatus

if
[[ $oldstatus != $newstatus ]]
then

echo "Your Server server is $newstatus"
echo $newstatus > $nitstatus
                  if [[ $newstatus == "down" ]]
                  then
                  echo sending email for down
                  /neuralit/scripts/sendEmail.pl -f "$FROM" -t "$TO"  -s  "$SMTP" -xu "$user" -xp "$pass"  -u "Your Server Server is Down" -m "
Your Server server is down on `date +%T`"
                  elif [[ $newstatus == "up" ]]
                  then 
                  echo sending email for up
                  /neuralit/scripts/sendEmail.pl -f "$FROM" -t "$TO"  -s  "$SMTP" -xu "$user" -xp "$pass"  -u "Your Server Server is Up" -m "
Your Server server is up on `date +%T`" 
                  fi

else
echo no change
fi

Get inventory in the lan

This script needs whoe network pcs having linux os, I have used sshpass for understanding you can use ssh keys also. This is written for 3  seperate networks in lan ( 192.168.10.0/24 192.168.100.0/24 and 192.168.20.0/24)

The script will provide the username ip address mac address memory cpu info (* note my user having . in username so i wrote accordingly. you may have different and so you have to edit the script according to you.




#!/bin/bash
 downhosts=/var/log/hostsdetails/down
 uphosts=/var/log/hostsdetails/up
 inventory=/var/log/hostsdetails/inventory
 old=/var/log/hostsdetails/old/
 pass=PASSWORD
 net=(10 100 20)
 hour=0
 start_time=$(date +%s)

mv "$downhosts" "$old"
 mv "$uphosts" "$old"
 mv "$inventory" "$old"
 echo "USERNAME <-> IP-ADDRESS <-> MAC-ADDRESS <-> MEMORY <->  CPU-INFORMATION" >> $inventory

for i in ${net[@]}; do
 echo  "for '$i'th network"
 for j in $(seq 1 254); do ip=192.168.$i.$j && ping -c 1 $ip>/dev/null; [ $? -eq 0 ] && echo "$ip is UP" && mac=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" ifconfig | grep eth0 | awk '{ print $5}'` && mem=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip"  free -m | grep Mem | awk '{print $2}'`&& user=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" ls -l /proc /| awk '{print $3}'| grep "\." | uniq` && if [ "$user" == "" ]; then  echo Unable to get the logged in user information for $ip; fi && cpu=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" cat /proc/cpuinfo |grep 'model name' | head -n 1 | cut -d ":" -f 2` && echo "$user <-> $ip <-> $mac <-> $mem <-> $cpu" >> $inventory && echo "$ip" >> $uphosts || echo "$ip" >> $downhosts ; done
 done

finish_time=`date +%s`
 time_taken=`expr $finish_time - $start_time`
 if [ $time_taken -ge 60 ];
 then
 min=`expr $time_taken / 60`
 sec=`expr $time_taken % 60`
 if [ $min -ge 60 ];
 then
 hour=`expr $min / 60`
 min=`expr $min % 60`
 fi
 fi
 hour=`printf "%0*d\n" 2 $hour`
 min=`printf "%0*d\n" 2 $min`
 sec=`printf "%0*d\n" 2 $sec`
 echo "Script execution time: $hour:$min:$sec"

 

Mysql Backup script using mysqldump

This script is written to backup and email the status of backup

Please make sure to replace your domain with example.com, and user and passwords accordingly




#!/bin/bash

### GLOBAL VARIABLES ###
 HOST=`hostname`
 MYSQL="/usr/bin/mysql"
 MYSQLDUMP="/usr/bin/mysqldump"
 MYSQL_USER=backupuser
 MYSQL_PASS=DBPASSWORD
 TSTAMP=`date +%F`
 BACKUP_DIR=/example/backup/db/$HOST
 mkdir $BACKUP_DIR/$TSTAMP
 databases=`$MYSQL -u $MYSQL_USER -p$MYSQL_PASS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|phpmyadmin)"`
 DB_VERSIONS=7
 DELVERSION=`expr "$DB_VERSIONS" + 1`
 DELDATE=`date +%F --date="$DELVERSION days ago"`

### EMAIL VARIABLES ###

TO=admin@example.com
 FROM="Backup Automation <automation@example.com>"
 EMAILUSER=user@mydomain
 EMAILPASS=PASSWORD
 SMTP=mail.example.com
 SUBJECT="`hostname` - Backup status (DB) : $TSTAMP"
 SENDEMAIL="/example/scripts/sendEmail.pl"

###################### Creating Fresh Backup ###################

STARTTIME=`date +%s`
 START=`date +%r`

for db in $databases; do
 $MYSQLDUMP --routines --triggers --force --opt --max_allowed_packet=2G -u $MYSQL_USER  -p$MYSQL_PASS $db  > "$BACKUP_DIR/$TSTAMP/$db.sql" && mv "$BACKUP_DIR/$TSTAMP/$db.sql" "$BACKUP_DIR/$TSTAMP/$TSTAMP-$db.sql"
 done

BTIME=`date +%s`
 BT=`date +%r`
 BSTATUS=`echo $?`
 if [[ "$BSTATUS" == 0 ]]
 then
 BACKUPSTATUS=SUCCESSFULL
 else
 BACKUPSTATUS=FAILED
 fi

#for db in $databases;do
 #     $MYSQLDUMP --force --opt --routines --user=$MYSQL_USER -p$MYSQL_PASS --databases $db | gzip > "$BACKUP_DIR/$TSTAMP/$db.sql.gz" && mv "$BACKUP_DIR/$TSTAMP/$db.sql.gz" "$BACKUP_DIR/$TSTAMP/$TSTAMP-$db.sql.gz"
 #done

####################### Zipping Backups #########################

for db in $databases; do gzip -f $BACKUP_DIR/$TSTAMP/$TSTAMP-$db.sql; done
 ZSTATUS=`echo $?`
 CTIME=`date +%s`
 CT=`date +%r`

if [[ "$ZSTATUS" == 0 ]]
 then
 ZIPSTATUS=SUCCESSFULL
 else
 ZIPSTATUS=FAILED
 fi

####################### Deleting old backups ########################

echo rm -frv "$BACKUP_DIR/$DELDATE" >> /tmp/deleteinfo
 rm -frv "$BACKUP_DIR/$DELDATE"
 DSTATUS=`echo $?`

if [[ "$DSTATUS" == 0 ]]
 then
 DELSTATUS=SUCCESSFULL
 else
 DELSTATUS=FAILED
 fi
 #find "$BACKUP_DIR" -mtime +"$DELDATE" |while read DEL ; do rm -frv "$DEL";done

ENDTIME=`date +%s`
 END=`date +%r`

$SENDEMAIL -f "$FROM" -t "$TO" -s "$SMTP" -xu "$EMAILUSER" -xp "$EMAILPASS" -u "$SUBJECT" -m "
 Databases : `echo $databases| tr ' ' ,`

Summary :

DB Backup started at $START
 DB Backup finished at $BT
 DB Backup time is `expr $BTIME - $STARTTIME` seconds

DB Zipping starged at $BT
 DB Zipping finished at $CT
 DB Zipping time is `expr $CTIME - $BTIME` seconds

DB Deletion started at  $CT
 DB Deletion finished at $END
 Old DB Deleting time is `expr $ENDTIME - $CTIME` seconds

Total Time is `expr $ENDTIME - $STARTTIME` seconds

BACKUP is $BACKUPSTATUS
 GZIPPING SQL DB FILES is $ZIPSTATUS
 DELETING OLDVERSION is $DELSTATUS
 "
 echo Done