How to ping a network host at regular intervals?
The Macbook was locked with Amphetamine running, the lid was closed but an external display was connected through a USB-C hub with HDMI output. I do not believe the Macbook can go to sleep under these circumstances.

I wouldn't be so sure. Just check the output of pmset -g log | grep &quot;Display is turned off&quot; - if it happened when the ping got interrupted, you Mac went to sleep. I find Macbooks particularly stubborn when the charger is disconnected.
Also note that if the external display was turned off, or switched to another input, this can also be sometimes detected (depends on model/connection, I assume), so this could also trigger the sleep.
There's no magic switch to ping to issue  echo requests regularly - it does that by default.