Skip to content

deuza/ping2km

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub last commit GitHub Release Date GitHub last commit GitHub commit activity GitHub code size in bytes License: CC0 License: WTFPL

Hack The Planet Built With Love X (formerly Twitter) Follow

ping2km

Ping with estimated distance based on the speed of light ... because why not ? :D

The shell script ping2km is a humorous tool that runs a continuous ping to a host and estimates the "as the fibre flies" distance based on the Round Trip Time (RTT).

ping2km screenshot

How it works

Four transmission models are available :

Mode Speed Factor Formula
--fiber (default) 2/3 c ≈ 199,861 km/s 99.9305 distance = RTT_ms × 99.9305 km
--copper 2/3 c ≈ 197,863 km/s 98.93 distance = RTT_ms × 98.93 km
--theoretical c ≈ 299,792 km/s 149.896 distance = RTT_ms × 149.896 km
--dialup 2/3 c minus modem 99.9305 distance = (RTT_ms - 120) × 99.9305 km

Since the RTT (Round Trip Time) is a round trip, the estimated distance is :

distance = (RTT_ms / 1000 / 2) × speed

About the models

The fiber model uses the real speed of light in optical fibre (~2/3 of c, due to the refractive index of silica glass).

The copper model uses the real propagation speed in telephone twisted pair cables (~2/3 of c, due to the dielectric properties of the insulation). Plot twist: fiber and copper give nearly identical results (~1% difference). The signal travels at roughly the same speed in both media. The real difference between fiber and copper has never been propagation speed — it's bandwidth (data throughput) and attenuation (signal loss over distance).

The theoretical model uses c, the speed of light in vacuum (299,792 km/s). This is the absolute physical limit, achievable only in a perfect vacuum. In reality, no transmission medium reaches c. But where's the fun in reality?

The dialup model simulates the good old RTC (Public Switched Telephone Network) era. Analog modems added approximately 120 ms of round-trip latency just for signal encoding/decoding (modulation/demodulation — that's what "modem" stands for). This overhead is subtracted from the RTT before calculating the distance. Back in the dial-up days, the world was smaller. If the RTT is less than 120 ms, the distance shows 0 km [too close for dial-up].

Disclaimer

This is intentionally naive. The estimation does not account for :

  • Router/switch/firewall processing latency
  • Network topology detours
  • Buffering and queuing delays
  • Application-level processing
  • etc.

The actual distance traveled by packets is always greater than the geographical distance, which means that Berlin may be closer to you than New York.

This tool is meant for fun between friends, not for calibrating interferometers ;-)

Installation

curl -O https://raw.githubusercontent.com/deuza/ping2km/main/ping2km.sh
mv ping2km.sh ping2km
chmod +x ping2km
sudo cp ping2km /usr/local/bin/

If curl is not available, you can use another tool such as wget to retrieve the script directly.

wget https://raw.githubusercontent.com/deuza/ping2km/main/ping2km.sh

Dependencies

Standard POSIX utilities, nothing exotic :

  • ping
  • grep
  • sed
  • bc
  • printf

On Debian/Ubuntu, bc might need to be installed : apt install bc

Usage

ping2km [--fiber|--copper|--theoretical|--dialup] [-4] [-c count] <host>
ping2km --help
ping2km --version

Hit Ctrl+C to stop, a statistics summary will be displayed, including RTT and estimated distance (min/avg/max).

Compatibility

Tested on :

  • Debian GNU/Linux 12.x / 13.x (arm64/amd64)
  • FreeBSD 13.x / 14.x
  • macOS (Darwin) Ventura, Sonoma, Sequoia (arm64)

The script uses strictly POSIX sed, POSIX shell arithmetic $((...)), and bc for floating point, just KISS.

Examples

Ping a hostname (fiber, default) :

$ ping2km www.deuza.net

Send only 5 packets :

$ ping2km -c 5 www.deuza.net

Ping with copper model (spoiler: same result as fiber, troll mode :P) :

$ ping2km --copper www.deuza.net

Ping with theoretical speed of light in vacuum :

$ ping2km --theoretical 1.1.1.1

Ping like it's 1998 :

$ ping2km --dialup www.kernel.org

Force IPv4 :

$ ping2km -4 www.deuza.net

Combine options :

$ ping2km --dialup -4 -c 10 www.kernel.org

Unknown host:

$ ping2km nope.invalid
ping2km: nope.invalid: Name or service not known

History

This script was created around 2002 on Solaris/FreeBSD at Club-Internet on 36 lines.
It was revived, corrected, made portable, and properly documented in March 2026.

License

CC0 1.0 Universal -- Public Domain.
CC0

WTFPL -- Do What the Fuck You Want to Public License.
WTFPL

Author

DeuZa

TODO

Nothing planned

Bugs

The output is designed to mimic standard ping(8) output as closely as possible, with the distance appended to each line.

Should not work under Solaris and with exotic ping outputs.
The output line that is parsed should look like this:

$ ping www.deuza.net
PING www.deuza.net (66.6.44.4) 56(84) bytes of data.
64 bytes from 66.6.44.4: icmp_seq=1 ttl=52 time=92.8 ms
64 bytes from 66.6.44.4: icmp_seq=2 ttl=52 time=96.1 ms
^C
--- www.deuza.net ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 92.836/94.470/96.105/1.634 ms
root@pi5:~#

Star History

Star History Chart

With ❤️ by DeuZa

About

Ping with estimated distance based on the speed of light. Because why not?

Resources

License

CC0-1.0, WTFPL licenses found

Licenses found

CC0-1.0
LICENSE
WTFPL
LICENSE-WTFPL

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages