bash 如何确定NTP通信状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23227775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:16:43  来源:igfitidea点击:

How to determine NTP communication status

linuxbashclientembedded-linuxntp

提问by Daniel Griscom

I'm working on an embedded OpenSUSE 12.3 system that lets the user configure the (single) NTP server address. I need to be able to give the user feedback on how things are going in terms of communicating with the specified server. Is the server address valid? Has NTP been unable to contact the server? Is it querying the server? Or, are we fully synchronized? (Note: once NTP thinks it's synchronized then I don't care just how synchronized it is.)

我正在开发一个嵌入式 OpenSUSE 12.3 系统,该系统允许用户配置(单个)NTP 服务器地址。我需要能够就与指定服务器通信的情况向用户提供反馈。服务器地址是否有效?NTP 是否无法联系服务器?它是在查询服务器吗?或者,我们完全同步了吗?(注意:一旦 NTP 认为它是同步的,那么我不在乎它是如何同步的。)

Right now I've written a bash script to parse the output of ntpq -np:

现在我已经编写了一个 bash 脚本来解析以下输出ntpq -np

  • If the output contains "ntpq: read: Connection refused"then ntpd isn't running
  • If the output contains "No association ID's returned"then the server address is invalid
  • If the output contains ".INIT."then ntpd hasn't yet connected to the server
  • If the output does NOT contain a line starting with "*"then we're querying the server
  • Otherwise we're synchronized.
  • 如果输出包含"ntpq: read: Connection refused"则 ntpd 未运行
  • 如果输出包含"No association ID's returned"则服务器地址无效
  • 如果输出包含".INIT."则 ntpd 尚未连接到服务器
  • 如果输出不包含以开头的行,"*"那么我们正在查询服务器
  • 否则我们是同步的。

Clearly, this is a pain in the rear and highly platform-dependent, and I'd love to have a better solution.

显然,这是一个很痛苦的问题,而且高度依赖平台,我很想有一个更好的解决方案。

I understand that there's a libntpq.a inside of a built ntp source tree that can be used by external applications, but until there's an official release then that's just switching to a new flavor of platform-dependence.

我知道在构建的 ntp 源代码树中有一个 libntpq.a 可供外部应用程序使用,但在正式发布之前,这只是切换到一种新的平台依赖性。

Is there an official way to determine the current communication status of an NTP client? Note again that only a single server will be configured.

有没有官方的方法来确定 NTP 客户端的当前通信状态?再次注意,只会配置一个服务器。

采纳答案by Ciano

How about ntpstat, would that work for you? Here are the exit/return values from the command when it's run:

ntpstat 怎么样,对你有用吗?以下是命令运行时的退出/返回值:

  • exit status 0 - Clock is synchronised.
  • exit status 1 - Clock is not synchronised.
  • exit status 2 - If clock state is indeterminant, for example if ntpd is not contactable
  • 退出状态 0 - 时钟已同步。
  • 退出状态 1 - 时钟未同步。
  • 退出状态 2 - 如果时钟状态不确定,例如如果 ntpd 不可联系

Check out the page Verify that my NTP is working

查看页面验证我的 NTP 是否正常工作

回答by dfc

Another option is to use the ntp-wait command. ntp-wait is normally used in startup scripts that need the system time to be synced before carrying on with other tasks. If you pass the verbose option to ntp-wait it will print out the status and or error if syncing fails. You can also tell ntp-wait how long to wait for the sync before returning an error.

另一种选择是使用 ntp-wait 命令。ntp-wait 通常用于启动脚本中,在执行其他任务之前需要同步系统时间。如果您将详细选项传递给 ntp-wait,它将在同步失败时打印出状态和/或错误。您还可以告诉 ntp-wait 在返回错误之前等待同步多长时间。

Unlike ntpstat ntp-wait is provided by the ntp reference implementation so relying on it is more portable than expecting ntpstat to be installed/available.

与 ntpstat 不同的是,ntp-wait 是由 ntp 参考实现提供的,因此依赖它比期望安装/可用的 ntpstat 更具可移植性。