Skip to content

Instantly share code, notes, and snippets.

@xdavidhu
Last active March 1, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdavidhu/8bde4d8d9b8b5b38e3c0465811454702 to your computer and use it in GitHub Desktop.
Save xdavidhu/8bde4d8d9b8b5b38e3c0465811454702 to your computer and use it in GitHub Desktop.
ipinfo.py - python ipinfo.io cli
#!/usr/bin/env python3
# author: @xdavidhu
import requests, json, sys, socket
if len(sys.argv) > 1:
ip = sys.argv[1]
else:
ip = False
if not ip:
url = "https://ipinfo.io/"
else:
try:
socket.inet_aton(ip)
domain = False
except socket.error:
domain = True
if domain:
try:
ip = socket.gethostbyname(ip)
except:
print("invalid domain name...")
sys.exit()
url = "https://ipinfo.io/" + str(ip)
try:
r = requests.get(url)
except:
print("error while querying info...")
sys.exit()
data = json.loads(r.text)
text = """
_,-===-,_ ipinfo v1.0 by @xdavidhu
,/ ",,/\8888.
/ ="\V=8)88\ target: {0}
, d88boodb="V. city: {1}
| (8888888?' '| region: {2}
`b " `8sm8" ' country: {3}
\ 8888,' / loc: {4}
` `T/ ' org: {5}
` ..... ' postal: {6}
"""
text = text.format(data.get("ip", "[NO DATA]"), data.get("city", "[NO DATA]"), \
data.get("region", "[NO DATA]"), \
data.get("country", "[NO DATA]"), \
data.get("loc", "[NO DATA]"), \
data.get("org", "[NO DATA]"), \
data.get("postal", "[NO DATA]"))
print(text)
@stufy
Copy link

stufy commented Mar 1, 2022

Good day and to convert ip-list to domain list ?
Tnx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment