|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Function to reliably fetch the external/public IP address |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Thomas Malt <thomas@malt.no> |
| 6 | +# |
| 7 | +# License: MIT |
| 8 | +# |
| 9 | +# Uses multiple fallback methods to ensure reliability: |
| 10 | +# 1. Cloudflare DNS (preferred, since likely using Cloudflare) |
| 11 | +# 2. OpenDNS (most reliable DNS-based method) |
| 12 | +# 3. HTTP-based services as final fallback |
| 13 | +# |
| 14 | +# Returns: External IP address on success, exits with error code on failure |
| 15 | +# |
| 16 | + |
| 17 | +myip() { |
| 18 | + local ip="" |
| 19 | + local verbose=false |
| 20 | + |
| 21 | + # Parse options |
| 22 | + if [[ "$1" == "-v" ]] || [[ "$1" == "--verbose" ]]; then |
| 23 | + verbose=true |
| 24 | + fi |
| 25 | + |
| 26 | + # Method 1: Try Cloudflare DNS first (fast and reliable) |
| 27 | + if [[ "$verbose" == true ]]; then |
| 28 | + echo "Trying Cloudflare DNS..." >&2 |
| 29 | + fi |
| 30 | + ip=$(dig +short txt ch whoami.cloudflare @1.0.0.1 2>/dev/null | tr -d '"') |
| 31 | + if [[ -n "$ip" ]] && [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 32 | + [[ "$verbose" == true ]] && echo "Success via Cloudflare DNS" >&2 |
| 33 | + echo "$ip" |
| 34 | + return 0 |
| 35 | + fi |
| 36 | + |
| 37 | + # Method 2: Try OpenDNS (gold standard for reliability) |
| 38 | + if [[ "$verbose" == true ]]; then |
| 39 | + echo "Trying OpenDNS..." >&2 |
| 40 | + fi |
| 41 | + ip=$(dig @resolver1.opendns.com myip.opendns.com +short 2>/dev/null) |
| 42 | + if [[ -n "$ip" ]] && [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 43 | + [[ "$verbose" == true ]] && echo "Success via OpenDNS" >&2 |
| 44 | + echo "$ip" |
| 45 | + return 0 |
| 46 | + fi |
| 47 | + |
| 48 | + # Method 3: Try ifconfig.me (reliable HTTP service) |
| 49 | + if [[ "$verbose" == true ]]; then |
| 50 | + echo "Trying ifconfig.me..." >&2 |
| 51 | + fi |
| 52 | + ip=$(curl -s --connect-timeout 5 --max-time 10 ifconfig.me 2>/dev/null) |
| 53 | + if [[ -n "$ip" ]] && [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 54 | + [[ "$verbose" == true ]] && echo "Success via ifconfig.me" >&2 |
| 55 | + echo "$ip" |
| 56 | + return 0 |
| 57 | + fi |
| 58 | + |
| 59 | + # Method 4: Try icanhazip.com (simple and fast) |
| 60 | + if [[ "$verbose" == true ]]; then |
| 61 | + echo "Trying icanhazip.com..." >&2 |
| 62 | + fi |
| 63 | + ip=$(curl -s --connect-timeout 5 --max-time 10 icanhazip.com 2>/dev/null) |
| 64 | + if [[ -n "$ip" ]] && [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 65 | + [[ "$verbose" == true ]] && echo "Success via icanhazip.com" >&2 |
| 66 | + echo "$ip" |
| 67 | + return 0 |
| 68 | + fi |
| 69 | + |
| 70 | + # Method 5: Try api.ipify.org (last resort) |
| 71 | + if [[ "$verbose" == true ]]; then |
| 72 | + echo "Trying api.ipify.org..." >&2 |
| 73 | + fi |
| 74 | + ip=$(curl -s --connect-timeout 5 --max-time 10 api.ipify.org 2>/dev/null) |
| 75 | + if [[ -n "$ip" ]] && [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 76 | + [[ "$verbose" == true ]] && echo "Success via api.ipify.org" >&2 |
| 77 | + echo "$ip" |
| 78 | + return 0 |
| 79 | + fi |
| 80 | + |
| 81 | + # All methods failed |
| 82 | + [[ "$verbose" == true ]] && echo "ERROR: Failed to retrieve external IP address" >&2 |
| 83 | + return 1 |
| 84 | +} |
0 commit comments