david/scripts-archive
david
/
scripts-archive
Archived
1
0
Fork 0
This repository has been archived on 2022-04-16. You can view files and clone it, but cannot push or open issues or pull requests.
scripts-archive/ping.sh

99 lines
2.4 KiB
Bash

#!/bin/bash
## ## ## ## ## ## ## ## ##
## ##
## jabber ping bot ##
## ##
## v0.1 ##
## ##
## author: david@socialnerds.org ##
## ##
## ## ## ## ## ## ## ## ##
## timestamp for logfile
timestamp=$(date '+%d %B %Y %H:%M')
echo "info: starting jabber ping bot $timestamp"
## check if sendxmpp is installed
if [ $(aptitude search sendxmpp | awk '{print $1}') = "i" ]; then
echo "info: sendxmpp found"
else
if [ $(whoami) = "root" ]; then
apt-get install sendxmpp
else
echo "error: permission denied"
echo "info: install sendxmpp or run this scrip as superuser"
exit 1
fi
fi
## set and load configfile
if [ -z $1 ]; then
configfile="/etc/mobots/ping.conf"
if [ -f $configfile ]; then
source $configfile
else
echo "error: no config file $configfile"
exit 1
fi
else
configfile=$1
if [ -f $configfile ]; then
source $configfile
else
echo "error: no config file $configfile"
exit 1
fi
fi
## check sendxmpp config
if [ -f ~/.sendxmpprc ]; then
echo "info: jabber config found in ~/.sendxmpprc"
else
if [ -z $port ]; then
port="5222"
else
echo "info: using port $port"
fi
echo "$user@$server:$port $pass" > ~/.sendxmpprc
chmod 600 ~/.sendxmpprc
echo "info: created sendxmpp config in ~/.sendxmpprc"
fi
## run the actual ping
set -- $dest
for var in "$@"
do
#result=$(ping -c 05 $var | grep transmitted | awk '{print $4}')
ping -c 03 $var >/dev/null 2>&1
errorcode=$?
if [ $errorcode = "0" ]; then
echo "info: $var is responding"
else
if [ $errorcode = "1" ]; then
echo "alert: $var is not responding"
## sending jabber message
echo "alert: $var is not responding
ping errorcode: $errorcode" | sendxmpp -r $res $watchdogs
else
if [ $errorcode = "2" ]; then
echo "alert: $var cannot be resolved"
## sending jabber message
echo "alert: $var cannot be resolved
ping errorcode: $errorcode" | sendxmpp -r $res $watchdogs
fi
fi
fi
done
## the end
exit 0