roberts
Group: Members
Posts: 4983
Joined: Oct. 2003 |
|
Posted: Feb. 21 2007,14:52 |
|
I have updated my Lua script so that the NIST timeserver can be specified and also the initial connection timeout defaults to 5 seconds but can be user specified as well.
Code Sample | #! /bin/lua --[[ This lua script based in part of the idea expressed in the bash script from newOldUser of the damnsmall linux forums by Robert Shingledecker - 2007/02/17
Usage: sudo gettime.lus {nist_time_server} {timeout-in-secs}
See: http://tf.nist.gov/service/its.htm
This script gets time information from an NIST time server in universal time format. It then rearranges it into the format that the date command uses MMDDhhmmCCYY.ss and issues the date command.
An internet connection must be available
sample of returned information follows 54144 07-02-13 16:14:43 00 0 0 95.3 UTC(NIST) * 1---0----1----1----2----2----3----3----4----4----5 1---5----0----5----0----5----0----5----0----5----0 --]]
host = arg[1] or "time-nw.nist.gov" port = 13 timeout = arg[2] or 5.00 target = {10,13,16,19,7,22} date = ""
timeserver = socket.tcp() timeserver:settimeout(timeout) _,err = timeserver:connect(host,port) if err then print("Could not connect "..err) os.exit(1) end _,err = timeserver:send("anything\n") if err then print("Error in sending "..err) os.exit(1) end print("Requesting time from: "..host) timeserver:settimeout(0.50) while 1 do line,err = timeserver:receive('*l') if err then print("Error in receiving "..err) os.exit(1) end if string.find(line,"UTC") then for i = 1, 6 do if i == 5 then date = date .. "20" end if i == 6 then date = date .. "." end date = date .. string.sub(line,target[i],target[i]+1) end os.execute("date -u "..date) os.exit(0) end end
|
Please choose a timeserver nearest to you so that we all don't gang up on one. See the URL embedded for a list.
|