Batch file how to ping – Stack Overflow

2, support ipv6

can support use ipv4 and ipv6 at the same time

3, support ping multi ip

NewBatchPinger can support multi ip ping

4, support two model

can use unprivileged mode , need not to be root


ping can support ping many ip, id is pid,but should notice modify udp.sendspace , udp.recvspace and raw.recvspace and so on.

Example of packet exceeding interval but arriving within timeout:


Note on linux support :

On Mac, it can use unprivileged and privileged, but on Linux or docker, you should use privileged and have sudo permission.

Overview of the scripts

The first script will run fastest since it simply records ping responses to a log file. The second script will run more slowly because it also tries to derive a machine name for each machine. Although this second script runs more slowly, it can be very useful in putting meaning to the list of returned IP addresses.

Ping every ip address in a text file?

You can try to use the tool I developed – ccmd – to help with similar tasks, it runs any terminal command against a number of targets, targets can be given in csv text file like:
#this is a comment line
#target, description, command (default – ping)
#in below command {target} will be replaced with, google DNS, ping -n 1 -w 500 {target}, my subnet, this is bbc news site

save above file in name.txt and point the script to it with -s option, by default it will run ping command if no other command given.
Run it with:
ccmd.exe -s name.txt -b 10 -c 30
reveals that output:

script written on python but has ccmd.exe vesion, that can be run on windows directly. By default details command logs output saved in ./LOG/ folder.

:/>  Очистка DNS кэша. Как очистить кэш DNS.


Summing things up

I hope these examples have been of some use and/or of some interest to you. Please feel free to write me a note in the comments below.

I have written a follow-up article to this article that addresses the issue of non-pingable machines on a network. This article expands on the examples I have described in this post and explains how to increase the accuracy of the machine detection routine.

The first script: logging the ip of active machines in a range of ipv4 addresses

In the script below you can clearly see that there are two FOR loops. The outer loop cycles through the third set of decimal ranges and the inner loop cycles through the fourth set of decimal ranges. For the sake of speed, I have set the outer loop to only cycle twice and the inner loop only to cycle ten times. You’ll need to adjust these numbers and IP addresses to scan the ranges that you are interested in.

As a reminder about batch file FOR loop syntax:

  1. The variable %%i needs two percent signs since it is in a batch file.
  2. The IN part of the statement in the form of (1,1,2) means (1 = the starting number, 1 = increment by one, 2 = the end number) .
  3. Finally, don’t forget to put the @ sign after the DO and before the ( to suppress extra text output to the command window.

As the script loops through the network machines and dynamically puts together IP addresses generated by the two FOR loops, the results of the Ping command get output to a file called IPScanResults.txt . Notice that the full contents of each ping would have too much extraneous information, so the command does a FIND for ping the response line that contains the keyword Reply and only outputs that line, since that line contains the information we are interested in.

:/>  Windows 7 в стиле Mac OS, Windows 10, Windows 8, Linux и других систем

The second script: logging the ip and machine names of active machines in a range of ipv4 addresses

This script is identical to the pure Ping response logging script in the example above. The one significant difference is that this script logs a second line of output for each IP address scanned. I’ve posted the source for this script below.

The second line that the new command in this script will output is the result of the nbtstat command. This is meant to put a descriptive machine name in pace to help you identify what machine each identified IP address actually is. The nbstat command takes a significant amount of time to run, so this slows down this script considerably. However in my opinion, the value add of getting a machine name is worth the wait.

Note that the nbstat command is not completely effective or guaranteed to resolve a name. Nbstat queries NETBIOS and attempts a number of different ways of resolving the machine name on the network. However this is unreliable and not guaranteed to work so you will need to judge for yourself its effectiveness for the network that you are taking a look at.

I found the following article at TechRepublic article about the nbstat command to be very interesting. Have a look if you are interested in finding out more about its inner workings.

Also, in case you are not getting the results from nbstat that you are expecting, try using a different keyword in the FIND command. I have set it to <20>, which is the Server name in the returned nbstat NETBIOS name registration table. A possible alternate keyword to try out might be <00>, which is the Workstation name.

Batch ping a list of computer names and write the results to file

This may not be directly what you are looking for, but I had a similar task: run ping and report success or failure. I’ll leave extracting the IP address to you – seeing as you have already done it.

:/>  Как быстро убрать пароль при входе в Windows 10: проверенные решения

The problem with ping is that it returns success upon name resolution, whether packets get lost or host is unreachable (will report 0% Loss) is irrelevant.

FOR %%a IN (
) DO @FOR /F "delims=" %%p IN (
     'PING -w 100 -n 1 %%a ^| findstr ^"Reply Request fail name^"'
   ) DO @(
     ECHO "%%p" | FINDSTR TTL >2 && echo %%a, success, %%p || echo %%a, failed, %%p
   ) >> Results.csv

Logic: Ping once, filter only lines with the one of the words listed. If TTL exists in resulting line (output to STDERR or NUL to avoid output pollution) echo success, else echo failed.

I’m on English Windows, words will have to be adjusted for other languages.


FOR %%a IN (
) DO @FOR /F "delims=" %%p IN ('PING -n 1 %%a ^| findstr TTL ^|^| echo Failed') DO @(
       ECHO "%%p" | FINDSTR TTL >2 && (for /f "tokens=3" %%b IN ("%%p") do @echo %%a, %%b) || echo %%a, failed, %%p

Less dependant on language, works only for IPv4, added IP extraction.
Filter ping output for TTL, set output to “Failed” if TTL not found.
If output string contains TTL, extract IP and echo host and IP, else echo host name and output string.

Оставьте комментарий