Working with ping localhost in Batch - batch-file

We use ping localhost -n 2 >nul to delay its following executions.
We can change 2 to the number of seconds needed.
How can I control this in a much broader way? I tried using 1.5 instead of 2 and it didn't work.
Is there any code by which we can change the unit of time?
EDIT: Instead of ping localhost -n 2 >nul. I'm using TIMEOUT 1 >nul.

The command timeout is the best choice for waiting a specific time in a batch file which is designed for execution on Windows 7 and later versions of Windows. It supports breaking the timeout by the user with any key except /NOBREAK is specified as parameter. And it shows a nice message with a seconds countdown for the user informing also the user how to break the timeout. But it supports only timeout values in seconds, not in milliseconds.
The command sleep could be also used on Windows XP and later versions of Windows when having access to Windows 2003 resource kit and this small executable is copied to all computers running the batch file. But this executable is deprecated because of being replaced by TIMEOUT and by default not installed on any Windows computer.
But a good choice for all Windows is using the command ping for pinging the loopback adapter or a not reachable IP address with using appropriate values of the options -n and -w for the delay.
The IP address of the loopback adapter of local machine is 127.0.0.1, see Wikipedia articles about Reserved IP addresses. localhost is just an RFC defined alias for 127.0.0.1 defined on Windows XP and former Windows versions in file %SystemRoot%\system32\drivers\etc\hosts and is defined built-in on Windows Vista and later Windows versions.
The first ping of 127.0.0.1 is always immediately successful. Therefore using command PING with -n 1 as option gives just a delay of approximately a millisecond in total.
For that reason using PING as delay on pinging 127.0.0.1requires a value greater 1 for option -n ... number of echo requests to send. After a successful request PING waits about 1 second before making the next request.
So for a delay of 5 seconds the following command line is necessary with 6 echo requests:
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 6 >nul
Note 1: Windows is not a real-time operating system and for that reason the time is not 100% accurate, but should be good enough for a batch file.
The option -w defines in milliseconds how long ping (Microsoft documentation) waits for an echo on the request. It does not define the time between two successful requests. Therefore this option can't be used to fine tune the delay on pinging the IP address 127.0.0.1 as this request is successful in less than 1 millisecond and value of option -w does not matter.
So for a delay in milliseconds instead of seconds it is necessary to ping an IP address which is definitely or at least most likely not reachable and which is not routed via networks because of being a private network address according to RFC 6761.
An example is:
%SystemRoot%\System32\ping.exe 168.192.255.253 -n 1 -w 1500
The IPv4 address range from 168.192.0.0 to 192.168.255.255 is for private networks. The highest address 192.168.255.255 in this network is the broadcast address and is not used for devices. It is common to configure a router with local area network broadcast address minus 1 which means 192.168.255.254 could be assigned to a router in case of current computer is part of this private network. And other devices in a LAN get assigned usually the IPv4 addresses from lowest address plus 1 upwards. Therefore for IPv4 network 168.192.0.0/16 the IP address 168.192.255.253 is most likely not assigned to any device which would respond on the echo request of PING.
Well, the milliseconds delay is not very accurate. But is it really important on execution of a batch file to wait exactly 1500 ms?
Note 2: This approach does not work if the computer on which the batch file is running is currently not connected to any network. Without any network connection each echo request is always immediately terminated and PING outputs for each echo request the error message:
PING: transmit failed. General failure.
The general failure is no network (connection) present at all and therefore only echo requests to local loopback adapter work.

Unfourtanelty, #thx1138v2's solution only delays 0.04 seconds on my machine. Therefore, I've modified his solution to make it more accurate.
ping 1.1.1.1 -n 1 -w 1500 >nul
1500 stands for 1500 milliseconds, which is 1.5 seconds.
ping is inaccurate when pinging a small amount of time, see this table:
Milliseconds In Code | Actual Waited Time
1500 | 1.24 seconds - 1240 milliseconds
1600 | 1.34 seconds - 1360 milliseconds
1700 | 1.52 seconds - 1520 milliseconds
As you can see, 1700 ms's wait time is much precise than 1500 ms, so you may need to consider some extra milliseconds.
Note: ping only supports delay more than 99 milliseconds

-n is the (n)umber of times to ping, not the amount of time to wait. You can't ping 1.5 times.
-w is the time to (w)ait on each ping in milliseconds. To pause 1.5 seconds would be
ping -n 3 -w 500
If there is a web site set up on the machine running the batch file the ping will find it as localhost and the timeout will not apply. The timeout only applies to failed requests. It is better to ping 0.0.0.1 for a delay.
ping -n 3 -w 500 0.0.0.1

Related

How to force Windows Comand Prompt to flush text output to a file line-by-line

