Skip to content

Instantly share code, notes, and snippets.

@deadflowers
Last active July 31, 2025 16:20
Show Gist options
  • Select an option

  • Save deadflowers/75815b7792b48bcf70728f66d4758f8d to your computer and use it in GitHub Desktop.

Select an option

Save deadflowers/75815b7792b48bcf70728f66d4758f8d to your computer and use it in GitHub Desktop.
IP Address to Geolocation

🌐 iplookup β€” IP2Location CLI Tool

Query IP address details using the IP2Location API. Be sure to sign up here before next steps, no CC is needed.

image

πŸ”§ Requirements

  • curl
  • jq
  • Set your IP2Location API key:
    export IP2LOCATION=your_api_key

πŸ›  Setup

chmod +x iplookup.sh
mkdir -p ~/.local/bin
cp iplookup.sh ~/.local/bin/iplookup

Make sure ~/.local/bin is in your PATH.

πŸ§ͺ Usage

iplookup 8.8.8.8
echo 8.8.8.8 | iplookup -m filter

πŸ” Output Modes

  • -m raw β†’ Raw JSON
  • -m jq (default) β†’ Pretty JSON via jq
  • -m filter β†’ Colorized summary

image

Bitcurrents, rkooyenga.github.io, ip2location CLI tool uses IP2Location.io IP geolocation web service.

#!/bin/bash
# IP2Location Lookup Script
# Usage: ip2location.sh [-m raw|jq|filter] [IP]
# Author: Ray Kooyenga
# Version: 0.4
# Dependencies: jq, curl
# IP2LOCATION API key must be in the IP2LOCATION env variable
# Color codes
RED="\e[1;31m"
YELLOW="\e[1;33m"
CYAN="\e[1;36m"
RESET="\e[0m"
print_usage() {
echo -e "${YELLOW}Usage:${RESET} $0 [-m raw|jq|filter] [IP]"
echo -e "${YELLOW}Example:${RESET} echo 8.8.8.8 | $0 -m filter"
exit 1
}
colored_label() {
echo -en "${CYAN}$1${RESET}"
}
lookup_ip() {
local ip="$1"
local mode="$2"
if [[ -z "$ip" ]]; then
echo -e "${RED}Error:${RESET} No IP address provided."
print_usage
fi
if [[ -z "$IP2LOCATION" ]]; then
echo -e "${RED}Error:${RESET} IP2LOCATION environment variable is not set."
exit 1
fi
echo -e "${YELLOW}Looking up:${RESET} $ip ..."
echo ""
response=$(curl -s -L -X GET "https://api.ip2location.io/?key=$IP2LOCATION&ip=$ip")
if [[ -z "$response" ]]; then
echo -e "${RED}Error:${RESET} No response received from IP2Location API."
exit 1
fi
case "$mode" in
raw)
echo "$response"
;;
jq)
echo "$response" | jq
;;
filter)
echo -e "${YELLOW}IP Address Info:${RESET}"
echo "----------------------------------------"
colored_label "ip : "; echo "$(echo "$response" | jq -r '.ip')"
colored_label "country code : "; echo "$(echo "$response" | jq -r '.country_code')"
colored_label "country : "; echo "$(echo "$response" | jq -r '.country_name')"
colored_label "region : "; echo "$(echo "$response" | jq -r '.region_name')"
colored_label "city : "; echo "$(echo "$response" | jq -r '.city_name')"
colored_label "zip code : "; echo "$(echo "$response" | jq -r '.zip_code')"
colored_label "ASN : "; echo "$(echo "$response" | jq -r '.asn')"
colored_label "ISP : "; echo "$(echo "$response" | jq -r '.isp')"
colored_label "time : "; echo "$(echo "$response" | jq -r '.time_zone_info.current_time')"
colored_label "gmt offset : "; echo "$(echo "$response" | jq -r '.time_zone_info.gmt_offset')"
colored_label "time code : "; echo "$(echo "$response" | jq -r '.time_zone_info.abbreviation')"
colored_label "usage type : "; echo "$(echo "$response" | jq -r '.usage_type')"
colored_label "fraud score : "; echo "$(echo "$response" | jq -r '.fraud_score')"
;;
*)
echo -e "${RED}Error:${RESET} Unknown mode '$mode'"
print_usage
;;
esac
}
# Default mode
MODE="jq"
IP=""
# Parse options
while [[ $# -gt 0 ]]; do
case "$1" in
-m|--mode)
MODE="$2"
shift 2
;;
-*)
echo -e "${RED}Unknown option:${RESET} $1"
print_usage
;;
*)
IP="$1"
shift
;;
esac
done
# Support piped input
if [[ -z "$IP" && ! -t 0 ]]; then
read -r IP
fi
lookup_ip "$IP" "$MODE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment