#!/bin/bash
# system log
sys_log()
{
logger -t smsd-handler -i "$1 from no.: $FROM"
}
# write sms to smsd
send_sms()
{
FILETMP=$(mktemp /tmp/_tmp_handler_send_XXXXXX)
# \n = new line
/bin/echo -e "To: $FROM\n\n$1" > $FILETMP
FILESMS=$(mktemp /var/spool/sms/outgoing/handler_send_XXXXXX)
mv "$FILETMP" "$FILESMS"
}
# expression for if
reg_expr()
{
/bin/echo -n "$TEXT" | grep -i "$1" >/dev/null
}
if [ "$1" == "RECEIVED" ]; then
# allowed numbers
ALLOWED_NO=" 12345678900 12345678901 12345678902 "
FROM=$(formail -zx From: < $2)
key=" $FROM "
if [[ $ALLOWED_NO == *$key* ]]; then
TEXT=$(sed -e '1,/^$/ d' < "$2")
# UPTIME check
if reg_expr "uptime password"; then
UPTIME=$(uptime)
sys_log "Authorized uptime"
send_sms "$UPTIME"
# PING check
elif reg_expr "ping password"; then
# ping function
my_ping()
{
/bin/ping -qc5 -s1 $1 | grep transmitted
}
status1=$(my_ping 192.168.0.2)
status2=$(my_ping 192.168.1.2)
set $status1
n1=$6
set $status2
n2=$6
sys_log "Authorized ping"
send_sms "Ping lost:\n# N1: $n1 # N2: $n2"
# REBOOT
elif reg_expr "reboot password"; then
send_sms "Rebooting Server"
sys_log "Authorized reboot"
sleep 20; sudo /sbin/reboot
else
# nothing
exit
fi
else
sys_log "Unuthorized action"
# or
##send_sms "You are not authorized"
fi
fi
# system log
sys_log()
{
logger -t smsd-handler -i "$1 from no.: $FROM"
}
# write sms to smsd
send_sms()
{
FILETMP=$(mktemp /tmp/_tmp_handler_send_XXXXXX)
# \n = new line
/bin/echo -e "To: $FROM\n\n$1" > $FILETMP
FILESMS=$(mktemp /var/spool/sms/outgoing/handler_send_XXXXXX)
mv "$FILETMP" "$FILESMS"
}
# expression for if
reg_expr()
{
/bin/echo -n "$TEXT" | grep -i "$1" >/dev/null
}
if [ "$1" == "RECEIVED" ]; then
# allowed numbers
ALLOWED_NO=" 12345678900 12345678901 12345678902 "
FROM=$(formail -zx From: < $2)
key=" $FROM "
if [[ $ALLOWED_NO == *$key* ]]; then
TEXT=$(sed -e '1,/^$/ d' < "$2")
# UPTIME check
if reg_expr "uptime password"; then
UPTIME=$(uptime)
sys_log "Authorized uptime"
send_sms "$UPTIME"
# PING check
elif reg_expr "ping password"; then
# ping function
my_ping()
{
/bin/ping -qc5 -s1 $1 | grep transmitted
}
status1=$(my_ping 192.168.0.2)
status2=$(my_ping 192.168.1.2)
set $status1
n1=$6
set $status2
n2=$6
sys_log "Authorized ping"
send_sms "Ping lost:\n# N1: $n1 # N2: $n2"
# REBOOT
elif reg_expr "reboot password"; then
send_sms "Rebooting Server"
sys_log "Authorized reboot"
sleep 20; sudo /sbin/reboot
else
# nothing
exit
fi
else
sys_log "Unuthorized action"
# or
##send_sms "You are not authorized"
fi
fi
Reboot perms for smsd (/etc/sudoers):
Small example for others