我有一個循環,我在其中逐一檢查文件中的 url,並希望獲取狀態標頭和每個請求的執行時間作為結果。我有兩個curl指令:
這個輸出標頭通訊(不需要)和最後是http狀態碼和時間 200 - 0,016
curl -w "%{http_code} - %{time_connect}" -I -s http://superuser.com >> test_result.txt
這個與來自標頭的 http 狀態代碼並將其列印到文件中HTTP/1.1 200 OK
curl -v -I -s http://superuser.com | findstr /c:"HTTP" >> test_result.txt
如何組合這兩個命令來取得一個請求的輸出,如下所示
HTTP/1.1 200 OK - 0,016
因此,提取帶有 http 標頭的行並附加執行時間,而無需文件中的所有其他標頭
答案1
您可以使用批次文件
@echo off
setlocal enableextensions disabledelayedexpansion
rem Just to test - Generate a file with URLs
> ".\urls.txt" (
echo http://superuser.com
echo http://www.google.com
)
> ".\testResults.txt" (
for /f "useback delims=" %%u in (".\urls.txt") do (
set "statusCode="
echo([%%u]
for /f "tokens=1,2 delims=#" %%a in ('
curl -w "##%%{time_connect}##." -I -s --url "%%~u"
^| findstr /l /b /c:"HTTP/" /c:"##"
') do if "%%b"=="." (
setlocal enabledelayedexpansion
echo( !statusCode! - %%a
endlocal
) else (
set "statusCode=%%a"
)
)
)
只有兩個嵌套循環。第一個循環遍歷 URL 文件,第二個執行並處理命令的輸出curl
。
命令的輸出curl
經過過濾,findstr
僅檢索帶有狀態代碼和連接時間的行(-w
已修改輸出字串以在輸出中找到此資料)