Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: port scanner script #524

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Amazing Python Projects_Scripts/Port-Scanner-Script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Port-Scanner-Script

- This program is just to provide a sample code to generate a Port Scanner. This Port Scanner will work for both the Web Applications as well as remote Host.
- This tool has been created to provide the basic functionality of a Port Scanner
45 changes: 45 additions & 0 deletions Amazing Python Projects_Scripts/Port-Scanner-Script/scanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/python

import sys #Allow us to enter command line arguments, amoung other things
import socket
from datetime import datetime

#Define Our Target
if len(sys.argv) == 2:
target = socket.gethostbyname(sys.argv[1]) #Translate host name to IPV4
else:
print("Invalid amount of arguments.")
print("Syntax:python3 scanner.py <ip>")
sys.exit()

#Add pretty banner
print("-" * 50)
print("Scanning Target "+target)
print("Time Started: "+str(datetime.now()))
print("-" *50)

try:
for port in range(50,85):
#Setup connection vaiables
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #AF_INET is IPV4 and SOCK_STREAM is port
socket.setdefaulttimeout(1) # This is Float
result = s.connect_ex((target,port)) #returns error indicators
print("Checking port {}".format(port))
if result == 0:
print("Port {} is open".format(port))
s.close()

except KeyboardInterrupt:
print("\nExiting program.")
sys.exit()

except socket.gaierror: #If We cannot connect to the hostname
print("Hostname could nor resolved.")
sys.exit()

except socket.error: #If server is down or anything is worng
print("Could'nt connect to the server.")
sys.exit()