CMD record of no reply from ping command

1

Im writing a script to ping WAPs and Switches within a school and everything on it is running great aside from it will not pipe no responses into a .txt because I have added

| FIND "TTL="


This is basically my script aside from mutlitple lines for all the IP's

@echo off
del Test.txt
set /p var1=How many times would you like to test the connection for each device?
cls
echo Testing WAP Connections, Please Wait...
ping -n %var1% 127.0.0.1 | FIND "TTL=" > Test.txt


when I run the script with the "FIND" part of the script i get this output

Reply from 127.0.0.1: bytes=32 time<1ms TTL=255

but if i dont get a reply from whatever i am pinging, it wont make a record of it in the .txt file. Is there any way I can get it to record a failed ping without having to get rid of: " | FIND "TTL=""?

Super Geoff

Posted 2014-08-11T07:42:49.617

Reputation: 70

The line, del Test.txt is not needed. The > in the ping line will overwrite the file at a better time (that will let your users press ctrl+C during the input and leave the old test data). – krowe – 2014-08-11T07:57:45.797

Answers

0

Is there any way I can get it to record a failed ping without having to get rid of: " | FIND "TTL=""?

No, at least not using Microsoft's stock find command.

Here's some documentation for find which on the client side applies to Windows Vista and Windows 8 that states, in part:

If you do not use /i, find searches for exactly what you specify for String.

Since you can only specify one String to search for, there is no direct way to do what you are trying to do.

You can, however, do it in a slightly roundabout way: redirect the output of ping to a temporary file, and run find twice on that file:

ping -n %var1% 127.0.0.1 > Test.tmp
FIND "TTL=" Test.tmp > Test.txt
FIND "Request timed out." Test.tmp >> Test.txt

Or you can use a tool that supports searching for multiple distinct strings, such as GNU grep (which is available for Win32). An alternative approach pointed out in a comment by and31415 is to use findstr instead, which is available in Windows 2000 and newer:

ping -n %var1% 127.0.0.1 | findstr /c:"TTL=" /c:"Request timed out."

Obviously, since both of these operate by looking at the native language output of ping they will have problems with non-English locales.

It's worth remembering that find originated in DOS 2.0. That was the same version of DOS that introduced directories, and hard disk support (DOS 1.x only supported using floppy disks for persistent storage, and lacked the concept of directories). Since backwards compatibility is something that Microsoft still takes quite seriously, they cannot easily change how find treats the String parameter, and back in the DOS 2.0 days, both memory and processing speed were at huge premiums. It appears that the find in DOS 2.x didn't even support case insensitive searching (added later with the /I switch). It is an arcane utility, really, surviving much because it cannot trivially be replaced with something different.

a CVn

Posted 2014-08-11T07:42:49.617

Reputation: 26 553

You basically just explained why the common GNU tools should be included on every Windows installation everywhere BTW. Start adding it to your ghost images and you'll be glad you did. – krowe – 2014-08-11T08:07:09.693

1If the OP is using Windows 2000 or later, then findstr should be available. That means you can search multiple strings, for example: ping 127.0.0.1 | findstr /c:"TTL=" /c:"timed out" That wouldn't work on non-English locales, but findstr doesn't support Unicode anyway. – and31415 – 2014-08-11T09:22:24.007

@user3582046 See my update just now based on and31415's comment. – a CVn – 2014-08-11T11:05:14.600

@MichaelKjörling While findstr might work, I would still recommend the method I described in my answer as it's not locale dependent and can find different kind of errors, which can be useful for troubleshooting purposes.

– and31415 – 2014-08-11T11:38:21.217

0

Workaround

Search . rather then TTL=:

ping -n %var1% 127.0.0.1 | FIND "." > Test.txt

As you're not looking for a specific text, this method should work in most Windows locales catching multiple error messages without extra effort.

Example output

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
Ping statistics for 127.0.0.1:

Request timed out

Pinging 1.2.3.4 with 32 bytes of data:
Request timed out.
Ping statistics for 1.2.3.4:

Destination host unreachable

Pinging 131.107.255.255 with 32 bytes of data:
Reply from 10.0.2.15: Destination host unreachable.
Ping statistics for 131.107.255.255:

General failure

Pinging 127.0.0.0 with 32 bytes of data:
General failure.
Ping statistics for 127.0.0.0:

Transmit failed

Pinging 0.0.0.0 with 32 bytes of data:
PING: transmit failed. General failure.
Ping statistics for 0.0.0.0:

Could not find host

Ping request could not find host a.b.c.d. Please check the name and try again.

Further reading

and31415

Posted 2014-08-11T07:42:49.617

Reputation: 13 382