... So on here is my Last work ... for this year ...
A sample bash Script for Linux developed on OpenSuSE 13.2
*) Check Admin Numbers
*) Check Servers allowed
*) Check cmd allowed
*) log incoming sms on error
*) send to admins sms
*) answer to Admins is not a Server cmd if incoming not from admins
*) ssh to Servers allowed cmd with User pwd and Root pwd
*) Running ssh cmd as Nobody
*) defined Users for ssh allowed for the SMS answer
*) Script password
Let the User Nobody Access /var/log/sms_incomming.log
With "chown nobody:nogroup file"
chmod +x smsd_notify.sh
chmod +x event.sh
event.sh
#!/bin/bash
#!/dev/tty1
NOW=$(date +"%m-%d-%y");
HOUR=$(date +"%T");
#run this script only when a message was received.
#sms like: PASSWORD ACTION rootpwd userpwd user host port cmd
#sms like: PASSWORD ACTION rootpwd userpwd user host@host port cmd
#cmd like: reboot , or call a Script like: /usr/local/bin/script.sh what have no options
################################### EDIT THIS ###########################################
# script PASSWORD
PASSWORD="Password"
# admin sms number
ADMIN="43676xxxxxxx"
ADMIN2="43676xxxxxxx"
# hosts in chain
# Nobody's Password
NBPWD="Nobody"
# host list for 2 accessable servers
HOSTS2=('192.168.0.10' '192.168.0.11');
# end host list
USER1="user1"
USER2="user2"
################################### EDIT THIS ###########################################
if [ "$1" = "RECEIVED" ]; then
TEXT=`sed -e '1,/^$/ d' < $2`
#Extract data from the SMS file
SENDER=`grep "From: " $2 | cut -d " " -f2`
RCVPASS=`echo "$TEXT" | cut -d " " -f1`
ACTION=`echo "$TEXT" | tr "a-z" "A-Z" | cut -d " " -f2`
ROOTPWD=`echo "$TEXT" | cut -d " " -f3`
USERPWD=`echo "$TEXT" | cut -d " " -f4`
USERNAME=`echo "$TEXT" | cut -d " " -f5`
HOSTS=`echo "$TEXT" | tr "A-Z" "a-z" | cut -d " " -f6`
PORTS=`echo "$TEXT" | cut -d " " -f7`
CMD=`echo "$TEXT" | cut -d " " -f8`
if [ -z "$PORTS" ]; then
PORTS="22"
else
PORTS=`echo "$TEXT" | cut -d " " -f7`
fi
TARGET=(${HOSTS//@/ });
case "$SENDER" in
"$ADMIN")
/etc/smsd_notify.sh $ADMIN "Info: $ACTION@${HOSTNAME} Is accepted From User: ${USERNAME} $NOW $HOUR"
;;
"$ADMIN2")
/etc/smsd_notify.sh $ADMIN2 "Info: $ACTION@${HOSTNAME} Is accepted From User: ${USERNAME} $NOW $HOUR"
;;
*)
/etc/smsd_notify.sh $ADMIN "You have a new Message from $SENDER : $TEXT $NOW $HOUR"
/etc/smsd_notify.sh $ADMIN2 "You have a new Message from $SENDER : $TEXT $NOW $HOUR"
exit 0
;;
esac
for G in ${TARGET[@]} ; do
FOUND=`echo "${HOSTS2[*]}" | grep "$G"`
if [ "${FOUND}" != "" ]; then
if [ "$PASSWORD" = "$RCVPASS" ]; then
case "${G}" in
"${HOSTS2[0]}")
echo $NBPWD | sudo -u nobody -S bash -c : && RUNAS="sudo -u nobody"
$RUNAS bash<<_
#!/bin/bash
#!/dev/tty0
case "$ACTION" in
#ssh
SSH)
sshpass -p "$USERPWD" ssh -p $PORTS -t -A -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o GlobalKnownHostsFile=/dev/null -E ~/ssh.log ${USERNAME}@${G} "bash -c 'echo $ROOTPWD | sudo -u root -S ${CMD} ; exit'";
;;
#else cmd
*)
echo Warning incomming sms for $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR >> /var/log/sms_incomming.log
if [ "${USER1}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
elif [ "${USER2}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN2 "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
fi
;;
esac
_
;;
"${HOSTS2[1]}")
echo $NBPWD | sudo -u nobody -S bash -c : && RUNAS="sudo -u nobody"
$RUNAS bash<<_
#!/bin/bash
#!/dev/tty0
case "$ACTION" in
#ssh
SSH)
sshpass -p "$USERPWD" ssh -p $PORTS -t -A -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o GlobalKnownHostsFile=/dev/null -E ~/ssh.log ${USERNAME}@${G} "bash -c 'echo $ROOTPWD | sudo -u root -S ${CMD} ; exit'";
;;
#else cmd
*)
echo Warning incomming sms for $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR >> /var/log/sms_incomming.log
if [ "${USER1}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
elif [ "${USER2}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN2 "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
fi
;;
esac
_
;;
esac
fi
else
echo Warning incomming sms for $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR >> /var/log/sms_incomming.log
if [ "${USER1}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
elif [ "${USER2}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN2 "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
fi
fi
done
fi
exit 0
#!/dev/tty1
NOW=$(date +"%m-%d-%y");
HOUR=$(date +"%T");
#run this script only when a message was received.
#sms like: PASSWORD ACTION rootpwd userpwd user host port cmd
#sms like: PASSWORD ACTION rootpwd userpwd user host@host port cmd
#cmd like: reboot , or call a Script like: /usr/local/bin/script.sh what have no options
################################### EDIT THIS ###########################################
# script PASSWORD
PASSWORD="Password"
# admin sms number
ADMIN="43676xxxxxxx"
ADMIN2="43676xxxxxxx"
# hosts in chain
# Nobody's Password
NBPWD="Nobody"
# host list for 2 accessable servers
HOSTS2=('192.168.0.10' '192.168.0.11');
# end host list
USER1="user1"
USER2="user2"
################################### EDIT THIS ###########################################
if [ "$1" = "RECEIVED" ]; then
TEXT=`sed -e '1,/^$/ d' < $2`
#Extract data from the SMS file
SENDER=`grep "From: " $2 | cut -d " " -f2`
RCVPASS=`echo "$TEXT" | cut -d " " -f1`
ACTION=`echo "$TEXT" | tr "a-z" "A-Z" | cut -d " " -f2`
ROOTPWD=`echo "$TEXT" | cut -d " " -f3`
USERPWD=`echo "$TEXT" | cut -d " " -f4`
USERNAME=`echo "$TEXT" | cut -d " " -f5`
HOSTS=`echo "$TEXT" | tr "A-Z" "a-z" | cut -d " " -f6`
PORTS=`echo "$TEXT" | cut -d " " -f7`
CMD=`echo "$TEXT" | cut -d " " -f8`
if [ -z "$PORTS" ]; then
PORTS="22"
else
PORTS=`echo "$TEXT" | cut -d " " -f7`
fi
TARGET=(${HOSTS//@/ });
case "$SENDER" in
"$ADMIN")
/etc/smsd_notify.sh $ADMIN "Info: $ACTION@${HOSTNAME} Is accepted From User: ${USERNAME} $NOW $HOUR"
;;
"$ADMIN2")
/etc/smsd_notify.sh $ADMIN2 "Info: $ACTION@${HOSTNAME} Is accepted From User: ${USERNAME} $NOW $HOUR"
;;
*)
/etc/smsd_notify.sh $ADMIN "You have a new Message from $SENDER : $TEXT $NOW $HOUR"
/etc/smsd_notify.sh $ADMIN2 "You have a new Message from $SENDER : $TEXT $NOW $HOUR"
exit 0
;;
esac
for G in ${TARGET[@]} ; do
FOUND=`echo "${HOSTS2[*]}" | grep "$G"`
if [ "${FOUND}" != "" ]; then
if [ "$PASSWORD" = "$RCVPASS" ]; then
case "${G}" in
"${HOSTS2[0]}")
echo $NBPWD | sudo -u nobody -S bash -c : && RUNAS="sudo -u nobody"
$RUNAS bash<<_
#!/bin/bash
#!/dev/tty0
case "$ACTION" in
#ssh
SSH)
sshpass -p "$USERPWD" ssh -p $PORTS -t -A -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o GlobalKnownHostsFile=/dev/null -E ~/ssh.log ${USERNAME}@${G} "bash -c 'echo $ROOTPWD | sudo -u root -S ${CMD} ; exit'";
;;
#else cmd
*)
echo Warning incomming sms for $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR >> /var/log/sms_incomming.log
if [ "${USER1}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
elif [ "${USER2}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN2 "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
fi
;;
esac
_
;;
"${HOSTS2[1]}")
echo $NBPWD | sudo -u nobody -S bash -c : && RUNAS="sudo -u nobody"
$RUNAS bash<<_
#!/bin/bash
#!/dev/tty0
case "$ACTION" in
#ssh
SSH)
sshpass -p "$USERPWD" ssh -p $PORTS -t -A -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o GlobalKnownHostsFile=/dev/null -E ~/ssh.log ${USERNAME}@${G} "bash -c 'echo $ROOTPWD | sudo -u root -S ${CMD} ; exit'";
;;
#else cmd
*)
echo Warning incomming sms for $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR >> /var/log/sms_incomming.log
if [ "${USER1}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
elif [ "${USER2}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN2 "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
fi
;;
esac
_
;;
esac
fi
else
echo Warning incomming sms for $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR >> /var/log/sms_incomming.log
if [ "${USER1}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
elif [ "${USER2}" = "${USERNAME}" ];then
/etc/smsd_notify.sh $ADMIN2 "Warning: $ACTION@${G} Port: ${PORTS} rootpwd: ${ROOTPWD} userpwd: ${USERPWD} from User: ${USERNAME} $NOW $HOUR";
fi
fi
done
fi
exit 0
And the smsd_notify.sh
#!/bin/bash
# This script send a text sms at the command line by creating
# a sms file in the outgoing queue.
# $1 is the destination phone number.
# $2 is the message text.
# like: your_script.sh 436768889901 "long message text"
ARGV=("$@")
ARGC=("$#")
#main - read incoming sms message and queue it to smsd
msg_rcpt=$1
msg_body=$2
msg_body_160=${msg_body:0:160}
# now send the sms's
outgoing="/var/spool/sms/GSM/GSM1"
file=`mktemp $outgoing/smsd_XXXXXX`
echo "To: $msg_rcpt" >> $file
echo "" >> $file
echo "$msg_body_160" >> $file
exit 0
# This script send a text sms at the command line by creating
# a sms file in the outgoing queue.
# $1 is the destination phone number.
# $2 is the message text.
# like: your_script.sh 436768889901 "long message text"
ARGV=("$@")
ARGC=("$#")
#main - read incoming sms message and queue it to smsd
msg_rcpt=$1
msg_body=$2
msg_body_160=${msg_body:0:160}
# now send the sms's
outgoing="/var/spool/sms/GSM/GSM1"
file=`mktemp $outgoing/smsd_XXXXXX`
echo "To: $msg_rcpt" >> $file
echo "" >> $file
echo "$msg_body_160" >> $file
exit 0
'bash' Syntax Highlight powered by GeSHi
Well know it has bugs but answers welcome ...
Good day