我正在嘗試創建一個腳本,使我能夠從文本文件中ping計算機列表並輸出它們是否響應或不響應CSV文件。當使用一臺機器我收到正確的錯誤級別響應測試命令,但是當我嘗試使用for語句的每個結果被列爲錯誤級別0批量ping結果爲CSV
for /f %%g in (computers.txt) do (
ping -n 1 %%g | findstr "TTL"
if errorlevel equ 0 (
echo %%g,success >> results.csv
) else (
echo %%g,fail >> results.csv
)
)
我在做什麼錯?我試過了上面的代碼,以及Batch ping a list of computer names and write the results to file(不返回任何響應)的代碼。有沒有更好的方法來實現結果?任何幫助將不勝感激。
解決方案
for /f %%g in (computers.txt) do (
for /f "tokens=1" %%a in ('ping -n 1 %%g ^| findstr /i /c:"try" /c:"out" /c:"TTL"') do (
if %%a EQU Ping (echo.%%g,Could not find host>>results.csv)
if %%a EQU Destination (echo.%%g,Destination host unreachable>>results.csv)
if %%a EQU Request (echo.%%g,Request timed out>>results.csv)
if %%a EQU Reply (echo.%%g,Replied>>results.csv))
)
更新作爲響應至G的回答說。謝謝G!
您打算使用[tag:powershell]代替嗎?比傳統的批處理文件更強大和靈活。嘗試在Google上搜索「powershell ping program」。許多可用的東西。 – David
我沒有想過使用powershell,但Test-Connection命令看起來非常有用。當我回到辦公室時,我會試試這個。謝謝大衛! – AKAJim