Skip to content

Commit 83064e5

Browse files
authored
Merge pull request ndleah#148 from BazarganDev/main
Added my own project to the repo.
2 parents a67d88d + fb01fef commit 83064e5

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

IP_Locator/LocateIP.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Import necessary modules
2+
import subprocess
3+
import platform
4+
import requests
5+
import pyfiglet
6+
import json
7+
import os
8+
import re
9+
10+
11+
def locate_ip():
12+
"""
13+
Locate an IP address using an API.
14+
"""
15+
# Clear the terminal
16+
if platform.system().lower() == 'windows':
17+
command = "cls"
18+
else:
19+
command = "clear"
20+
subprocess.call(command, shell=True)
21+
22+
# Banner of the tool.
23+
banner = pyfiglet.figlet_format("IP LOCATER")
24+
print(banner)
25+
26+
# A pattern to validate an IP address
27+
IP_PATTERN = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
28+
29+
# Get the IP address.
30+
# if it matches the IP pattern, then it is valid.
31+
# If not, raise an error message and try again.
32+
while True:
33+
IP_address = input("\nEnter a valid IP address: ")
34+
if IP_address.lower() == 'q':
35+
quit()
36+
if not re.match(IP_PATTERN, IP_address):
37+
print("Invalid IP address.")
38+
else:
39+
break
40+
41+
# Change the current working directory to system's desktop
42+
os.chdir(rf"C:\Users\{os.getlogin()}\Desktop")
43+
44+
# Files to save IP data
45+
filename_json = "ip_data.json"
46+
filename_text = "ip_data.txt"
47+
48+
# The files containing IP data will have these fields
49+
fields = "status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,currency,isp,org,as,mobile,proxy,hosting,query"
50+
51+
# Send a request to the API
52+
url = f"http://ip-api.com/json/{IP_address}?fields={fields}"
53+
response = requests.get(url)
54+
55+
# Write the extracted data to files
56+
with open(filename_json, 'w') as ip_data_file_json:
57+
json.dump(response.json(), ip_data_file_json, indent=4)
58+
59+
with open(filename_text, 'w') as ip_data_file_text:
60+
ip_data_file_text.write(response.text)
61+
62+
# Let the user know that files created on the system's desktop.
63+
print("You got the files containing data about the given IP address.")
64+
print("Please check your system desktop.")
65+
input("\nPress any key to continue...")
66+
67+
def get_ip():
68+
"""
69+
Get the IP of a certain domain name.
70+
"""
71+
# Clear the terminal
72+
if platform.system().lower() == 'windows':
73+
command = "cls"
74+
else:
75+
command = "clear"
76+
subprocess.call(command, shell=True)
77+
78+
# Banner of the tool.
79+
banner = pyfiglet.figlet_format("IP FINDER")
80+
print(banner)
81+
82+
# Get the domain name
83+
domain_name = input("Enter a domain name: ")
84+
85+
if domain_name.lower() == 'q':
86+
quit()
87+
else:
88+
command = f"nslookup {domain_name}"
89+
90+
subprocess.call(command) == 0
91+
input("\nPress any key to continue...")

IP_Locator/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# IP Tools
2+
3+
## Description
4+
Tools:
5+
- IP Locater
6+
- IP Finder
7+
8+
IP Locater extracts data of an IP address.
9+
10+
IP Finder gets the IP of a domain name.
11+
12+
## Languages Used
13+
This tool is written in Python 3.
14+
15+
Modules Used:
16+
- pyfiglet
17+
- requests
18+
19+
## How To Run
20+
Clone the project using git:
21+
22+
`git clone https://github.com/BazarganDev/python-mini-project.git`
23+
24+
Go to project `IP_Locater`
25+
26+
Install the required modules using pip:
27+
28+
`pip install -r requirements.txt`
29+
30+
run `main.py`
31+
32+
## Demo
33+
![screenshot](https://github.com/BazarganDev/python-mini-project/assets/124906353/cc148ef9-ecf8-4321-b2cd-70518c890a02)
34+
35+
## Author
36+
[Mohammad Bazargan](https://github.com/BazarganDev)

IP_Locator/main.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Import necessary modules
2+
import subprocess
3+
import LocateIP
4+
5+
6+
def Display_Menu():
7+
header = """
8+
__________________________
9+
| IP Locater |
10+
| IP Finder |
11+
|___________ ___________|
12+
\ \
13+
^_ _^
14+
(o o)\_________
15+
(_ _)\ )/\/
16+
U ||----W||
17+
|| ||
18+
### Tools:
19+
[1] Locate an IP address
20+
[2] Get the IP address of a domain
21+
[3] Exit
22+
23+
* Type <clear> to clear previous commands
24+
* Type <q> or to abort operation of a tool
25+
"""
26+
print(header)
27+
28+
def option_1():
29+
"""
30+
Option 1 --> IP Locater
31+
"""
32+
LocateIP.locate_ip()
33+
Display_Menu()
34+
Home()
35+
36+
def option_2():
37+
"""
38+
Option 2 --> IP Finder
39+
"""
40+
LocateIP.get_ip()
41+
Display_Menu()
42+
Home()
43+
44+
def option_3():
45+
"""
46+
Option 3 --> Exit the program
47+
"""
48+
quit()
49+
50+
51+
def Home():
52+
"""
53+
Home of the program is selecting an option from available options.
54+
"""
55+
# Available options' numbers as shown in the menu
56+
available_options = (1, 2, 3)
57+
58+
# Get the option's number.
59+
# If it is wrong, this while loop will run unless it gets
60+
# a proper number that is related with one of the available options.
61+
while True:
62+
try:
63+
selected_option = input("\nEnter your option\n>>> ")
64+
# In order to clear the mess created by the previous commands,
65+
# we define 'clear' command to clear up the screen from previous commands.
66+
if selected_option == 'clear':
67+
subprocess.call('cls', shell=True)
68+
Display_Menu()
69+
continue
70+
else:
71+
selected_option = int(selected_option)
72+
except ValueError:
73+
print("Please enter the number of the option")
74+
continue
75+
else:
76+
# If the given option number is not available, try to get an available option number.
77+
if selected_option not in available_options:
78+
print("The option is not available.\nTry another one.")
79+
continue
80+
else:
81+
break
82+
83+
# Operate the option related to the given number
84+
if selected_option == 1:
85+
option_1()
86+
elif selected_option == 2:
87+
option_2()
88+
elif selected_option == 3:
89+
option_3()
90+
91+
92+
# Run the program
93+
def Start_Program():
94+
Display_Menu()
95+
Home()
96+
97+
Start_Program()

IP_Locator/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pyfiglet
2+
requests

0 commit comments

Comments
 (0)