-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipchecker.py
More file actions
executable file
·34 lines (34 loc) · 1002 Bytes
/
ipchecker.py
File metadata and controls
executable file
·34 lines (34 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
"""IPChecker - small CLI to fetch IP info (requests)
Usage: python3 ipchecker.py <IP or host>
"""
import sys, requests
API1 = "http://ip-api.com/json/{}"
API2 = "https://ipinfo.io/{}/json"
def fetch(target):
out={}
try:
r=requests.get(API1.format(target), timeout=6)
if r.ok: out['ip-api']=r.json()
except: pass
try:
r2=requests.get(API2.format(target), timeout=6)
if r2.ok: out['ipinfo']=r2.json()
except: pass
return out
def pretty(d):
if not d:
print('No data.')
return
for src, info in d.items():
print('Source:', src)
for k in ('query','ip','city','region','country','loc','org','timezone','isp'):
if k in info: print(' {}: {}'.format(k, info.get(k)))
def main():
if len(sys.argv)<2:
print('Usage: python3 ipchecker.py <IP or hostname>')
sys.exit(1)
target=sys.argv[1]
data=fetch(target)
pretty(data)
if __name__=='__main__': main()