You know how sometimes the answer to the question you ask is not necessarily the answer you need? I have a vague suspicion this might be one of those times.
If this is a Windows machine you're trying to reboot, you can reboot it remotely without needing to use a CGI script served by the remote host. If the account you're logged in with on the triggering PC also has privileges on the remote PC, you can trigger the reboot with the shutdown
command.
shutdown /m \\remotePC /r /t 0
Do shutdown /?
in a cmd console for more info. Or if you must authenticate, you can use wmic
instead.
wmic /node:remotePC /user:remotePCadmin /password:remotePCpass process call create "shutdown -r -t 0"
In case I was mistaken, here's the answer to the question you asked. The fastest way to execute a remote CGI script would be to use an XMLHTTPRequest with Windows Script Host (VBScript or JScript). Here's an example.
@if (@CodeSection == @Batch) @then
@echo off & setlocal
set "URL=http://192.168.1.100/cgi-bin/reboot"
cscript /nologo /e:jscript "%~f0" "%URL%"
goto :EOF
@end // end batch / begin JScript chimera
var x = WSH.CreateObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0),true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState != 4) WSH.Sleep(50);
For what it's worth, you can also parse x.responseText
as needed. You can scrape it as flat text, or even evaluate it as a hierarchical DOM object. This answer demonstrates such parsing. And if you just can't get enough, here are more examples.
If you'd rather have something simpler at the expense of efficiency, you can invoke a PowerShell command.
@echo off & setlocal
set "URL=http://192.168.1.100/cgi-bin/reboot"
powershell "ipmo BitsTransfer; Start-BitsTransfer \"%URL%\" \"%temp%\a\""
del /q "%temp%\a"
goto :EOF
You could probably alternatively use Invoke-WebRequest
to avoid the temporary file, but Invoke-WebRequest
requires PowerShell version 3 or newer. Start-BitsTransfer
works with PowerShell version 2, so it should work on more computers. One might also use the [System.Net]::WebRequest
.NET class, but it gets a little complicated constructing all the objects needed to proceed beyond fetching the HTTP headers to having the web server serve the web page. If you're curious, it looks something like this:
powershell "[void](new-object IO.StreamReader([Net.WebRequest]::Create(\"%URL%\").GetResponse().GetResponseStream())).ReadToEnd()"
Not exactly what I'd call simple. In hybrid format for easier readability:
<# : batch portion
@echo off & setlocal
set "URL=http://192.168.1.100/cgi-bin/reboot"
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin PowerShell hybrid chimera #>
$request = [Net.WebRequest]::Create($env:URL)
$response = $request.GetResponse()
$stream = $response.GetResponseStream()
$reader = new-object IO.StreamReader($stream)
[void]$reader.ReadToEnd()
In any case, any PowerShell solution will be a second or two slower than the JScript solution near the top of this answer. powershell.exe
takes a second or two to load (indeed, several seconds if it's not been loaded since Windows was last rebooted); whereas cscript.exe
fires nearly instantly.