Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Last active April 24, 2025 20:24
Show Gist options
  • Save diegofcornejo/f4c752ce05fb541612d98efb60e93074 to your computer and use it in GitHub Desktop.
Save diegofcornejo/f4c752ce05fb541612d98efb60e93074 to your computer and use it in GitHub Desktop.
Get IP from bash
alias myip=ipinfo
ipinfo() {
# Check if an IP is passed as an argument
if [ -n "$1" ]; then
ip="$1"
data_ipinfo=$(curl -s "https://ipinfo.io/widget/demo/$ip")
# Only data from ipinfo if a custom IP is passed
jq -n \
--argjson ipinfo "$data_ipinfo" \
'{
ip: $ipinfo.input,
location: $ipinfo.data.loc,
timezone: $ipinfo.data.timezone,
asn: $ipinfo.data.asn,
carrier: $ipinfo.data.carrier,
privacy: $ipinfo.data.privacy,
abuse: $ipinfo.data.abuse
}'
else
# Get local IP and query both services
ipv4=$(curl -s https://ipinfo.io/ip)
data_ipinfo=$(curl -s "https://ipinfo.io/widget/demo/$ipv4")
data_ipguide=$(curl -sL https://ip.guide)
jq -n \
--argjson ipinfo "$data_ipinfo" \
--argjson ipguide "$data_ipguide" \
'{
ipv4: $ipinfo.data.ip,
ipv6: $ipguide.ip,
location: $ipguide.location,
asn: $ipinfo.data.asn,
carrier: $ipinfo.data.carrier,
privacy: $ipinfo.data.privacy,
abuse: $ipinfo.data.abuse,
ipv6_network: $ipguide.network
}'
fi
}
alias myip=ipinfo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment