- Joined
- Sep 26, 2002
- Location
- Indianapolis, IN
here is another script that i wrote for #idlerpg on synIRC (see http://splat.mine.nu/idlerpg if you don't know what that is). i have two scripts. the first one is to just return the status to the command line. the second will check the status page and see if i am online or not. if i am not, it will email me.
./idlerpg_v1
example output:
here is the 2nd one which emails:
./idlerpg_v2
with this one, there is no output to the command line but will email me:
./idlerpg_v1
Code:
#!/bin/bash
## written by mbentley
## set variables
command=$0
username=$1
function check_username {
if [ -z "${username}" ];
then
echo "Usage: $command <username>";
exit 1
fi
}
function temp_dir {
tempdir=/tmp/idlerpg_$username_`date +%N`
mkdir $tempdir
if [ ! -d "$tempdir" ]
then
exit 1
fi
}
function query_stats {
cd $tempdir
wget -t 1 -T 5 -q http://splat.mine.nu/idlerpg/xml.php?player=$username
}
function verify_download {
if [ ! -e "$tempdir/xml.php?player=$username" ]
then
echo "Error fetching data! Please check manually: http://splat.mine.nu/idlerpg/players.php";
cleanup_tmp
exit 1
fi
}
function cleanup_tmp {
rm -rf $tempdir
}
function validate_user {
validate=`cat $tempdir/xml.php?player=$username | grep "<online>" | awk -F "[<>]" '{ print $3 }'`
if [ -z "${validate}" ];
then
echo "$username is not a valid user (usernames are case sensitive)";
cleanup_tmp
exit 1
fi
}
function grab_status {
user_status=$validate
}
function status_check {
if [ $user_status == 1 ];
then
echo "$username is online";
else
echo "$username is offline";
fi
}
## run functions
check_username
temp_dir
query_stats
verify_download
validate_user
grab_status
status_check
cleanup_tmp
example output:
root@athena:~/scripts# ./idlerpg_v1 mbentley
mbentley is online
root@athena:~/scripts# ./idlerpg_v1 Baked
Baked is offline
here is the 2nd one which emails:
./idlerpg_v2
Code:
#!/bin/bash
## written by mbentley
## set variables
command=$0
username=$1
function check_username {
if [ -z "${username}" ];
then
echo "Usage: $command <username>";
exit 1
fi
}
function temp_dir {
tempdir=/tmp/idlerpg_$username_`date +%N`
mkdir $tempdir
if [ ! -d "$tempdir" ]
then
exit 1
fi
}
function query_stats {
cd $tempdir
wget -t 1 -T 5 -q http://splat.mine.nu/idlerpg/xml.php?player=$username
}
function verify_download {
if [ ! -e "$tempdir/xml.php?player=$username" ]
then
cleanup_tmp
exit 1
fi
}
function cleanup_tmp {
rm -rf $tempdir
}
function validate_user {
validate=`cat $tempdir/xml.php?player=$username | grep "<online>" | awk -F "[<>]" '{ print $3 }'`
if [ -z "${validate}" ];
then
cleanup_tmp
exit 1
fi
}
function grab_status {
user_status=$validate
}
function status_check {
if [ $user_status == 0 ];
then
email_offline
fi
}
function email_offline {
mail=$tempdir/mail_`date +%F`
touch $mail
chmod 600 $mail
echo -e "subject: idleRPG: $username is offline" >> $mail
echo -e "$username is currently offline!" >> $mail
/usr/sbin/sendmail -f [email protected] root < $mail
}
## run functions
check_username
temp_dir
query_stats
verify_download
validate_user
grab_status
status_check
cleanup_tmp
with this one, there is no output to the command line but will email me:
Date: Wed, 09 Jun 2010 15:49:47 -0400
From: root <[email protected]>
Subject: idleRPG: Baked is offline
Baked is currently offline!
Last edited: