Skip to content

Instantly share code, notes, and snippets.

@schappim
Last active February 17, 2024 06:15
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save schappim/1d958254a2907f073cf3b70091ab4b0f to your computer and use it in GitHub Desktop.
Save schappim/1d958254a2907f073cf3b70091ab4b0f to your computer and use it in GitHub Desktop.
Just F-ing Ping - Because sometimes you just want to f-ing ping!

Just F-ing Ping

Because sometimes you just want to f-ing ping!

Modern browsers, believing they are being clever, hide the protocol in the URL bar.

image

However, even if you only select the hostname, when you paste that URL into your terminal, you will encounter the following:

image

This will inevitably fail due to the browser's "helpful" addition of the protocol and a trailing slash to the hostname you've copied.

This basic Bash function alters the ping command, allowing it to accept URLs as well.

Example:

ping https://google.com

instead of having to remove everything but the hostname i.e.

ping google.com

The entirety of the function is:

function ping() {
 local new_args=()
 local url_found=0

 for arg in "$@"; do
     if [[ "$arg" =~ ^http ]] && [ $url_found -eq 0 ]; then
         # Process the URL
         local url="$arg"
         url=${url#*://}  # Remove protocol
         url=${url%%/*}   # Remove path
         url=${url%%:*}   # Remove port
         url=${url%%@*}   # Remove user info
         new_args+=("$url")
         url_found=1
     else
         # Add the argument as is
         new_args+=("$arg")
     fi
 done

 # Call the original ping command with the new argument list
 command ping "${new_args[@]}"
}

Install Instructions

To add the ping function to your shell environment so that it's available every time you open a terminal, you'll need to add it to your shell's configuration file. For Bash, this is typically .bashrc, and for Zsh, it's .zshrc. Here are the steps for both:

For Bash (~/.bashrc):

  1. Open your .bashrc file in a text editor. You can use a command-line editor like nano or vi, or any graphical text editor you prefer. For instance, to edit it with nano, use:

    nano ~/.bashrc
  2. At the end of the file, add the ping function:

function ping() {
    local new_args=()
    local url_found=0

    for arg in "$@"; do
        if [[ "$arg" =~ ^http ]] && [ $url_found -eq 0 ]; then
            # Process the URL
            local url="$arg"
            url=${url#*://}  # Remove protocol
            url=${url%%/*}   # Remove path
            url=${url%%:*}   # Remove port
            url=${url%%@*}   # Remove user info
            new_args+=("$url")
            url_found=1
        else
            # Add the argument as is
            new_args+=("$arg")
        fi
    done

    # Call the original ping command with the new argument list
    command ping "${new_args[@]}"
}
  1. Save the file and exit the editor. In nano, you do this by pressing CTRL + X, then Y to confirm, and Enter to save.

  2. To make the changes take effect in your current terminal session, source the .bashrc file:

    source ~/.bashrc

For Zsh (~/.zshrc):

  1. Open your .zshrc file in a text editor, like so:

    nano ~/.zshrc
  2. At the end of the file, add the same ping function:

function ping() {
    local new_args=()
    local url_found=0

    for arg in "$@"; do
        if [[ "$arg" =~ ^http ]] && [ $url_found -eq 0 ]; then
            # Process the URL
            local url="$arg"
            url=${url#*://}  # Remove protocol
            url=${url%%/*}   # Remove path
            url=${url%%:*}   # Remove port
            url=${url%%@*}   # Remove user info
            new_args+=("$url")
            url_found=1
        else
            # Add the argument as is
            new_args+=("$arg")
        fi
    done

    # Call the original ping command with the new argument list
    command ping "${new_args[@]}"
}
  1. Save and exit the editor.

  2. To apply the changes, source the .zshrc file:

    source ~/.zshrc

Now, the modified ping function will be available in any new terminal session. Note that this will override the default ping command in your shell, so make sure this is the behavior you want. If you ever wish to revert to the original ping command, simply remove these lines from your .bashrc or .zshrc and source the file again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment