SMS Server Tools 3
This site is hosted by Kekekasvi.com
 Menu
Basic information:
Additional information:
Support:
Get SMS Server Tools 3:
Additional Options

 Sponsored links

 Search
Custom Search

 Visitor locations
 
 SMS Server Tools 3 Community
Welcome, Guest. Please login or register. Fri Mar 29, 2024 10:31
SMSTools3 Community » Help and support Bottom

set system date and time via smstools

Login and Post Reply

Page:  1

Author Post
Member
Registered:
Mar 2017
Location: Germany
Raspberry Pi - Raspian Jessie:
current smstools version:
installed via apt-get command
Huawei Surf Stick

Hello,
it is possible to set system date and time via smstools?
Thanks.

Cosmoxer

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
With eventhandler everything is possible. Take a look at this post where other member from Germany is using eventhandler to execute system commands.

However, many commands require privileges of root, and by default smsd is running as smsd:dialout. Therefore setting time with date -s does not work directly. You could run smsd as root, or preferably create a program to change the time and suid that program.

Member
Registered:
Mar 2017
Location: Germany
Topic owner
Hi keke,

thanks for your reply.

Maybe my question was not written clearly.

I use my raspberry pi without internet connection. So I thought it would be possible to set date and time with the help of 3G modem.

Is it possible?

Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
As there is no "time protocol" available in the GSM network, some trick should be used.

When a SMS is sent, time of sending is written to the file. When a status report is requested, and the report is received, there is a timestamp which tells when SMSC received the message for delivery. At least with messages containing single part only, those timestamps are almost the same, or at least very near to each other.

Here is an eventhandler which is based on the scripts/eventhandler_report sample. When a report is received, the script determines the difference of times, and then adjusts system time if the difference is too big. A command super is used for adjusting, because only root can change the time. A line date /bin/date smsd is required in the file /etc/super.tab, when smsd is running as smsd:dialout.

The variable ADJUST_TIME defines how many seconds of difference is acceptable. Value 0 disables the adjusting.

#!/bin/bash
# SMS Server Tools 3.
# Sample eventhandler script for storing delivery timestamps.

ADJUST_TIME=10

#--------------------------------------------------------------------------
date2stamp()
{
  date --utc --date "$1" +%s
}

dateDiff()
{
  local sec=0

  case $1 in
      -s)   sec=1;      shift;;
      -m)   sec=60;     shift;;
      -h)   sec=3600;   shift;;
      -d)   sec=86400;  shift;;
      *)    sec=86400;;
  esac
  local dte1=$(date2stamp "$1")
  local dte2=$(date2stamp "$2")
  local diffSec=$((dte2-dte1))
  echo $((diffSec/sec))
}

set_header()
{
  # $1 = filename
  # $2 = header

  local tmp=`mktemp /tmp/smsd_XXXXXX`
  cp "$1" $tmp
  formail -f -I "$2" < $tmp > "$1"
  unlink $tmp
}

#--------------------------------------------------------------------------

# The following code stores delivery timestamp to the sent message
if [ "$1" = "REPORT" ]; then
  SENTDIR=/var/spool/sms/sent
  if grep "Status: 0" $2 >/dev/null; then
    FROM=`formail -zx From: < $2`
    TMPFILE=`mktemp /tmp/smsd_XXXXXX`
    formail -I "" < $2 | sed -e"1,2d" > $TMPFILE
    MESSAGE_ID=`formail -zX Message_id: < $TMPFILE`
    RECEIVED=`formail -zx Discharge_timestamp: < $TMPFILE`
    grep -lx "$MESSAGE_ID" $SENTDIR/* > $TMPFILE
    cat $TMPFILE | while read FNAME; do
      OLDRECEIVED=`formail -zx Received: < ${FNAME}`
      if [ "$OLDRECEIVED" = "" ]; then
        TO=`formail -zx To: < ${FNAME}`
        if [ "$TO" = "$FROM" ]; then
          set_header "${FNAME}" "Received: $RECEIVED"

          if [ $ADJUST_TIME -gt 0 ]; then
            SENT=`formail -zx Sent: < ${FNAME}`
            TIME_DIFF=`dateDiff -s "$SENT" "$RECEIVED"`
            set_header "${FNAME}" "Time_diff: $TIME_DIFF"
            AGO=""
            if [ $TIME_DIFF -lt 0 ]; then
              AGO=" ago"
              TIME_DIFF=$(($TIME_DIFF * -1))
            fi
            if [ $TIME_DIFF -ge $ADJUST_TIME ]; then
              super date -s "$TIME_DIFF seconds$AGO"
            fi
          fi

        fi
      fi
    done
    unlink $TMPFILE
  fi
fi

exit 0
 
 
'bash' Syntax Highlight powered by GeSHi


Member
Registered:
Mar 2017
Location: Germany
Topic owner
Hello deke,

thanks for you reply. I'll try to test it next week.

cosmoxer

Member
Registered:
Mar 2017
Location: Austria
@keke

I have the same problem. Thank you for take care.

Login and Post Reply

Page:  1

SMSTools3 Community » Help and support Top

 
Time in this board is UTC.  

Privacy Policy   SMS Server Tools 3 Copyright © Keijo Kasvi.