Skip to content

Instantly share code, notes, and snippets.

@jensmeindertsma
Created July 17, 2025 08:08
Show Gist options
  • Save jensmeindertsma/080930c8f38479b0dfd207e134e3b473 to your computer and use it in GitHub Desktop.
Save jensmeindertsma/080930c8f38479b0dfd207e134e3b473 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Try to get IPv4 address and mask from tun0
tun_info=$(ip -4 addr show tun0 2>/dev/null | grep -oP 'inet \K[\d.]+/\d+')
# Try to get IPv4 address and mask from eth0 if tun0 fails
eth_info=$(ip -4 addr show eth0 2>/dev/null | grep -oP 'inet \K[\d.]+/\d+')
# Decide which to display
if [[ -n "$tun_info" ]]; then
echo "$tun_info [tun0]"
elif [[ -n "$eth_info" ]]; then
echo "$eth_info [eth0]"
else
echo "No IP address found for tun0 or eth0" >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment