Ping is the utility used to test the reachability of a host on a network. It is virtually available for all operating systems that have networking capability.
We often use ping to know if the host is available or not. It can also be used to monitor when the host becomes available or unavailable. But in both these scenarios, the one thing we miss is the time at which the host becomes available or unavailable.
So, to get the date & time-stamps at which the host becomes available, we may use any of the following methods.
• For Linux, we can use the following one-liner with a ‘while read line’ for getting the time-stamped output.
SYNTAX:
# ping HOST | while read line; do echo `date` – $line; done
NOTE:
⇾ Replace HOST with the required hostname/ipaddress (eg: google.com)
⇾ Output from the ping command is piped to the following while read line loop
while read line; do echo TASK; done
• For Linux, we may also combine the ping utility with perl for getting the time-stamped output.
NOTE:
⇾ Replace HOST with the required hostname/ipaddress (eg: google.com)
⇾ -n option wraps the -e/-E code in a while loop
⇾ -n option causes Perl to execute our script inside the following loop
while (<>) {SCRIPT}
⇾ -e execute code on the command line
⇾ -E execute code on the command line with all the latest features enabled
• For Windows, we can use the following one-liner ping command.
SYNTAX:
cmd> ping -t HOST | cmd /q /v /c “(pause&pause) > nul & for /l %a in () do (set /p “data=” && echo(!date! !time! !data!) &ping -n 2 HOST > nul”
NOTE:
⇾ Replace HOST with the required hostname/ipaddress (eg: google.com)
⇾ ping – This is the ping utility
⇾ -t – This is for continuous pinging
⇾ /q – For cmd, turns echo off
⇾ /v – Enable delayed environment variable expansion using ! as the delimiter. It expands variable at input time, which is quite a different thing when inside of a for loop
⇾ /c – Carries out the command specified by string and then terminates
⇾ for /l %a – for loop with /l uses an iterative variable to set the starting value (start#) and then set through a set range of values until the value exceeds the set ending value (end#).
%a is for use on commandline. To use from a script, replace %a with %%a
⇾ set /p – Sets environment variable, where /p prompts for user input
⇾ Two additional pause commands are included at the start of subshell (only one can be used, but a pause consumes a input character, a CRLF pair is broken and a line with a LF is read) to wait for input data
⇾ ping -n 2 command is is included to wait a second for each read in the inner loop. The inner ping can be replaced with a pause, but then the first character of each read line is consumed by the pause and not retrieved by the set /p
• For Windows, we may also use the following PowerShell script for getting the time-stamped output.
SYNTAX:
powershell> ping -t HOST | ForEach {“{0} – {1}” -f (Get-Date),$_}
NOTE:
⇾ Replace HOST with the required hostname/ipaddress (eg: google.com)
⇾ ping – This is the ping utility
⇾ -t – This is for continuous pinging
⇾ Output from the ping command is piped to the following
⇾ Foreach specifies the next task in our command
⇾ [{0} – {1} -f] – This is the text formatting
⇾ (Get-Date) – This is the date. It is also what is going into {0} in the text formatting above
⇾ $_ – This takes our previous input from ping and puts into {1}
⇒ In addition, for Windows operating systems, the following third-party utilities with more advanced features can also be used.
▪ PINGWIZ (Windows)
PINGWIZ (https://www.ctrl-alt-del.com.au/freeware-sbc/) can ping devices with an IPv4 address.
It was written to replace the command Ping utility currently found in the MS Windows Operating System.
SYNTAX:
cmd> PINGWIZ HOST /t
NOTE: Replace HOST with the required hostname/ipaddress(eg: google.com)
▪ Bping (Windows)
Bping (https://gizmoware.net/bping/) is a Windows ping alternative that beeps whenever it gets replies.
This is handy if we have to spend time away from the pinging machine, fiddling with network cables
and is having a difficult time reaching back to the machine and running back to the networking devices
multiple times. Bping also has a bunch of other enhancements including the ability to scan entire networks.
SYNTAX:
cmd> bping -u HOST
NOTE:
⇾ Replace HOST with the required hostname/ipaddress(eg: google.com)
⇾ Use the -q option if beep sound is not required
▪ hrPING (Windows)
hrPING (https://www.cfos.de/en/ping/ping.htm) is a high precision ping utility with advanced
functionality and improved statistics.
It has high resolution timers, multiple pings “in-flight”, UDP and ICMP support, control over packet
sizes, graphical display result, traceroute, pathping, and much more.
NOTE: You have to be a member of the Administrator group to run hrPing, since it uses “raw sockets“.
SYNTAX:
cmd> hrping -t -T HOST
NOTE: Replace HOST with the required hostname/ipaddress(eg: google.com)
▪ PowerPing (Windows)
PowerPing(https://github.com/Killeroo/PowerPing) is a small improved command line ICMP ping program.
It also provides features such as colored output, graphing, display customization, ICMP packet
customisation, ICMP packet listen/capture, scanning, flooding, ip location lookup, whois lookup etc.
SYNTAX:
cmd> PowerPing –t –fts HOST
NOTE: Replace HOST with the required hostname/ipaddress(eg: google.com)
Done.