I'm trying to log every ping in my Windows CE5.0 machine Command Prompt using
> ping 192.168.1.1 -t -l 60000 >> ping.txt
The file starts with a single line of output and then only flushes after pressing 'ctrl+c'.
I was wondering if there was a way to force it to print in every new line.
I do not know any direct way to do this.
But you could work around that by doing single ping requests in a (infinite) loop and write to the log file in the loop, like this:
for /L %I in () do #(timeout 1 > nul & ping 192.168.1.1 -n 1 -l 60000 | find "TTL=" >> "ping.txt")
The timeout 1 command establishes a one-second delay in every loop iteration in order to avoid heavy CPU load, > nul suppresses its console output.
The find command is used to filter for lines containing TTL from positive replies (like Reply from 192.168.1.1: bytes=60000 time<1ms TTL=128). If you want, you can change that to findstr /B /C:"Reply from " /C:"Request " /C:"Ping request ", for example, to capture positive replies as well as negative ones like Request timed out. or Ping request could not find host ..., or you can remove it completely (also the |) to write the whole ping response to the file, including header and footer.

How to ignore timeouts in ab (apache bench)?

I run benchmarks with apache bench on a web service. I know that 1-2 requests from the test will be timeouted during measurement (it's a web framework issue). And when timeout occurs ab quits with the message apr_pollset_poll: The timeout specified has expired (70007) and does not show results. I want to get measurement results ignoring these timeouted tests (or count them too, but just use timeout value as response time). Is it possible with ab?
EDIT: The command I use is
ab -n 1000 -c 10 http://localhost:80
I looked into ab source and from what I saw it's impossible to ignore these errors. Maybe there is a fork which implements such feature?
The default timeout is 30 seconds. You can change this with -s:
ab -s 9999 -n 1000 -c 10 http://localhost:80

CMD - Ping and traceroute from list of ips

We have sporadic connection failures when webserver tries to connect to service on the net.
There is a problem to trace failure from PHP for many reasons.
I'm a web-programmer and not familiar with command-line scripts. Can anyone help with following cmd-script:
-there is a list of ips separated by newline in text file (ip_list.txt)
-take ip from list and ping it, if it fails on first attempt - traceroute it
-go to next ip in file
I don't really sure what you want to test, but here's a pretty useful command to test ping. Enter ping IP_ADDRESS -l 10 -n 10 directly to cmd, change the IP_ADDRESS to ip address you want. -l 10 - Ping you ip address with 10 bytes of data
-n 10 - Ping for 10 times
ping /? - For more informations
To get each ip address (line by line) in a text file, use for /f %%a in (YOUR_FILE.txt) do ( //to do ). Since I'm not sure about what you want, so that's all I can help with :)

Save ping results to text file with Computer name

I'd like to use a variation of the following batch script to save the results of a ping test to a folder on the network with the computer name appended to the file name or somewhere in the results.
ping www.google.com -n 1000 > pinglog.txt
type pinglog.txt
I need to perform some diagnostics on all computers of a network to determine whether a connection stability issue is router related, internet related or localised to just one of the computers.
The batch file will be stored on the network at \\192.168.1.254\ICT\Scripts, and I would like the log files saved to that location with the computer name added so that I can determine which PC the results belong to (eg. pinglog-reception.txt).
Can this be done? If so, how?
If you want to do this for different servers, then:
server=www.google.com
ping %server% -n 1000 > \\192.168.1.254\ICT\Scripts\%server%.txt
type \\192.168.1.254\ICT\Scripts\%server%.txt
If you want to do this for different querying computers on your own network, then see the other answer. You will need to use the network name for the file:
ping www.google.com -n 1000 > \\192.168.1.254\ICT\Scripts\pinglog-%COMPUTERNAME%.txt
Use the %COMPUTERNAME% environment variable:
ping www.google.com -n 1000 > pinglog-%COMPUTERNAME%.txt
pathping
would seem to be better suited to this than ping.

Batch loop not performing as expected

I'm writing a script to start up a system via IPMI. One of the machines must be reachable before bringing up the other machines.
I wrote a simple loop expecting it to work, but it's giving me some troubles
:PING_LOOP
ping -n 1 -w 1000 <IP Address>
IF %ERRORLEVEL% NEQ 0 GOTO PING_LOOP
Where <IP Address> is the ip address of the machine I need to wait for before continuing. The loop works for like 3 or 4 iterations, but then fails after that even though the system isn't reachable yet. Is there a reason for this?
EDIT: This script is being run from a Win Server 2008 R2 machine
Alright figured it out.
Apparently when you ping something and you get Destination host unreachable it produces an ERRORLEVEL of 0 even though you aren't getting a ping, which doesn't make sense to me. Maybe I just don't completely understand how ping works
Here is a quick fix, though. I just parse out TTL. It's a bit naive, but it works for my application.
:PING_LOOP
ping -n 1 -w 1000 <IP Address> | find "TTL"
IF %ERRORLEVEL% NEQ 0 GOTO PING_LOOP

Resources