Skip to content

Instantly share code, notes, and snippets.

@zvchei
Last active May 16, 2025 15:18
Show Gist options
  • Save zvchei/2199b21fda19c5581f4639e206ee4e50 to your computer and use it in GitHub Desktop.
Save zvchei/2199b21fda19c5581f4639e206ee4e50 to your computer and use it in GitHub Desktop.
Retrieves the route for a given domain using the `ip` and `dig` tools.
#!/bin/bash
HOST="$1"
if [[ -z "$HOST" ]]; then
echo "Usage: $0 <HOSTNAME>"
exit 1
fi
IP_ADDRESS=$(dig +short A "$HOST" 2>/dev/null)
if [[ -z "$IP_ADDRESS" ]]; then
echo "Could not resolve IP address for $HOST"
exit 1
fi
route=$(ip route get "$IP_ADDRESS" 2>/dev/null | head -1)
if [[ -z "$route" ]]; then
echo "No route found for $IP_ADDRESS ($HOST)"
else
# echo "Route for $HOST ($IP_ADDRESS):"
echo "$route"
fi
# MIT No Attribution
#
# Copyright (c) 2025 zvchei
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment