Simple Port Scanner in Python using SOCKET Library -Getting started with Python for Network Security
Code is available at:
https://github.com/cloudsecuritylabs/...
import socket
A class to scan ports using simple Python
class PortScanner:
def __init__(self, ip):
self.ip = ip
self.open_ports = []
Scan a range of ports
def scan_ips(self, starting_ip, ending_ip):
for port in range(starting_ip, ending_ip + 1):
print(f'scanning {port} ....')
if port is open update the list
if self.is_open(port):
self.open_ports.append(port)
this function checks for open port using socket library
def is_open(self, port):
s = socket.socket()
exit_code = s.connect_ex((self.ip, port))
s.close()
if exit_code == 0:
return True
else:
return False
write open ports to a file of your choosing
def write_to_file(self, path_to_file):
with open(path_to_file, "a") as f:
for port in self.open_ports:
print(port)
f.write(str(port) + "\n")
def main():
ip = input("Enter the IP to scan: ")
starting_port = int(input("Enter the lower end of port to scan: "))
ending_port = int(input("Enter the upper end of port to scan: "))
scanner = PortScanner(ip)
scanner.scan_ips(starting_port, ending_port)
print(scanner.open_ports)
scanner.write_to_file("open_ports.txt")
if _name_ == '__main__':
main()
On this page of the site you can watch the video online Simple Port Scanner in Python using SOCKET Library -Getting started with Python for Network Security with a duration of hours minute second in good quality, which was uploaded by the user Cloud Security Training & Consulting 24 April 2022, share the link with friends and acquaintances, this video has already been watched 589 times on youtube and it was liked by 1 viewers. Enjoy your viewing!