我的openwrt学习笔记(二十二):网络时间同步

来源:互联网 发布:java 静态代理原理 编辑:程序博客网 时间:2024/05/17 02:48

在实际的LINUX 开发中,网络设备都有工作时间,一般需要进行网络时间同步。

一般采用NTP服务器进行同步。

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止恶毒的协议攻击。

对嵌入式设备而言,NTP方式关键在于选择哪个NTP服务器,因为有的NTP服务器有时候会出现些问题,如无法访问!

 

本文介绍一种利用HTTP header信息的方法,需要借助curl,curl是什么后面会进行分析阐述。

 需要一定的shell http知识哦!

linux@ubuntu:~/http/testcurl$ curl -sI http://nist.time.gov/timezone.cgi?UTC/s/0| awk -F': ' '/Date: /  {print $2}'

Mon, 24 Aug 2015 11:19:25 GMT

linux@ubuntu:~/http/testcurl$ date -s "curl -sI http://nist.time.gov/timezone.cgi?UTC/s/0| awk -F': ' '/Date: /  {print $2}'"

date: invalid date `curl -sI http://nist.time.gov/timezone.cgi?UTC/s/0| awk -F\': \' \'/Date: /  {print }\''

linux@ubuntu:~/http/testcurl$ date -s "`curl -sI http://nist.time.gov/timezone.cgi?UTC/s/0| awk -F': ' '/Date: /  {print $2}'`"

date: cannot set date: Operation not permitted

Mon Aug 24 19:20:00 CST 2015

linux@ubuntu:~/http/testcurl$ sudo date -s "`curl -sI http://nist.time.gov/timezone.cgi?UTC/s/0| awk -F': ' '/Date: /  {print $2}'`"

[sudo] password for linux:

Sorry, try again.

[sudo] password for linux:

Mon Aug 24 19:20:09 CST 2015

 

 

换个国内的baidu.com吧,再来测试下。

linux@ubuntu:~/http/testcurl$ sudo date -s "`curl -sI www.baidu.com| awk -F': ' '/Date: /  {print $2}'`"

[sudo] password for linux:

Wed Aug 26 13:48:49 CST 2015

linux@ubuntu:~/http/testcurl$ curl -sI www.baidu.com

HTTP/1.1 200 OK

Date: Wed, 26 Aug 2015 05:49:09 GMT

Content-Type: text/html; charset=utf-8

Connection: Keep-Alive

Vary: Accept-Encoding

Set-Cookie: BAIDUID=5B78DC1DCE22807CBA79CEBA91B125CB:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: BIDUPSID=5B78DC1DCE22807CBA79CEBA91B125CB; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: PSTM=1440568149; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: BDSVRTM=0; path=/

Set-Cookie: BD_HOME=0; path=/

Set-Cookie: H_PS_PSSID=16415_1428_14602_12868_16799_16904_17000_17004_12835_15582_12338_13932_16950_16968_16866; path=/; domain=.baidu.com

P3P: CP=" OTI DSP COR IVA OUR IND COM "

Cache-Control: private

Cxy_all: baidu+005986c4967689962c9f01e2f019a0d3

Expires: Wed, 26 Aug 2015 05:49:02 GMT

X-Powered-By: HPHP

Server: BWS/1.1

X-UA-Compatible: IE=Edge,chrome=1

BDPAGETYPE: 1

BDQID: 0xfc0fed0200000131

BDUSERID: 0

 

linux@ubuntu:~/http/testcurl$ curl -sI www.baidu.com| awk -F': ' '/Date: /  {print $2}

> ^C

linux@ubuntu:~/http/testcurl$ curl -sI www.baidu.com| awk -F': ' '/Date: /  {print $2}'

Wed, 26 Aug 2015 05:49:31 GMT




 

       MT 7688 Openwrt 中完全可以使用这种方式进行网络时间的同步!

1 0