MattHiggs Posted May 2, 2017 Share Posted May 2, 2017 Hey all. So I was goofing off on the internet when I found this really interesting web page that had a downloadable Wake on Lan Com Object that could be used in scripting. Has very simple instructions on how to register the dll, how to create the object in a script, and the different, and the different properties associated with the object. https://www.depicus.com/wake-on-lan/wake-on-lan-com Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted May 2, 2017 Moderators Share Posted May 2, 2017 Moved to a more appropriate forum as it is not an AutoIt example "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
Gianni Posted May 3, 2017 Share Posted May 3, 2017 ..... If you prefer not to have to register third part's dll on clients around, you can use this little script instead that it works very well without any registration. MattHiggs and argumentum 2 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
MattHiggs Posted May 4, 2017 Author Share Posted May 4, 2017 On 5/3/2017 at 6:41 AM, Chimp said: ..... If you prefer not to have to register third part's dll on clients around, you can use this little script instead that it works very well without any registration. Very interesting sir. I will have to add that to the collection. However, I don't really have any use for Wake on LAN at the moment. There was a time in which I developed a solution for a previous employer that utilized Wake On LAN, but I found that there were too many points of failure (With just wake on lan in general) for it to be of any use. Incorporated Nirsoft's WakeMeOnLan tool, which was able to scan a LAN, obtain all relevant information needed for WOL from all clients that are on the LAN, save them to config file for future use, send WOL packets to client devices based on info pulled from config file, and best of all, had a command line interface as well as GUI. However, even with a tool as useful as that, while I was testing the tool and doing research into WOL in general, the following limitations came to light (please feel free to correct/add limitations that are either not accurate or missing): 1. Motherboard has to support WOL 2. Disabled by default in most systems built currently which meet the first condition 3. Can be (and probably is by default) disabled for the network adapter which will be receiving the WOL packets and would need to be configured within the network adapter advanced settings. 4. Even if somehow all of the above conditions were met, and the device was perfectly configured for WOL, my project was doomed to failure due to the fact that WOL can only be successful when the device is properly shutdown. For example, if an office were to lose power and the machines powered off as a result (which was the scenario I was developing the solution for in the first place), WOL would not be able to power on the machines whenever power was restored. From what I recall, this is due to the fact that, when WOL is configured correctly, the computer will keep providing power to the NIC and keep it in a state in which it is listening for WOL packets. So all in all, WOL is useless unless the devices and the scenario in which it is being utilized allow it to work. WOL is a technology that will be improved upon in the future, as its benefits are undeniable, but its limitations make its implementation not worth the time. What are others thoughts on this? Link to comment Share on other sites More sharing options...
Gianni Posted May 4, 2017 Share Posted May 4, 2017 On the tests I did I saw that activating the "WOL" parameter in the bios, and running the script from a client of the same LAN is enough to turn on the target client. Here is a little modification on the original script, so that it's enought to pass only the MAC address of the target client to be turned on. I've inserted 2 functions: one to get the SubnetMask and another to get the broadcast address so that it works on any LAN without the need to calculate those 2 lan parameters. If you need to turn on a client of a remote LAN instead, then you can use the PaExec program ( a better and free version of psexec) to run this wol executable on a client of that remote LAN (this is the reason to compile this script as command line exe) expandcollapse popup; Wake On Lan ; http://www.autoitscript.com/forum/topic/29772-copyright-free-wake-on-lan-script/page__hl__%20lan%20%20scanner ; This script must be compiled in "command line mode" ; It must be run with just one parameter, that is the MAC address of the client on this LAN to be turned On. ; Since you can turn On only clients of the same LAN, this script can automatically retrieve the other 2 required ; parameters: the SubnetMask and the BroadcastAddress from the client where it runs. ; (Same LAN = same SubnetMask and same Broadcast address for all the LAN and all clients belonging to it) ; to run this exe on a remote client of another LAN you can use the PaExec tool (a better and free version of PsExec) ; get PaExec from here: -> https://www.poweradmin.com/paexec/ ; #AutoIt3Wrapper_Change2CUI=y If $CmdLine[0] Then Local $MACAddress = $CmdLine[1] ; get mac address from the first (and the only) command line parameter $MACAddress = StringReplace(StringReplace($MACAddress, ":", ""), "-", "") ; remove ":" or "-" if any Else ConsoleWriteError("Error: missing MAC address") Exit EndIf Local $IPAddress = @IPAddress1 Local $sSubNet = _GetLocalMask() Local $sBroadcast = _GetLocalBroadcast($IPAddress, $sSubNet) UDPStartup() $connexion = UDPOpen($sBroadcast, 7) $res = UDPSend($connexion, GenerateMagicPacket($MACAddress)) ; MsgBox(0, "", $res) ConsoleWrite("The magic packet was fired." & @CRLF) UDPCloseSocket($connexion) UDPShutdown() ; =================================================================== ; Functions ; =================================================================== ; This function convert a MAC Address Byte (e.g. "1f") to a char Func HexToChar($strHex) Return Chr(Dec($strHex)) EndFunc ;==>HexToChar ; This function generate the "Magic Packet" Func GenerateMagicPacket($strMACAddress) $MagicPacket = "" $MACData = "" For $p = 1 To 11 Step 2 $MACData = $MACData & HexToChar(StringMid($strMACAddress, $p, 2)) Next For $p = 1 To 6 $MagicPacket = HexToChar("ff") & $MagicPacket Next For $p = 1 To 16 $MagicPacket = $MagicPacket & $MACData Next Return $MagicPacket EndFunc ;==>GenerateMagicPacket ; This function retrieve the Subnet Mask of this LAN Func _GetLocalMask() Local $IP = @IPAddress1, $Text = "", $Subnet = "" $PID = Run("ipconfig", "", @SW_HIDE, 2) Do Sleep(10) $Text &= StdoutRead($PID) Until @error Local $aText = StringSplit($Text, @CRLF, 1) For $i = 1 To $aText[0] If StringInStr($aText[$i], $IP) Then $Subnet = StringMid($aText[$i + 1], StringInStr($aText[$i + 1], ":") + 2) ExitLoop EndIf Next Return $Subnet EndFunc ;==>_GetLocalMask ; This function retrieve the Broadcast address of this LAN Func _GetLocalBroadcast($sIP, $sMask) Local $aIPAddress = StringSplit($sIP, '.'), $aSubnetMask = StringSplit($sMask, '.') $sIP = '' For $i = 1 To $aIPAddress[0] $aIPAddress[$i] = BitOR($aIPAddress[$i], BitNOT($aSubnetMask[$i] - 256)) $sIP &= $aIPAddress[$i] & '.' Next Return StringTrimRight($sIP, 1) EndFunc ;==>_GetLocalBroadcast coffeeturtle 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
MattHiggs Posted May 8, 2017 Author Share Posted May 8, 2017 (edited) On 5/4/2017 at 2:58 PM, Chimp said: On the tests I did I saw that activating the "WOL" parameter in the bios, and running the script from a client of the same LAN is enough to turn on the target client. Here is a little modification on the original script, so that it's enought to pass only the MAC address of the target client to be turned on. I've inserted 2 functions: one to get the SubnetMask and another to get the broadcast address so that it works on any LAN without the need to calculate those 2 lan parameters. If you need to turn on a client of a remote LAN instead, then you can use the PaExec program ( a better and free version of psexec) to run this wol executable on a client of that remote LAN (this is the reason to compile this script as command line exe) expandcollapse popup; Wake On Lan ; http://www.autoitscript.com/forum/topic/29772-copyright-free-wake-on-lan-script/page__hl__%20lan%20%20scanner ; This script must be compiled in "command line mode" ; It must be run with just one parameter, that is the MAC address of the client on this LAN to be turned On. ; Since you can turn On only clients of the same LAN, this script can automatically retrieve the other 2 required ; parameters: the SubnetMask and the BroadcastAddress from the client where it runs. ; (Same LAN = same SubnetMask and same Broadcast address for all the LAN and all clients belonging to it) ; to run this exe on a remote client of another LAN you can use the PaExec tool (a better and free version of PsExec) ; get PaExec from here: -> https://www.poweradmin.com/paexec/ ; #AutoIt3Wrapper_Change2CUI=y If $CmdLine[0] Then Local $MACAddress = $CmdLine[1] ; get mac address from the first (and the only) command line parameter $MACAddress = StringReplace(StringReplace($MACAddress, ":", ""), "-", "") ; remove ":" or "-" if any Else ConsoleWriteError("Error: missing MAC address") Exit EndIf Local $IPAddress = @IPAddress1 Local $sSubNet = _GetLocalMask() Local $sBroadcast = _GetLocalBroadcast($IPAddress, $sSubNet) UDPStartup() $connexion = UDPOpen($sBroadcast, 7) $res = UDPSend($connexion, GenerateMagicPacket($MACAddress)) ; MsgBox(0, "", $res) ConsoleWrite("The magic packet was fired." & @CRLF) UDPCloseSocket($connexion) UDPShutdown() ; =================================================================== ; Functions ; =================================================================== ; This function convert a MAC Address Byte (e.g. "1f") to a char Func HexToChar($strHex) Return Chr(Dec($strHex)) EndFunc ;==>HexToChar ; This function generate the "Magic Packet" Func GenerateMagicPacket($strMACAddress) $MagicPacket = "" $MACData = "" For $p = 1 To 11 Step 2 $MACData = $MACData & HexToChar(StringMid($strMACAddress, $p, 2)) Next For $p = 1 To 6 $MagicPacket = HexToChar("ff") & $MagicPacket Next For $p = 1 To 16 $MagicPacket = $MagicPacket & $MACData Next Return $MagicPacket EndFunc ;==>GenerateMagicPacket ; This function retrieve the Subnet Mask of this LAN Func _GetLocalMask() Local $IP = @IPAddress1, $Text = "", $Subnet = "" $PID = Run("ipconfig", "", @SW_HIDE, 2) Do Sleep(10) $Text &= StdoutRead($PID) Until @error Local $aText = StringSplit($Text, @CRLF, 1) For $i = 1 To $aText[0] If StringInStr($aText[$i], $IP) Then $Subnet = StringMid($aText[$i + 1], StringInStr($aText[$i + 1], ":") + 2) ExitLoop EndIf Next Return $Subnet EndFunc ;==>_GetLocalMask ; This function retrieve the Broadcast address of this LAN Func _GetLocalBroadcast($sIP, $sMask) Local $aIPAddress = StringSplit($sIP, '.'), $aSubnetMask = StringSplit($sMask, '.') $sIP = '' For $i = 1 To $aIPAddress[0] $aIPAddress[$i] = BitOR($aIPAddress[$i], BitNOT($aSubnetMask[$i] - 256)) $sIP &= $aIPAddress[$i] & '.' Next Return StringTrimRight($sIP, 1) EndFunc ;==>_GetLocalBroadcast Interesting script, @Chimp . However, concerning your explanation as to waking up clients on Remote LANs by using paexec, since paexec performs the same function as psexec, I am going to refer to both as the same tool. I was under the impression that paexec only worked within the LAN in which it was run as well, or at least the local environment in which it is run. In order to use paexec to run commands on a remote LAN, you would need something that is connecting you to that LAN, like a VPN, wouldn't you? But if you have a VPN connecting you to a remote LAN, then would you really need paexec? Or are you referring to multiple subnets within the same network? * EDIT* Also, this is odd, but I registered the COM dll and instead tried to create a powershell script in which a WOL COM object was created in order to let me explore all the features it has to offer. But when I run New-Object -ComObject "WolCom.Wol" the powershell console replies with: New-Object : Retrieving the COM class factory for component with CLSID {9138EF1B-72FF-48E3-BEFD-BBB1F58EC2D3} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). But it is registered, as I verified the COM object was in the registry under HKLM\Software\classes. Any ideas? Edited May 8, 2017 by MattHiggs Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now