使用curl或wget工具同步某网站时间到本地环境。
使用curl工具
#!/bin/bash
# Replace 'example.com' with the domain you want to query
url="http://example.com"
# Fetch HTTP header and extract Date field
date_str=$(curl -sI "$url" | grep -i "^Date:" | sed 's/Date: //I')
if [ -n "$date_str" ]; then
# Set system date using the fetched date
sudo date -s "$date_str"
# Sync hardware clock with system time
sudo hwclock --systohc
echo "System time synchronized with $url and hardware clock updated"
else
echo "Failed to fetch date from $url"
fi
使用wget工具
#!/bin/bash
# Replace 'example.com' with the domain you want to query
url="http://example.com"
# Fetch HTTP header and extract Date field
date_str=$(wget --server-response -O /dev/null "$url" 2>&1 | grep -i "^ Date:" | sed 's/^ Date: //I')
if [ -n "$date_str" ]; then
# Set system date using the fetched date
sudo date -s "$date_str"
# Sync hardware clock with system time
sudo hwclock --systohc
echo "System time synchronized with $url and hardware clock updated"
else
echo "Failed to fetch date from $url"
fi