从命令行执行 URL 而无需在 Windows 中打开浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36947181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute a URL from a command line without opening a browser in windows
提问by Destroconut
So I'm putting together a batch file to run at startup that executes a small number of processes, one of them is to execute a reboot of a certain program at the end of the set of processes. I've been searching ways how to do this in the command line in windows but I need to be able to do this without opening a browser. What I need is to execute the reboot in the following url without opening a browser at all.
因此,我将一个批处理文件放在一起,以在启动时运行,该文件执行少量进程,其中之一是在一组进程结束时执行某个程序的重新启动。我一直在寻找如何在 Windows 的命令行中执行此操作的方法,但我需要能够在不打开浏览器的情况下执行此操作。我需要的是在以下 url 中执行重启,而根本不打开浏览器。
http://192.168.1.100/cgi-bin/reboot
Everything that I've tried has opened a new browser window. I don't want to have to download anything to get this going in windows if that's possible. Thanks for any suggestions.
我尝试过的一切都打开了一个新的浏览器窗口。如果可能的话,我不想下载任何东西来让它在 Windows 中运行。感谢您的任何建议。
回答by rojo
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.
如果这是您尝试重新启动的 Windows 机器,您可以远程重新启动它,而无需使用远程主机提供的 CGI 脚本。如果您在触发 PC 上登录的帐户在远程 PC 上也具有权限,则可以使用该shutdown
命令触发重新启动。
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.
shutdown /?
在 cmd 控制台中执行以获取更多信息。或者,如果您必须进行身份验证,则可以wmic
改用。
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.
如果我弄错了,这是您提出的问题的答案。执行远程 CGI 脚本的最快方法是使用带有 Windows 脚本宿主(VBScript 或 JScript)的 XMLHTTPRequest。这是一个例子。
@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 demonstratessuch parsing. And if you just can't get enough, here are more examples.
对于它的价值,您还可以x.responseText
根据需要进行解析。您可以将其抓取为纯文本,甚至可以将其评估为分层 DOM 对象。 这个答案演示了这种解析。如果您还不够,这里有更多示例。
If you'd rather have something simpler at the expense of efficiency, you can invoke a PowerShell command.
如果您希望以牺牲效率为代价获得更简单的东西,您可以调用 PowerShell 命令。
@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:
您可能还可以使用Invoke-WebRequest
避免临时文件,但Invoke-WebRequest
需要 PowerShell 版本 3 或更高版本。 Start-BitsTransfer
适用于 PowerShell 版本 2,因此它应该适用于更多计算机。也可以使用[System.Net]::WebRequest
.NET 类,但是构建所有需要的对象会变得有点复杂,这些对象从获取 HTTP 标头到让 Web 服务器为网页提供服务。如果你很好奇,它看起来像这样:
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.
在任何情况下,任何 PowerShell 解决方案都将比此答案顶部附近的 JScript 解决方案慢一两秒。 powershell.exe
加载需要一两秒钟(实际上,如果自 Windows 上次重新启动后尚未加载,则需要几秒钟);而cscript.exe
几乎立即开火。
回答by Rodrigo M
If you cannot download anything, then Windows PowerShell is the best option. You can call it from a PS script file, or directly from the command line in a batch file:
如果您无法下载任何内容,则 Windows PowerShell 是最佳选择。您可以从 PS 脚本文件中调用它,也可以直接从批处理文件中的命令行调用它:
powershell.exe -noprofile -command "Invoke-WebRequest -Uri http://192.168.1.100/cgi-bin/reboot"
You can also consider Curlfor that type of process. There is no equivalent in Windows, so it will require a download.
您也可以考虑 Curl用于该类型的过程。Windows 中没有等效项,因此需要下载。
curl http://192.168.1.100/cgi-bin/reboot
Curl will not open a browser window, and has great command line options (See curl -help
), and will return error codes for batch file error handling.
Curl 不会打开浏览器窗口,并且具有很好的命令行选项(请参阅 参考资料curl -help
),并且会返回错误代码以进行批处理文件错误处理。
回答by Eli Richardson
I don't think Windows comes with that feature.
我不认为 Windows 带有该功能。
Try making a VBscript that will open the browser Windows but will not display anything maybe.
尝试制作一个可以打开浏览器 Windows 但可能不会显示任何内容的 VBscript。
回答by Zimba
To process URL response direct, from Win CMD:
从 Win CMD 直接处理 URL 响应:
powershell "Invoke-WebRequest -Uri <url>"
To save server response to file:
将服务器响应保存到文件:
powershell "Invoke-WebRequest -Uri <url> -outfile <file.ext>"
.NET solution; from Win CMD:
.NET 解决方案;来自 Win CMD:
powershell "[System.Net.Webclient]::new().DownloadString(\"<URL>\")"