检索 NTP 服务器状态
Retrieve NTP server status
我正在编写脚本,脚本需要知道 NTP 服务器是否可访问,如果不可访问,将打印出一条错误消息。
我是运行Ubuntu,我需要完成这个任务而不需要下载任何第三方库等
我遇到了 ntpq。我有点通过 运行 以下命令得到我想要的结果:
ntpq -p 8.8.8.8
我希望我可以使用退出代码来确定命令的状态,但不幸的是,ntpq 似乎没有任何退出代码,因为它总是 returns 0.
有什么方法可以完成我想做的事情吗?
根据 Ubuntu documentation for ntpq
One of the following exit values will be returned:
0 (EXIT_SUCCESS)
Successful program execution.
1 (EXIT_FAILURE)
The operation failed or the command syntax was not valid.
但是,ntpq
用于管理您控制的 NTP 服务器。测试其他 NTP 服务器可访问性的更好选择是使用 ntpdate
及其 -q
选项到
Query only - don't set the clock.
示例:
$ ntpdate -q example.org
server 93.184.216.34, stratum 0, offset 0.000000, delay 0.00000
21 Jan 17:53:00 ntpdate[11652]: no server suitable for synchronization found
$ echo $?
1
$ ntpdate -q 0.ubuntu.pool.ntp.wrong
Error resolving 0.ubuntu.pool.ntp.wrong: Name or service not known (-2)
21 Jan 17:52:11 ntpdate[11650]: Can't find host 0.ubuntu.pool.ntp.wrong: Name or service not known (-2)
21 Jan 17:52:11 ntpdate[11650]: no servers can be used, exiting
$ echo $?
1
$ ntpdate -q 0.ubuntu.pool.ntp.org
server 46.4.99.122, stratum 2, offset 0.000625, delay 0.02603
server 62.138.205.79, stratum 2, offset 0.002026, delay 0.03394
server 94.130.184.193, stratum 2, offset -0.000003, delay 0.02867
server 193.30.120.245, stratum 2, offset 0.000738, delay 0.03133
21 Jan 17:52:01 ntpdate[11649]: adjust time server 46.4.99.122 offset 0.000625 sec
$ echo $?
0
我正在编写脚本,脚本需要知道 NTP 服务器是否可访问,如果不可访问,将打印出一条错误消息。
我是运行Ubuntu,我需要完成这个任务而不需要下载任何第三方库等
我遇到了 ntpq。我有点通过 运行 以下命令得到我想要的结果:
ntpq -p 8.8.8.8
我希望我可以使用退出代码来确定命令的状态,但不幸的是,ntpq 似乎没有任何退出代码,因为它总是 returns 0.
有什么方法可以完成我想做的事情吗?
根据 Ubuntu documentation for ntpq
One of the following exit values will be returned:
0 (EXIT_SUCCESS) Successful program execution. 1 (EXIT_FAILURE) The operation failed or the command syntax was not valid.
但是,ntpq
用于管理您控制的 NTP 服务器。测试其他 NTP 服务器可访问性的更好选择是使用 ntpdate
及其 -q
选项到
Query only - don't set the clock.
示例:
$ ntpdate -q example.org
server 93.184.216.34, stratum 0, offset 0.000000, delay 0.00000
21 Jan 17:53:00 ntpdate[11652]: no server suitable for synchronization found
$ echo $?
1
$ ntpdate -q 0.ubuntu.pool.ntp.wrong
Error resolving 0.ubuntu.pool.ntp.wrong: Name or service not known (-2)
21 Jan 17:52:11 ntpdate[11650]: Can't find host 0.ubuntu.pool.ntp.wrong: Name or service not known (-2)
21 Jan 17:52:11 ntpdate[11650]: no servers can be used, exiting
$ echo $?
1
$ ntpdate -q 0.ubuntu.pool.ntp.org
server 46.4.99.122, stratum 2, offset 0.000625, delay 0.02603
server 62.138.205.79, stratum 2, offset 0.002026, delay 0.03394
server 94.130.184.193, stratum 2, offset -0.000003, delay 0.02867
server 193.30.120.245, stratum 2, offset 0.000738, delay 0.03133
21 Jan 17:52:01 ntpdate[11649]: adjust time server 46.4.99.122 offset 0.000625 sec
$ echo $?
0