# Wake On Lan Before Connection To Remote Desktop - MikroTik Script RouterOS

    If you have a PC at home and you need to connect to it via RDP at random times, but don't want to spend money on electricity needed to keep the PC powered on all day long, then this solution is what you are looking for.

    First, note my configuration:

    First i check if there are any new connections to my forwarded RDP port:

    /ip firewall nat add action=add-dst-to-address-list address-list=RDP250 chain=dstnat comment=HTPC dst-address=MY-PUBLIC-IP dst-port=MY-RDP-PORT in-interface=ether10 protocol=tcp to-addresses=10.0.0.250 to-ports=3389
    Then, i made 'Hairpin' NAT so i can reach my PC via my hostname from any location (internal network and the internet):

    /ip firewall nat add action=dst-nat chain=dstnat comment=HTPC dst-address=MY-PUBLIC-IP dst-port=MY-RDP-PORT in-interface=ether10 protocol=tcp to-addresses=10.0.0.250 to-ports=3389
    /ip firewall nat add action=masquerade chain=srcnat comment="HTPC - Hairpin" dst-address=10.0.0.250 dst-port=MY-RDP-PORT out-interface=bridge1 protocol=tcp src-address=10.0.0.0/24
    So, when I tryed to access the PC via RDP, Mikrotik added my public IP to an address list and then NATed me in. But what if the PC is in standby?

    I have created this litle script to check if there any IP's on the RDP250 address list:

    :foreach A in=[/ip firewall address-list find name=RDP250] do={
      if ([/ip firewall address-list get $A list]="RDP250") do={
        :log info "Sending WoL to $[/ip firewall address-list get $A address]"
        /tool wol mac=PC-MAC-ADDRESS interface=bridge1
        :log info "Removing IP $[/ip firewall address-list get $A address] from list"
        /ip firewall address-list remove $A
      }
    }
    And a scheduler to run this script every 10 seconds.

    In 99% of the cases, the RDP client connected on the first try, within 5 seconds. The worst case scenario is that you will have to try to connect twice.

    Note that the script is rather small and simple and will not impact the CPU unless you have a large number of addresses on the list. So be careful when doing this. The alternative is to connect to the router and send WoL packet manualy.

    Note that I don't have a static IP on my WAN interface. Instead, i update the IP via my already active ChangeIP scheduler with this extra line of code:

    /ip firewall nat set [/ip firewall nat find comment=HTPC] dst-address=[:pick $ddnsip 0 [:find $ddnsip "/"] ]
    The full article on ChangeIP is here http://wiki.mikrotik.com/wiki/Dynamic_DNS_Update_Script_for_ChangeIP.com

    Original article is http://www.plecko.com.hr/?permalink_static=123

    Credit: https://wiki.mikrotik.com/wiki/Wake_on_Lan_before_connection_to_Remote_Desktop