WOL a PC from sleep mode and run a program when its up Script

Here is a Scritp script that wakes a computer from sleep mode, waits until it have turned on by pinging it, and runs a program when its up.

Windows

You must download wolcmd before using this script, and place the wolcmd.exe file in the same folder where the script bat file is placed.

WOLCMD offical Page

Save the code as a bat file, for example script.bat using notepad, and edit the following variables:

@echo off::You can change the follwing varibles, but don't add spaces before or afterset macaddress=123456789ABCset ipaddress=192.168.2.20set subnetmask=255.255.255.0set wolport=9 set exit_timer=0set program_to_start_path=notepad.exe :network_connection(ipconfig | find "IPv4") || goto no_network :wakewolcmd %macaddress% %ipaddress% %subnetmask% %wolport%echo Waiting for computer to wake up:echo(:doping(ping %ipaddress% -n 1 | find "TTL=") || goto wakeecho(echo ============================================echo Target (%ipaddres%) is up!!!echo ============================================goto end:no_networkecho There is no network connection! :endTIMEOUT /T %exit_timer% /NOBREAKstart "" "%program_to_start_path%"

Linux

The variables you need to change on the Linux bash script are similar, however you need to write your own commands at the end of the script.

Also, you need to install "wakeonlan" before running this script, by entering the following command in the terminal:

sudo apt-get install wakeonlan


#!/bin/bash
#here are the varibles you need to change

ipaddress=192.168.2.20
subnetmask=192.168.2.255
macaddress=a0:d3:c1:47:12:b4
wolport=9

#start of the script
echo Trying to wake $ipaddress
host_up=0
wakeonlan -i $subnetmask -p $wolport $macaddress 
sleep 1
while (($host_up == 0))
do
  if ping -c 1 $ipaddress &> /dev/null
 then
  echo $ipaddress is up!
  host_up=1
 else
  wakeonlan -i $subnetmask -p $wolport $macaddress 
  echo -n "."
  sleep 1
 fi

done

if (($host_up == 1)); then
 #enter you commands here 
 echo "Hello world"
fi