1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

my ntpsync blocked .. could htpdate work instead?

Discussion in 'Tomato Firmware' started by sage_za, Aug 2, 2008.

  1. sage_za

    sage_za Addicted to LI Member

    Hello there,

    I live in a condominium with shared broadband access. To safeguard the internal network, many ports are blocked by the gateway.
    This means that my Tomato WRT54G v2 no longer gains and sets it's time via NTP..
    I suspect that other folks may also be in my situation, hence my post.
    I've made a search in these Forums, but didn't find any discussion regarding this.

    I've looked around the Web, and found htpdate, which seems to offer a potential solution as it's web server driven through port 80.
    It's really small, which is a good thing.
    Also, I don't need the time to be perfectly set.

    I'm no Linux guru, and don't have an option to compile something, but I see that OpenWRT has htpdate.
    Looking at it's makefile, it seems that it just needs a configuration file, and a properly combined binary, to work...
    Also, it seems to have a dependency on libconfig - I'm not sure if this is available already within Tomato (could be the dealbreaker if this is true..I hope not).

    Could someone consider this, and let me know if:
    a) a binary and sample config can be made for Tomato (e.g. one could then wget it via a script, and run it from there)
    b) whether it could be built into a future firmware release?
    (all assuming it can even be run within the environment in the first place of course)


    Some useful links:

    http://www.clevervest.com/twiki/bin/view/HTP/WebHome
    http://www.rkeene.org/oss/htp/
    https://dev.openwrt.org/cgi-bin/trac.fcgi/browser/packages/net/htpdate/Makefile
     
  2. TexasFlood

    TexasFlood Network Guru Member

    I don't have a direct answer to your question but have some alternative suggestions.

    Some ISP routers provide NTP service. Try a traceroute to, for example google.com. See if you can identify ISP upstream routers and try those as NTP time sources.

    Also if you can afford to be off by a few seconds, get the time off of the web and use it to set the time with a date command. I can retrieve my local time that's within a second or so of NTP by running the below command on my Tomato router. I'm sure there are other ways, this is just the first one I found.

    wget -O - http://tycho.usno.navy.mil/cgi-bin/timer.pl|grep MDT|sed '/<BR>/ s/<BR>//'
     
  3. sage_za

    sage_za Addicted to LI Member

    Mmm, your idea sparked off something for me...thanks!

    The script on tycho.usno.navy.mil is what opened my mind - the issue was that it's output does not include the year, and is formatted for humans to read. Which is not what the date command likes and would need to be manipulated first..

    So, first, I created a small Perl script (stamp.cgi) to do the hard work. This script I uploaded to my own webpages on my ISP, and set the permissions. When called, it spits out the required date format, to within 1 minute..
    Code:
    #!/usr/bin/perl
    print "Content-Type: text/plain\n\n", `date "+%m%d%H%M%Y"`; 
    Great, now I have a way to get that server's time via HTTP.
    So, next, I call it from Tomato in the WAN UP script, like so:

    Code:
    #!/bin/sh
    logger DATE_TIME Script Executing
    sleep 5
    eval TIMEFILE="/tmp/temptime"
    eval TEMPTIME=""
    echo "" > $TIMEFILE
    eval SOURCE="http://www.whateveryoursiteis.com/cgi-bin/stamp.cgi"
    wget $SOURCE -O - >> $TIMEFILE
    TEMPTIME=`cat $TIMEFILE`
    date -s $TEMPTIME
    logger DATE_TIME Script completed: $TEMPTIME
    Thanks...its working now :smile:

    Could I run this script elsewhere e.g. Firewall ??
    Reason being, it will run every time the lease is renewed which is unnecessary..
    I suppose I could write it to check for the existence of $TIMEFILE first and skip, but I'm lazy now...
     
  4. fyellin

    fyellin LI Guru Member

    I think you should be asking "How can I run my script more often?", not "How can I run it less often?" Clocks on routers have been known to be cheap and to drift unreliably. I'd want to fix the clock at least once a day.

    To give an idea of whether this is happening on your router, you could log the time both before and after you change it, so you can see how much it is drifting.
     
  5. sage_za

    sage_za Addicted to LI Member

    I understand your point ... the IP lease here for me is 6 hours, which means the script runs every 6 hours.
    So that takes care of re-running and updating the time.

    This script of mine isn't the best - I'll be the first to admit...
    - It only sets the time to the nearest minute
    - It needs one to have access to another Un*x webserver, with a properly configured cgi-bin
    - It needs that server to allow Perl scripts to run
    - It depends on the lease time to re-run

    But it was good fun for me to write it, and it serves as a workaround.

    I'd still like a binary of htpdate, so I can cron that ... anyone out there who can compile it, or say if it's a viable option??
     
  6. TexasFlood

    TexasFlood Network Guru Member

    Seems like a pretty decent workaround to me. But I would think that, wouldn't I, :grin:

    If you wanted more control, you could put something in the Init tab. As the boot default date is "Wed Dec 31 17:00:00 MST 1969", run the intial update until the year is not 1969 then set cron to run it at your defined interval.

    I'm not much of a script writer but I still like to hack away for fun. Here is an (untested) example of what that might look like. This (hopefully) writes out the script to a file in /tmp, set permissions to allow root execution, sets cron to run it at the top of every hour, then runs it every 5 seconds until the year is updated to other than 1969 & exits.
    Code:
    #!/bin/sh
    /bin/sleep 5
    /bin/cat <<END > /tmp/setdate.sh
    #!/bin/sh
    /usr/bin/logger DATE_TIME Script Executing
    eval SOURCE="http://www.whateveryoursiteis.com/cgi-bin/stamp.cgi"
    /bin/date -s `/usr/bin/wget $SOURCE -O -`
    /usr/bin/logger DATE_TIME Script completed: `/bin/date`
    END
    /bin/chmod 755 /tmp/setdate.sh
    /usr/sbin/cru a SetDateFromISPCGIPerl "0 * * * * /tmp/setdate.sh >/dev/null 2>&1"
    while [ `/bin/date | /usr/bin/cut -d " " -f 7` = "1969" ]
    do
      /bin/sleep 5
      /tmp/setdate.sh
    done
    And couldn't you get accuracy (nearly) to the second with this?
    Code:
    #!/usr/bin/perl
    print "Content-Type: text/plain\n\n", `date "+%m%d%H%M%Y.%S"`;
     
  7. sage_za

    sage_za Addicted to LI Member

    Of course! ... one can cat to a file, and then cron that file ... nice piece of thinking, I missed that. My head was tied up in the scripting sections of WAN UP...
    Yes, %S gives the seconds - I just tested it as you wrote - format is correct and DATE accepts it properly.
    Nice...
     
  8. reddwarf

    reddwarf Networkin' Nut Member

    Hey Guys - I know this is pretty old by I am trying this and not getting the proper script written to /tmp/setdate.sh file from the wanup script. Do I need escape characaters somewhere now?
    Tomato Firmware v1.28.7495 MIPSR2-Toastman-RT K26 VPN
    Running on an ASUS N12

    root@n12-remote:/tmp/home/root# cat /tmp/script_wanup.sh
    #!/bin/sh
    #!/bin/sh
    /bin/sleep 5
    /bin/cat <<END > /tmp/setdate.sh
    #!/bin/sh
    logger DATE_TIME Script Executing
    sleep 5
    eval TIMEFILE="/tmp/temptime"
    eval TEMPTIME=""
    echo "" > $TIMEFILE
    eval SOURCE="http://www.fsdfas.sdsdf/sdf/saf"
    wget $SOURCE -O - >> $TIMEFILE
    TEMPTIME=`cat $TIMEFILE`
    date -s $TEMPTIME
    logger DATE_TIME Script completed: $TEMPTIME
    END
    /bin/chmod 755 /tmp/setdate.sh
    /usr/sbin/cru a SetDateFromISPCGIPerl "0 * * * * /tmp/setdate.sh >/dev/null 2>&1
    "
    while [ `/bin/date | /usr/bin/cut -d " " -f 7` = "1969" ]
    do
    /bin/sleep 5
    /tmp/setdate.sh
    done

    root@n12-remote:/tmp/home/root# cat /tmp/setdate.sh
    #!/bin/sh
    logger DATE_TIME Script Executing
    sleep 5
    eval TIMEFILE="/tmp/temptime"
    eval TEMPTIME=""
    echo "" >
    eval SOURCE="www.fsdfas.sdsdf/sdf/saf"
    wget -O - >>
    TEMPTIME=
    date -s
    logger DATE_TIME Script completed:
    root@n12-remote:/tmp/home/root#
     
  9. reddwarf

    reddwarf Networkin' Nut Member

    Working script ended up being:

    #!/bin/sh
    /bin/sleep 5
    /bin/cat <<END > /tmp/setdate.sh
    #!/bin/sh
    logger DATE_TIME Script Executing
    eval SOURCE="http://www.blah.com/blah/blah"
    wget \$SOURCE -O - > /tmp/TIMEFILE
    chmod 755 /tmp/TIMEFILE
    TEMPTIME=\`cat /tmp/TIMEFILE\`
    date -u \$TEMPTIME
    logger DATE_TIME Script completed: \$TEMPTIME
    END
    /bin/chmod 755 /tmp/setdate.sh
    /usr/sbin/cru a SetDateFromISPCGIPerl "0 * * * * /tmp/setdate.sh >/dev/null 2>&1"
    while [ `/bin/date | /usr/bin/cut -d " " -f 7` = "1969" ]
    do
    /bin/sleep 5
    /tmp/setdate.sh
    done
     
  10. jsmiddleton4

    jsmiddleton4 Network Guru Member

    Working on this in another thread. Is there an existing default web site that can be placed in the blah\blah\blah....?
     
  11. reddwarf

    reddwarf Networkin' Nut Member

    Sounds like you got it working. I don't know if a "public" website does this in the proper date format, but you can use mine for fun and giggles if you want. I will PM you.
     
  12. jsmiddleton4

    jsmiddleton4 Network Guru Member

    Can 't get it to read in Init or Wanup.
     
  13. reddwarf

    reddwarf Networkin' Nut Member

    Can you get it run from SSH - ?
    Code:
    sh /tmp/setdate.sh
    This file should have been created when the cron job ran the first time.

    Does it create the /tmp/TIMEFILE ? Does this file have the date in it?
    Code:
    cat /tmp/TIMEFILE
    As to running it from init - that didn't work for me either - I suspect that the routes are not up by then.
     

Share This Page