-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipeeinfo.py
323 lines (259 loc) · 9.95 KB
/
ipeeinfo.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Kourva
# Source: https://github.com/Kourva/IpeeInfo
# IpeeInfo is a top-of-the-line IP information tool
# that boasts a wide range of features to help you
# gain insight into any IP address. Whether you're
# looking for the geolocation of a specific IP, its
# latitude and longitude coordinates, or the city
# it's based in, iPeeInfo has got you covered.
# What sets iPeeInfo apart from other IP information
# tools is its versatility. With its command-line
# interface, you can easily specify multiple IP
# addresses to be scanned at once, making it perfect
# for network administrators who need to gather
# information about large numbers of IP addresses
# quickly. Additionally, iPeeInfo allows you to
# choose between three different output formats
# (text, JSON, or CSV), so you can customize your
# output depending on your needs.
# But that's not all - iPeeInfo also includes an option
# to save the results of your scan, allowing you to
# refer back to them later if needed. This feature
# is particularly useful if you're performing scans
# on a regular basis and need to keep track of your
# findings.
# Overall, iPeeInfo is a powerful and reliable IP
# information tool that is easy to use and packed
# with features. Whether you're a seasoned IT
# professional or new to the field, iPeeInfo is an
# essential tool for anyone who needs to gather data
# about IP addresses.
# Libraries (All are built-in) (No need to install)
# anything with pip package installer
import threading
import itertools
import requests
import argparse
import logging
import time
import json
import sys
import os
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Colors
org = "\033[0;33;40m" # Orange
wht = "\033[0;37;40m" # White
grn = "\033[0;32;40m" # Green
red = "\033[0;31;40m" # Red
rst = "\033[m" # Reset
# Ip search function
def ip_to_location(target, ipformat="text", output=None):
"""
IP search function that retrieves the location data for a
given IP address.
:param target:
The target IP address to search for.
:param ipformat:
The desired format for the output (text, json, or csv)
Default is 'text'.
:param output:
Optional output file path. If provided, the result
will be saved to this file.
:returns:
If no output file path is provided, the result will be
printed to the console. Otherwise, the result
will be saved to the specified output file path.
"""
# Make request with session to API
with requests.Session() as session:
response = session.get(
f"https://freeipapi.com/api/json/{target}", stream=True, timeout=10
).json()
# Extract relevant fields from response
ipAddress = response["ipAddress"]
ipVersion = response["ipVersion"]
latitude = response["latitude"]
longitude = response["longitude"]
countryCode = response["countryCode"]
countryName = response["countryName"]
timeZone = response["timeZone"]
cityName = response["cityName"]
regionName = response["regionName"]
# Stop animation
event.set()
print()
# Enter Statement if Format is Text (Default)
if ipformat == "text":
# Store output
output_result = (
f"{org} |\n{org}[*] General Info\n {org}|{rst}\n "
f"{org}|--{rst} {wht}IP Address :{rst} {grn}{ipAddress}\n "
f"{org}|--{rst} {wht}Version :{rst} {grn}{ipVersion}\n {org}|\n"
f"[*] Geo Location\n {org}|{rst}\n {org}|--{rst} {wht}Latitude :{rst} "
f"{grn}{latitude}\n {org}|--{rst} {wht}Longitude :{rst} {grn}{longitude}\n "
f"{org}| \n[*] Address Info\n {org}|{rst}\n {org}|--{rst} {wht}"
f"Country code :{rst} {grn}{countryCode}\n {org}|--{rst} {wht}"
f"Country name :{rst} {grn}{countryName}\n {org}|--{rst} {wht}"
f"Time Zone :{rst} {grn}{timeZone}\n {org}|--{rst} {wht}"
f"City name :{rst} {grn}{cityName}\n {org}|--{rst} {wht}"
f"Region name :{rst} {grn}{regionName}\n\n\n"
)
# Print output if no save option requested
if output == None:
print(output_result)
return
# Otherwise save the result
else:
with open(f"{os.getcwd()}/{ipAddress}.txt", "w") as out:
out.write(
output_result.replace(org, "")
.replace(rst, "")
.replace(grn, "")
.replace(wht, "")
)
print(f"{org} |\n{org}[*] Saved to {os.getcwd()}/{ipAddress}.txt\n")
return
# Enter Statement if Format is json
elif ipformat == "json":
# Store output
output_result = (
f"{org} |\n{org}[*] Json Output {rst} "
f"{grn}{json.dumps(response, indent=4)}{rst}"
f"\n\n\n"
)
# Print output if no save option requested
if output == None:
print(output_result)
return
# Otherwise save the result
else:
with open(f"{os.getcwd()}/{ipAddress}.json", "w") as out:
out.write(
output_result.replace(f"{org} |\n{org}[*] Json Output {rst} ", "")
.replace(org, "")
.replace(rst, "")
.replace(grn, "")
.replace(wht, "")
)
print(f"{org} |\n{org}[*] Saved to {os.getcwd()}/{ipAddress}.json\n")
return
# Enter Statement if Format is csv
elif ipformat == "csv":
# Store output
output_result = (
f"{org}ipAddress,ipVersion,latitude,longitude,countryCode,countryName,timeZone,cityName,regionName{rst}\n"
f"{grn}{ipAddress},{ipVersion},{latitude},{longitude},{countryCode},{countryName},{timeZone},{cityName},{regionName}{rst}\n"
)
# Print output if no save option requested
if output == None:
print(output_result)
return
# Otherwise save the result
else:
with open(f"{os.getcwd()}/{ipAddress}.csv", "w") as out:
out.write(
output_result.replace(org, "").replace(rst, "").replace(grn, "")
)
print(f"{org} |\n{org}[*] Saved to {os.getcwd()}/{ipAddress}.csv\n")
return
# Define the thread event variable (Used to stop thread)
event = threading.Event()
# Function to animate loading prompt
def animate_loading():
# Animation actions
sequence = ["-", "/", "|", "\\"]
dots = [". ", ".. ", "... ", "...."]
# Cycle between all indexes of two lists
for char, dot in itertools.cycle(zip(sequence, dots)):
# Stop thread when we set even
if event.is_set():
break
print(f"\r{org}[{char}]{rst} {wht}Searching {dot}{rst}", end="")
time.sleep(0.5)
# Main Function
def ipeeinfo(ipaddr=[], ipformat="text", output=None):
"""
This function takes an IP address as input and retrieves
information about the location of that IP address using
the ip_to_location function.
:param ipaddr:
A list or string containing one or more IP addresses
to retrieve information for.
If not provided, the user will be prompted to enter a
target IP address.
:param ipformat:
The format in which to retrieve the IP location data
(default is "text").
:param output:
The file path to save the retrieved IP location data
to (default is None).
:return:
None, but the retrieved IP location data will be
printed to the console and/or saved to a file.
"""
# Get target from input if there is no argument Ip address
if ipaddr == []:
target = input(f"{org}[?]{rst} {wht}Enter Target IP Address:{rst} {org}")
print(f" |{rst}")
ipaddr.append(target)
# Otherwise use argument IP(s)
else:
target = ipaddr
# Get Information of target(s) for each
for target in ipaddr:
print(f"{org}[*] IpeeInfo: Starting Process!")
# Create animation thread
animation = threading.Thread(target=animate_loading, daemon=True)
# Try to get Info
try:
print(f"{org} | {rst}")
event.clear()
animation.start()
# Call Ip to location function for each IP
response = ip_to_location(target, ipformat, output)
# Error Handling and Keyboard Interrupt
except Exception as ex:
logger.info(f"\n{org} | {rst}\n{red}[x] Error: {ex!r}{rst}", exc_info=True)
except KeyboardInterrupt:
logger.info(f"\n{org} | {rst}\n{red}[x] Exit code requested by user.{rst}")
sys.exit()
# Set the arguments and run the main function
if __name__ == "__main__":
# Initialize the argument parser
parser = argparse.ArgumentParser(
description="Powerful Ip Info Script made in Python"
)
# Add options to parser
# Option 1 -> Ip option
parser.add_argument(
"ip",
nargs="*",
help="iP address(es) to lookup"
)
# Option 2 -> format option
parser.add_argument(
"-f",
choices=["text", "json", "csv"],
default="text",
help="output format (text or json or csv)"
)
# Option 3 -> save option
parser.add_argument(
"-o",
action="store_true",
help="save result(s) of scan"
)
# Set the arguments
args = parser.parse_args()
# Handle arguments
if args.ip:
ip_args = [args.ip]
else:
ip_args = []
output_format = args.f or "text"
output_path = args.o or None
ipeeinfo(ip_args, output_format, output_path)