Ping Command with Examples

Ping 
Ping is the primary TCP/IP command used to troubleshoot connectivity, reachability, and name resolution. Ping works by sending ICMP(Internet Control Message Protocol) echo request to the specified interface on a network and waiting for the reply.

Below are some of the ways on how we can use ping tool via. command line in windows machine.

1.In window machine, 4 replies are sent by default for each request.
C:\Users\Prakash>ping 192.168.1.1

2.You can also use ping tool with the URL.
C:\Users\Prakash>ping www.microsoft.com

3.This option sets the number of ICMP Echo Requests to send, from 1 to 4294967295. The ping command will send 4 by default if -n isn't used.
C:\Users\Prakash>ping 192.168.1.1 -n 6

4.This option let you ping the specified host until it is stopped.
C:\Users\Prakash>ping 192.168.1.1 -t

5.Use the "help" to see the details about the Usage and Options of Ping Commands
C:\Users\Prakash>ping /?

6.Using this command you can print the ping result in the separate notepad file. Be sure to change the directory the directory to the desired location where you want to save the ping. For example if you want to save the ping to the Desktop. Use>> cd Desktop  and then run the below command.
C:\Users\Prakash\Desktop>ping 192.168.1.1 -t > ping_result.txt

7.Similarly, if you want to limit the number of ICMP request you can use -n <desired value>. Also, if you replace -n with -t it will continue to print the result to the notepad file until it is stopped by the user.
C:\Users\Prakash>ping 192.168.1.1 -n 5 > ping_result.txt

8.If you want the date and time to be printed in each ping result you can you the below command.
Here;
The (1,0,2) inside the loop structure (for /l %i in (1,0,2) ...) denotes the parameters for the loop control variable %i. Each value inside the parentheses represents:
The first value (1) is the starting point of the loop.
The second value (0) is the increment value by which the loop control variable %i is increased with each iteration.
The third value (2) is the ending point of the loop.
So, (1,0,2) means the loop starts at 1, increments by 0 (which effectively means it stays at 1), and ends at 2. However, this loop structure won't execute as expected because the increment value is 0, causing an infinite loop.
As I want to print the result to my desktop, I have changed the directory to Desktop first.
 
C:\Users\Prakash>cd Desktop
C:\Users\Prakash\Desktop>(for /l %i in (1,0,2) do @echo|cmd /v:on /c set /p=!time! !date! & ping -n 1 192.168.1.1 | findstr "Reply timed" && timeout /t 1 > nul) >> date.txt


9.If you want the result of shown in step 8 to be printed limited number of time. For example; lets say you want to print only 11 pings then, change the loop as below
C:\Users\OCR-user\Desktop>(for /l %i in (1,1,11) do @echo|cmd /v:on /c set /p=!time! !date! & ping -n 1 192.168.1.1 | findstr "Reply timed" && timeout /t 1 > nul) >> ping_result.txt
 
10.You can also get the same result by using the Powershell command.

Now, type the below commands, save the command and execute or exit the command using the button shown in the picture.
ping.exe -t 192.168.1.1 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> C:\users\prakash\desktop\ping_result.txt



Previous Post Next Post

Ads.

Ads..