From bf89ff198ca78bc32070839e4180c6686e069d44 Mon Sep 17 00:00:00 2001 From: sherlock2010 <15151851377@163.com> Date: Fri, 2 Aug 2024 02:53:45 +0000 Subject: [PATCH] try ntpq with both IPv4 and IPv6 addresses --- ...pq-with-both-IPv4-and-IPv6-addresses.patch | 50 +++++++++++++++++++ ntpstat.spec | 10 +++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 backport-try-ntpq-with-both-IPv4-and-IPv6-addresses.patch diff --git a/backport-try-ntpq-with-both-IPv4-and-IPv6-addresses.patch b/backport-try-ntpq-with-both-IPv4-and-IPv6-addresses.patch new file mode 100644 index 0000000..1ad832f --- /dev/null +++ b/backport-try-ntpq-with-both-IPv4-and-IPv6-addresses.patch @@ -0,0 +1,50 @@ +From 9fec1482a71231b25f1315dc55b315dc3e8dd21a Mon Sep 17 00:00:00 2001 +From: Miroslav Lichvar +Date: Thu, 8 Oct 2020 12:22:30 +0200 +Subject: [PATCH] try ntpq with both IPv4 and IPv6 addresses + +Don't rely on "localhost" resolving to the IPv4 or IPv6 address that +ntpd is listening on if it is not configured to listen on both. If +127.0.0.1 doesn't respond, try ::1. + +Conflict:NA +Reference:https://github.com/mlichvar/ntpstat/commit/9fec1482a71231b25f1315dc55b315dc3e8dd21a +--- + ntpstat | 12 ++++++++---- + 1 file changed, 8 insertions(+), 4 deletions(-) + +diff --git a/ntpstat b/ntpstat +index 0bdb82b..9c03245 100755 +--- a/ntpstat ++++ b/ntpstat +@@ -88,11 +88,15 @@ parse_rv_field() { + } + + get_ntpd_state() { +- local output syspeer_id disp delay ++ local host output syspeer_id disp delay + local leap source address stratum distance poll + +- output=$("${NTPQ[@]}" -c "rv 0" 2> /dev/null) || return 2 +- [[ $output == *"associd"*"status"* ]] || return 3 ++ for host in "127.0.0.1" "::1" "x"; do ++ [ "$host" == "x" ] && return 3 ++ ++ output=$("${NTPQ[@]}" -c "rv 0" "$host" 2> /dev/null) ++ [[ $output == *"associd"*"status"* ]] && break ++ done + + leap=$(parse_rv_field "$output" "leap") + source=$(parse_rv_field "$output" "status" | \ +@@ -117,7 +121,7 @@ get_ntpd_state() { + distance=$(echo "$delay $disp" | awk '{ printf "%.3f", $1 / 2.0 + $2 }') + + syspeer_id=$(parse_rv_field "$output" "peer") +- output=$("${NTPQ[@]}" -c "rv $syspeer_id" 2> /dev/null) || return 5 ++ output=$("${NTPQ[@]}" -c "rv $syspeer_id" "$host" 2> /dev/null) || return 5 + + if [ "$source" = "NTP server" ]; then + address=$(parse_rv_field "$output" "srcadr") +-- +2.33.0 + diff --git a/ntpstat.spec b/ntpstat.spec index 9ff82a3..dc3437f 100644 --- a/ntpstat.spec +++ b/ntpstat.spec @@ -1,12 +1,14 @@ Name: ntpstat Version: 0.6 -Release: 4 +Release: 5 Summary: Print NTP synchronisation status License: MIT URL: https://github.com/mlichvar/ntpstat Source0: https://github.com/mlichvar/ntpstat/archive/%{version}/%{name}-%{version}.tar.gz BuildArch: noarch +Patch0: backport-try-ntpq-with-both-IPv4-and-IPv6-addresses.patch + Requires: (ntp or chrony) %description @@ -43,6 +45,12 @@ install -p -m644 ./ntpstat.1 $RPM_BUILD_ROOT%{_mandir}/man1 %{_mandir}/man1/ntpstat.* %changelog +* Fri Aug 02 2024 zhouyihang - 0.6-5 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:try ntpq with both IPv4 and IPv6 addresses + * Thu Jun 16 2022 yanglu - 0.6-4 - DESC: delete macros in changelog -- Gitee