{"id":83,"date":"2011-04-14T17:58:28","date_gmt":"2011-04-14T21:58:28","guid":{"rendered":"http:\/\/griffinscs.wordpress.com\/?p=83"},"modified":"2016-11-21T17:19:30","modified_gmt":"2016-11-21T17:19:30","slug":"wake-on-lan-with-ironpython","status":"publish","type":"post","link":"https:\/\/brainslug.azurewebsites.net\/?p=83","title":{"rendered":"Wake on Lan with IronPython"},"content":{"rendered":"<p>Since introducing a custom built Media Center PC to my living room configuration back in 2005, the machine filling the role has also become a local storage server of sorts.\u00a0 The music functions and storage requirements alone make it an appealing location for centralized storage.\u00a0 Of course it's not always on, nor would we really want it to be, so we would need an easy way to wake it up from workstations, phones, and laptops on the LAN.<\/p>\n<p>Enter Wake-on-Lan.\u00a0 It wasn't difficult to figure this out. In fact my Xbox, which we were using as an extender in the bedroom, was already using it.\u00a0 With that already working, there was no configuration to be done on the Media Center PC itself (I'll cover it anyway).\u00a0 Writing a WOL.exe to form the \"Magic Packet\" and send it was easy, setting up shortcuts on my wife's and my own systems with the right command arguments to include the port and the Media Center's MAC address was even easier.<\/p>\n<p>In <a href=\"\/blog\/?p=83\">my last post<\/a> I discussed Windows scripting, and it seemed like something like this was a good candidate for an example.\u00a0 Plus, back when I wrote that WOL.exe, it seemed a little heavy handed, but I still loved the simplicity of the code itself and wanted to share it.<\/p>\n<p><strong>Set up the Host<\/strong><a href=\"\/wp-content\/uploads\/2016\/11\/wol_adapter1.png\"><img loading=\"lazy\" class=\"alignright size-full wp-image-85\" title=\"WOL Magic Packet Setting\" src=\"\/wp-content\/uploads\/2016\/11\/wol_adapter1.png\" alt=\"\" width=\"414\" height=\"479\" \/><\/a><\/p>\n<p>As I said, I didn't have to mess with this, but you will need to make sure that your host's network adapter is ready to receive a Wake-on-Lan. Go to device manager, open up your favorite network adapter, and make sure the setting to the right is enabled.<\/p>\n<p>While you're at it, get the MAC address of the adapter.\u00a0 You can do an \"ipconfig \/all\" in the command prompt and it will be listed as \"Physical Address\".\u00a0 Write this down for later.<\/p>\n<p>Some systems will require that you modify settings in the BIOS to support Wake-on-Lan.\u00a0 I'm not going to cover it, since BIOS menus vary, but this was another thing that I happily didn't need to do.<\/p>\n<p><strong>The Script<\/strong><br \/>\nPull out the MAC address you recorded from the host machine and place each hex digit in the byte array, WAKE_MAC_ADDRESS<strong>.<br \/>\n<\/strong><\/p>\n<pre class=\"brush: python; \">\nimport clr\r\nclr.AddReference(&#039;System&#039;)\r\nclr.AddReference(&#039;System.Core&#039;)\r\nfrom System import *\r\nfrom System.Net.Sockets import *\r\nfrom System.Net import *\r\nfrom System.Linq import *\r\n\r\nWAKE_MAC_ADDRESS = Array[Byte]([0x0A,0x0D,0x0D,0x04,0x0E,0x55])\r\nPORT = 40000\r\n\r\n#fill the first 6 bytes of the packet with 0xFF\r\nmagicPacket = Enumerable.Repeat[Byte](0xff, 6);\r\n\r\n#repeat the MAC address 16 times\r\nfor i in range(16):\r\nmagicPacket = Enumerable.Concat(magicPacket, WAKE_MAC_ADDRESS)\r\n\r\n#send a UDP boradcast of the magic packet\r\nsocket = Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)\r\nbroadcastEndpoint = IPEndPoint(IPAddress.Broadcast, PORT)\r\nsocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, True)\r\nsocket.SendTo(Enumerable.ToArray(magicPacket), broadcastEndpoint)\r\nsocket.Close()\r\n\r\n\n<\/pre>\n<p><strong>Shortcut<\/strong><\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/11\/wol11.png\"><img loading=\"lazy\" class=\"alignleft size-full wp-image-92\" title=\"WOL Shortcut\" src=\"\/wp-content\/uploads\/2016\/11\/wol11.png\" alt=\"\" width=\"386\" height=\"533\" \/><\/a>What I did at this point was to make a shortcut called \"Wake Emcee\" (Emcee being the name of my Media Center PC), and pointed it to <a href=\"\/blog\/?p=83\">ScriptShell<\/a> using the -a option so it silently executes the script and exits.\u00a0 Shell32.dll has the perfect icon for this.\u00a0 It will be the default location searched, if you click \"Change Icon...\".<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since introducing a custom built Media Center PC to my living room configuration back in 2005, the machine filling the role has also become a local storage server of sorts.\u00a0 The music functions and storage requirements alone make it an appealing location for centralized storage.\u00a0 Of course it&#8217;s not always on, nor would we really [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,1],"tags":[6,9,14,27,28,40,45,46,56],"_links":{"self":[{"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts\/83"}],"collection":[{"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=83"}],"version-history":[{"count":2,"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":685,"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions\/685"}],"wp:attachment":[{"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brainslug.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}