繁体   English   中英

在批处理脚本中调用URL并将输出保存到变量或文本文件?

[英]Call URL In Batch Script and save output to variable or text file?

我正在使用一个批处理文件来检查mysql站点的状态。 现在在网站上一切都很好,因此当我连接到它时,它会表示User is OnlineOffline 需要一个批处理文件来调用url 并将页面输出保存到文本文件中 这篇文章不需要保存输出就可以执行我的要求: 不使用浏览器从批处理文件中打开URL 只要可以通过批处理文件调用它,它就可以是vbscript。 谢谢一群!

上一篇文章的原始脚本:

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************

    setlocal enableextensions disabledelayedexpansion

    rem Batch file will delegate all the work to the script engine 
    if not "%~1"=="" (
        wscript //E:JScript "%~dpnx0" %1
    )

    rem End of batch area. Ensure batch ends execution before reaching
    rem javascript zone
    exit /b

@end
// **** Javascript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('Msxml2.XMLHTTP.6.0');

// Retrieve the url parameter
var url = WScript.Arguments.Item(0)

    // Make the request

    http.open("GET", url, false);
    http.send();

    // All done. Exit
    WScript.Quit(0);

如果您不介意从批处理中使用Powershell:

:: WebResponse.cmd
@Echo off
For /f "delims=" %%A in (
  'powershell -NonI -NoP -C "(Invoke-Webrequest "%~1").content"'
) Do Echo one line of Webresponse %%A

带有URL的示例输出可获取您当前的外部IP

> WebResponse.cmd "http://api.ipify.org"
one line of Webresponse 93.226.203.xxx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM