Files
SnarfCode/scripts/test_ntp_clients.py

75 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""
Test NTP client configuration on 3 sample hosts:
- aerodrome-wrk02 (140.44.4.22) - Aerodrome Debian worker
- spaceport-ctl01 (140.44.4.41) - Spaceport DietPi master
- shipyard-swm01 (140.44.4.71) - Shipyard RPi OS Docker Swarm
"""
import os
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8")
PRIV_KEY_PATH = os.path.expanduser("~/.ssh/id_ed25519_snarfnet")
TIMESYNCD_CONF = """[Time]
NTP=140.44.4.11 14.4.4.44
FallbackNTP=time.cloudflare.com time.google.com 0.us.pool.ntp.org 1.us.pool.ntp.org
PollIntervalMinSec=32
PollIntervalMaxSec=512
"""
TEST_HOSTS = [
("aerodrome-wrk02", "140.44.4.22", "root"),
("spaceport-ctl01", "140.44.4.41", "root"),
("shipyard-swm01", "140.44.4.71", "snarf"),
]
def main():
pkey = paramiko.Ed25519Key.from_private_key_file(PRIV_KEY_PATH)
for name, ip, user in TEST_HOSTS:
print(f"\n==================================================")
print(f"Configuring NTP Client on {name} [{ip}] as {user}...")
print(f"==================================================")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(ip, username=user, pkey=pkey, timeout=10)
cmd = f"""
if [ "$(id -u)" -ne 0 ]; then SUDO="sudo"; else SUDO=""; fi
# If DietPi, set CONFIG_NTP_MODE=4
if [ -f /boot/dietpi.txt ]; then
$SUDO sed -i 's/^[[:blank:]]*CONFIG_NTP_MODE=.*/CONFIG_NTP_MODE=4/' /boot/dietpi.txt
fi
# Configure systemd-timesyncd
$SUDO mkdir -p /etc/systemd
$SUDO tee /etc/systemd/timesyncd.conf << 'EOF' > /dev/null
{TIMESYNCD_CONF}
EOF
$SUDO systemctl unmask systemd-timesyncd 2>/dev/null || true
$SUDO systemctl enable systemd-timesyncd 2>/dev/null || true
$SUDO systemctl restart systemd-timesyncd
$SUDO timedatectl set-ntp true 2>/dev/null || true
sleep 2
echo "--- timedatectl timesync-status or show-timesync ---"
timedatectl timesync-status 2>&1 || timedatectl show-timesync 2>&1 || timedatectl status 2>&1
echo "--- Active NTP server ---"
systemctl status systemd-timesyncd --no-pager | grep -iE 'status|synchronized|contacted' || true
"""
stdin, stdout, stderr = client.exec_command(cmd)
out = stdout.read().decode('utf-8', errors='replace')
print(out)
except Exception as e:
print(f"Error on {name}: {e}")
finally:
client.close()
if __name__ == "__main__":
main()