Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit17d37df

Browse files
authored
Create port_scanner_ip_obj.py
1 parent9c171ac commit17d37df

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

‎port_scanner_ip_obj.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# The socket module in Python is an interface to the Berkeley sockets API.
2+
importsocket
3+
# We import the ipaddress module. We want to use the ipaddress.ip_address(address)
4+
# method to see if we can instantiate a valid ip address to test.
5+
importipaddress
6+
# We need to create regular expressions to ensure that the input is correctly formatted.
7+
importre
8+
9+
# Regular Expression Pattern to extract the number of ports you want to scan.
10+
# You have to specify <lowest_port_number>-<highest_port_number> (ex 10-100)
11+
port_range_pattern=re.compile("([0-9]+)-([0-9]+)")
12+
# Initialising the port numbers, will be using the variables later on.
13+
port_min=0
14+
port_max=65535
15+
16+
# This script uses the socket api to see if you can connect to a port on a specified ip address.
17+
# Once you've successfully connected a port is seen as open.
18+
# This script does not discriminate the difference between filtered and closed ports.
19+
20+
# Basic user interface header
21+
print(r"""______ _ _ ______ _ _
22+
| _ \ (_) | | | ___ \ | | | |
23+
| | | |__ ___ ___ __| | | |_/ / ___ _ __ ___ | |__ __ _| |
24+
| | | / _` \ \ / / |/ _` | | ___ \/ _ \| '_ ` _ \| '_ \ / _` | |
25+
| |/ / (_| |\ V /| | (_| | | |_/ / (_) | | | | | | |_) | (_| | |
26+
|___/ \__,_| \_/ |_|\__,_| \____/ \___/|_| |_| |_|_.__/ \__,_|_|""")
27+
print("\n****************************************************************")
28+
print("\n* Copyright of David Bombal, 2021 *")
29+
print("\n* https://www.davidbombal.com *")
30+
print("\n* https://www.youtube.com/davidbombal *")
31+
print("\n****************************************************************")
32+
33+
open_ports= []
34+
# Ask user to input the ip address they want to scan.
35+
whileTrue:
36+
ip_add_entered=input("\nPlease enter the ip address that you want to scan: ")
37+
# If we enter an invalid ip address the try except block will go to the except block and say you entered an invalid ip address.
38+
try:
39+
ip_address_obj=ipaddress.ip_address(ip_add_entered)
40+
# The following line will only execute if the ip is valid.
41+
print("You entered a valid ip address.")
42+
break
43+
except:
44+
print("You entered an invalid ip address")
45+
46+
47+
whileTrue:
48+
# You can scan 0-65535 ports. This scanner is basic and doesn't use multithreading so scanning all
49+
# the ports is not advised.
50+
print("Please enter the range of ports you want to scan in format: <int>-<int> (ex would be 60-120)")
51+
port_range=input("Enter port range: ")
52+
# We pass the port numbers in by removing extra spaces that people sometimes enter.
53+
# So if you enter 80 - 90 instead of 80-90 the program will still work.
54+
port_range_valid=port_range_pattern.search(port_range.replace(" ",""))
55+
ifport_range_valid:
56+
# We're extracting the low end of the port scanner range the user want to scan.
57+
port_min=int(port_range_valid.group(1))
58+
# We're extracting the upper end of the port scanner range the user want to scan.
59+
port_max=int(port_range_valid.group(2))
60+
break
61+
62+
# Basic socket port scanning
63+
forportinrange(port_min,port_max+1):
64+
# Connect to socket of target machine. We need the ip address and the port number we want to connect to.
65+
try:
66+
# Create a socket object
67+
# You can create a socket connection similar to opening a file in Python.
68+
# We can change the code to allow for domain names as well.
69+
# With socket.AF_INET you can enter either a domain name or an ip address
70+
# and it will then continue with the connection.
71+
withsocket.socket(socket.AF_INET,socket.SOCK_STREAM)ass:
72+
# You want to set a timeout for the socket to try and connect to the server.
73+
# If you make the duration longer it will return better results.
74+
# We put it at 0.5s. So for every port it scans it will allow 0.5s
75+
# for a successful connection.
76+
s.settimeout(0.5)
77+
# We use the socket object we created to connect to the ip address we entered and the port number.
78+
# If it can't connect to this socket it will cause an exception and the open_ports list will not
79+
# append the value.
80+
s.connect((ip_add_entered,port))
81+
# If the following line runs then then it was successful in connecting to the port.
82+
open_ports.append(port)
83+
84+
except:
85+
# We don't need to do anything here. If we were interested in the closed ports we'd put something here.
86+
pass
87+
88+
# We only care about the open ports.
89+
forportinopen_ports:
90+
# We use an f string to easily format the string with variables so we don't have to do concatenation.
91+
print(f"Port{port} is open on{ip_add_entered}.")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp