Skip to content

Instantly share code, notes, and snippets.

@XavierTolza
Created October 9, 2025 20:39
Show Gist options
  • Save XavierTolza/d06d2565fd8ad4a4b60c1aa325710ffd to your computer and use it in GitHub Desktop.
Save XavierTolza/d06d2565fd8ad4a4b60c1aa325710ffd to your computer and use it in GitHub Desktop.
Wake-on-LAN automatique sur Linux

Wake-on-LAN automatique sur Linux (Wacom LAN)

Ce Gist contient tout ce qu’il faut pour activer automatiquement le Wake-on-LAN sur une interface réseau Linux (ex: eno1) au démarrage, avec :

  • Script Bash de surveillance et activation automatique
  • Service systemd pour lancement au boot
  • Logs dans /tmp/wol-monitor.log et dans journalctl
  • Limite de tentatives et vérification des privilèges root

1️⃣ Script Bash /usr/local/bin/wol-monitor.sh

#!/bin/bash
#
# Script de surveillance et d’activation du Wake-on-LAN (WOL)
# Nécessite les droits root.
# Journalisation : /tmp/wol-monitor.log + journalctl.

INTERFACE="eno1"          # Modifier si besoin
ETH_TOOL="/usr/sbin/ethtool"
SLEEP_INTERVAL=5          # secondes entre les tentatives
MAX_ATTEMPTS=10           # nombre maximum de tentatives
LOG_FILE="/tmp/wol-monitor.log"

log() {
    local MSG="[WOL] $1"
    echo "$(date '+%Y-%m-%d %H:%M:%S') $MSG" | tee -a "$LOG_FILE"
    logger -t wol-monitor "$MSG"
}

# Vérification des privilèges root
if [[ $EUID -ne 0 ]]; then
    echo "Erreur : ce script doit être exécuté en tant que super utilisateur (root)." >&2
    exit 1
fi

# Initialisation
echo "" > "$LOG_FILE"
log "Démarrage du service WOL pour l'interface ${INTERFACE}"
log "Intervalle: ${SLEEP_INTERVAL}s | Tentatives max: ${MAX_ATTEMPTS}"
log "Le journal est enregistré dans $LOG_FILE"

# Boucle principale
ATTEMPT=1
while (( ATTEMPT <= MAX_ATTEMPTS )); do
    CURRENT_STATE=$($ETH_TOOL $INTERFACE 2>/dev/null | grep "Wake-on" | awk '{print $2}')
    if [[ "$CURRENT_STATE" == "g" ]]; then
        log "✅ Wake-on-LAN activé sur ${INTERFACE}. Fin du script."
        exit 0
    else
        log "Tentative ${ATTEMPT}/${MAX_ATTEMPTS}: Wake-on-LAN non activé (état: ${CURRENT_STATE:-inconnu})."
        log "→ Tentative d’activation..."
        $ETH_TOOL -s $INTERFACE wol g 2>&1 | tee -a "$LOG_FILE" | logger -t wol-monitor
    fi
    ((ATTEMPT++))
    sleep $SLEEP_INTERVAL
done

log "❌ Échec : impossible d’activer le Wake-on-LAN sur ${INTERFACE} après ${MAX_ATTEMPTS} tentatives."
exit 2

Rendre le script exécutable :

sudo chmod +x /usr/local/bin/wol-monitor.sh

2️⃣ Service systemd /etc/systemd/system/wol-monitor.service

[Unit]
Description=Surveillance et activation automatique du Wake-on-LAN (WOL)
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/wol-monitor.sh
Restart=on-failure
RestartSec=15
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

3️⃣ Activer le service

sudo systemctl daemon-reload
sudo systemctl enable wol-monitor.service
sudo systemctl start wol-monitor.service

4️⃣ Vérification et logs

Suivre les logs en temps réel :

journalctl -u wol-monitor.service -f

Consulter le log local :

cat /tmp/wol-monitor.log

Vérifier si WOL est activé :

sudo ethtool eno1 | grep Wake-on
# Devrait afficher: Wake-on: g

5️⃣ Personnalisation rapide

  • Modifier l’interface réseau : INTERFACE="eno1"
  • Modifier le nombre de tentatives : MAX_ATTEMPTS=10
  • Modifier l’intervalle entre tentatives : SLEEP_INTERVAL=5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment