Skip to content

Commit 0b85f5c

Browse files
committed
Added tools.py
1 parent 2998558 commit 0b85f5c

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

Networking-Tools/tools.py

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
"""
2+
This module contains tools such as IP Locator, IP Finder,
3+
Pinging and Port Scanner.
4+
DISCLAIMER: Please consider using Port Scanner in educational purposes only.
5+
6+
7+
## TOOLS DESCRIPTION
8+
### IP Locater
9+
Takes the IP address and with the help of an [API](https://ip-api.com/), extracts data about the IP address.
10+
Then writes the extracted data in two different files with the same name "ip_data". One of the files is a JSON file. The other one is
11+
a text file. These files will be saved on the system's desktop.
12+
13+
### IP Finder
14+
Takes the domain name and extracts the IPs that are relevant with the domain name.
15+
It uses windows `nslookup` command to get IPs.
16+
17+
### Pinging
18+
Takes the domain name or IP address and then pings it.
19+
It uses `ping` command to ping the domain name or IP address.
20+
21+
### Port Scanner
22+
Takes the domain name or IP address. After that, it gets the range of the ports to scan.
23+
Then it starts scanning to look for open ports. The Port Scanner will only look for open ports.
24+
So if a port is not open, it will not show it on the screen.
25+
"""
26+
27+
28+
# Import necessary modules
29+
import subprocess
30+
import platform
31+
import requests
32+
import pyfiglet
33+
import datetime
34+
import socket
35+
import json
36+
import os
37+
import re
38+
39+
40+
# Make the foreground color of the terminal green.
41+
subprocess.call("color A", shell=True)
42+
43+
# A pattern to validate an IP address.
44+
ip_pattern = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
45+
46+
47+
# Tools
48+
def locate_ip():
49+
"""
50+
Locate an IP address using an API.
51+
"""
52+
# Clear up the terminal.
53+
subprocess.call("cls", shell=True)
54+
# Banner of the tool.
55+
banner = pyfiglet.figlet_format("IP LOCATER")
56+
print(banner)
57+
# Get the IP address. Check the input whether it is a valid IP address.
58+
# This while loop will run until it gets a valid IP address that matches the IP pattern.
59+
while True:
60+
IP_address = input("\nEnter a valid IP address: ")
61+
# Check if the given input whether it is 'q'.
62+
# If it is, then it will abort operation and quit program.
63+
if IP_address.lower() == 'q':
64+
quit()
65+
# Validating the given IP address.
66+
if not re.match(ip_pattern, IP_address):
67+
print("Invalid IP address")
68+
continue
69+
else:
70+
break
71+
# Change current working directory to system's desktop
72+
os.chdir(rf"C:\Users\{os.getlogin()}\Desktop")
73+
# File names containing data of the IP address.
74+
file_name_json = "ip_data.json"
75+
file_name_text = "ip_data.txt"
76+
# The files containing IP data will have these fields.
77+
fields = "status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,currency,isp,org,as,mobile,proxy,hosting,query"
78+
# Sending a request to extract data.
79+
url = f"http://ip-api.com/json/{IP_address}?fields={fields}"
80+
response = requests.get(url)
81+
# Write the extracted data in the files.
82+
# Write on both JSON file and text file.
83+
with open(file_name_json, 'w') as ip_data_file_json:
84+
json.dump(response.json(), ip_data_file_json, indent=4)
85+
with open(file_name_text, 'w') as ip_data_file_text:
86+
ip_data_file_text.write(response.text)
87+
# Let the user know that files created on the system's desktop.
88+
print("You got the files containing data about the given IP address.")
89+
print("Please check your system desktop.")
90+
input("\nPress any key to continue...")
91+
92+
93+
def get_ip():
94+
"""
95+
Get the IP address of a certain domain name.
96+
"""
97+
subprocess.call("cls", shell=True)
98+
# Banner of the tool.
99+
banner = pyfiglet.figlet_format("IP FINDER")
100+
print(banner)
101+
# Get the domain name.
102+
# Check if the given input whether it is 'q'.
103+
# If it is, then it will abort operation and quit program.
104+
domain_name = input("\nEnter a valid domain name: ")
105+
if domain_name.lower() == 'q':
106+
quit()
107+
# Get the IP of the domain name.
108+
command = f"nslookup {domain_name}"
109+
subprocess.call(command) == 0
110+
input("\nPress any key to continue...")
111+
112+
113+
def ping():
114+
"""
115+
Ping a domain or website or IP address.
116+
"""
117+
subprocess.call("cls", shell=True)
118+
# Banner of the tool.
119+
banner = pyfiglet.figlet_format("PING")
120+
print(banner)
121+
# Get the host to ping.
122+
# Host can be a domain name or an IP address.
123+
# Check the given input whether it is 'q'.
124+
# If it is, then abort operation and quit program.
125+
host = input("Enter a valid domain name or IP address: ")
126+
if host.lower() == 'q':
127+
quit()
128+
# If the os is Windows, then the parameter is "-n".
129+
# If not, the parameter is "-c"
130+
if platform.system().lower() == "windows":
131+
parameter = "-n"
132+
else:
133+
parameter = "-c"
134+
# It will send 5 packages to ping the host.
135+
command = f"ping {parameter} 5 {host}"
136+
subprocess.call(command) == 0
137+
input("\nPress any key to continue...")
138+
139+
140+
def port_scanner():
141+
"""
142+
Scan ports on a certain host.
143+
"""
144+
subprocess.call("cls", shell=True)
145+
# Banner of the tool.
146+
banner = pyfiglet.figlet_format("PORT SCANNER")
147+
print(banner)
148+
print("For scanning using a domain, enter <domain>.\nFor scanning using an IP, enter <ip>")
149+
# All this terrifying while loop does is to check the given input at "scan_type".
150+
# If it is "q", then it will abort operation and quit program.
151+
# If it is "ip", then it will validate the IP address. If it is valid, then it will break the loop.
152+
# If it is "domain", then it will get the domain name and break the loop.
153+
# If it is none of the above, it will let the user know that the input is not valid.
154+
while True:
155+
scan_type = input(">>> ")
156+
if scan_type.lower() == 'q':
157+
quit()
158+
if scan_type.lower() == 'ip':
159+
while True:
160+
host = input("Enter IP for scanning:\n>>> ")
161+
if host.lower() == 'q':
162+
quit()
163+
if not re.match(ip_pattern, host):
164+
print("Invalid IP address.\n")
165+
continue
166+
else:
167+
break
168+
break
169+
elif scan_type.lower() == 'domain':
170+
host = input("Enter domain for scanning:\n>>> ")
171+
if host.lower() == 'q':
172+
quit()
173+
break
174+
else:
175+
print("Invalid input.\n")
176+
continue
177+
# Get the IP address of the domain name if a domain name is given.
178+
hostIP = socket.gethostbyname(host)
179+
# Get the port range.
180+
port_range = input("Enter port range in format <start>-<end> (ex: 20-80):\n>>> ")
181+
# Split the starting port and the ending port.
182+
port_range = port_range.split("-")
183+
# Make a neat banner again containing IP address and the time that scanning started.
184+
print("_"*60)
185+
print("Scanning ports on host: ", hostIP)
186+
start_time = datetime.datetime.now()
187+
print("Scan started at ", start_time)
188+
print("_"*60)
189+
print("\nPort\t\t\tStatus\n")
190+
# Start scanning ports
191+
for port in range(int(port_range[0]), int(port_range[1])+1):
192+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
193+
# Timeout time for scanning each port is half a second.
194+
s.settimeout(0.5)
195+
connection = s.connect_ex((hostIP, port))
196+
# Check whether the port is open or not.
197+
# If it is open, print it.
198+
# If it is not open, pass the closed port.
199+
if connection == 0:
200+
print(f"{port}\t----------\tOpen")
201+
else:
202+
pass
203+
s.close()
204+
# Scan ending time.
205+
end_time = datetime.datetime.now()
206+
# Time taken to scan.
207+
time_taken = end_time - start_time
208+
# Make a neat banner again containing the time that scanning ended and the time taken to scan.
209+
print("_"*60)
210+
print("Scan ended at ", end_time)
211+
print("Time taken: ", time_taken)
212+
print("_"*60)
213+
input("\nPress any key to continue...")

0 commit comments

Comments
 (0)