Ping every IP address in a text file?

Solution 1:

Try this:

@echo off
for /f "delims=" %%a in (computerlist.txt) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a failed to respond) 
pause

If you have to use a filename or path with spaces or odd characters then Instead of (computerlist.txt) use ( ' type "c:\folder\computer file.txt" ' )

Solution 2:

@Echo OFF

For /F "Usebackq Delims=" %%# in (
    "List.txt"
) do (
    Echo+
    Echo [+] Pinging: %%#

    Ping -n 1 "%%#" 1>nul && (
        Echo     [OK]) || (
        Echo     [FAILED])
)

Pause&Exit

Output:

[+] Pinging: www.google.com
    [OK]

[+] Pinging: ffff
    [FAILED]

Solution 3:

Suggest using powershell, this is faster compared to the traditional ping, here is the cmd,

If you want more details, refer here https://tech3motion.com/powershell-cmd-to-ping-list-of-servers-ip/

$InputFile = 'C:\Temp\MachineList.txt'
$machines = Get-content $InputFile

foreach ($machine in $machines){
 if (Test-Connection -ComputerName $machine -Count 1 -ErrorAction SilentlyContinue){
   Write-Host "$machine,up" -ForegroundColor Green
 }
 else{
   Write-Host "$machine,down" -ForegroundColor Red
     } 
 }

Solution 4:

An alternative you may wish to look at is to use PowerShell:

cls;
ForEach ($targetComputer in (Get-Content C:\installs\computerlist.txt)) {
    if (Test-Connection -ComputerName $targetComputer -Count 1 -Quiet) {
        "$targetComputer - Ping OK"
    } else {
        "$targetComputer - Ping FAIL"
    }
}

Replace the contants of C:\Installs\ComputerList.txt and you're away :)

enter image description here