ping: Network Connectivity Test

ping is a fundamental network utility used to test the reachability of a host on an Internet Protocol (IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer. It operates by sending Internet Control Message Protocol (ICMP) echo request packets to the target host and listening for ICMP echo reply packets.

1. Basic ping Usage (Cross-Platform)

The most basic use of ping involves specifying the target IP address or hostname.

# Linux/macOS
ping <target_ip_or_hostname>
 
# Windows (Command Prompt/PowerShell)
ping <target_ip_or_hostname>

Example:

ping google.com
ping 8.8.8.8

The output typically shows the destination, packet size, sequence number, time-to-live (TTL), and the time taken for the round trip.

2. Common ping Options (Linux/Unix)

Ping a Specific Number of Times (-c)

Send a specified number of echo requests and then stop.

ping -c 5 <target_ip_or_hostname>

Specify Interval Between Pings (-i)

Set the time (in seconds) to wait between sending each packet.

ping -i 0.5 <target_ip_or_hostname> # Ping every half second

Specify Packet Size (-s)

Set the size of the data portion of the ICMP packets.

ping -s 1000 <target_ip_or_hostname> # Send 1000-byte packets

Log Output to a File (Linux/Unix)

Redirect standard output to a file.

ping -c 10 google.com > ping_log.txt

3. Common ping Options (Windows)

Continuous Ping (-t)

Send echo requests to the destination until interrupted (Ctrl+C).

ping -t <target_ip_or_hostname>

Ping a Specific Number of Times (-n)

Send a specified number of echo requests.

ping -n 5 <target_ip_or_hostname>

Specify Packet Size (-l)

Set the size of the data portion of the ICMP packets.

ping -l 1000 <target_ip_or_hostname> # Send 1000-byte packets

Log Output to File in Windows PowerShell

The provided PowerShell commands are sophisticated ways to log continuous ping output with timestamps.

ping.exe -t <target_ip> | Foreach { "{0} - {1}" -f (Get-Date), $_ } >> pingloguks.txt
  • ping.exe -t <target_ip>: Initiates a continuous ping.
  • | Foreach { ... }: Pipes each line of ping output to a Foreach-Object loop.
  • "{0} - {1}" -f (Get-Date), $_: Formats each line by prepending the current timestamp (Get-Date) to the ping output ($_).
  • >> pingloguks.txt: Appends the formatted output to pingloguks.txt.

Example:

ping.exe -t 10.21.1.4 | Foreach { "{0} - {1}" -f (Get-Date), $_ } >> pinglog_server1.txt

Note: The behavior and available options for ping can vary slightly between operating systems. Always consult the man page (man ping on Linux/macOS) or help (ping /? on Windows) for the most accurate information for your system.