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

Commit841632d

Browse files
authored
Create port_scanner_regex.py
1 parent17d37df commit841632d

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

‎port_scanner_regex.py

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp