Skip to content

Instantly share code, notes, and snippets.

@gs-projekt
Created September 19, 2021 23:33
Show Gist options
  • Save gs-projekt/4c20143efeb652fe045e3dde3b93b70b to your computer and use it in GitHub Desktop.
Save gs-projekt/4c20143efeb652fe045e3dde3b93b70b to your computer and use it in GitHub Desktop.
import socket
import subprocess
import sys
from datetime import datetime
# Clear the screen
# subprocess.call('clear', shell=True)
import socket # importing library
from pyfiglet import Figlet
custom_fig = Figlet(font='digital')
print(custom_fig.renderText('GateKeeper Security'))
file = open(r"Port Scan.txt", "w")
## error handler##
# Ask for input
try:
hostName = input("What is the name of the host you wish to scan?")
ip = socket.gethostbyname(hostName)
except:
print("Unable to reach host. Program closing.")
file.write("Unable to reach host. The program has closed. \n")
exit()
# Print a nice banner with information on which host we are about to scan
else:
print("-" * 60)
print("Please wait, scanning remote host", ip)
print("-" * 60)
# Check what time the scan started
t1 = datetime.now()
format = "%m/%d/%Y %H:%M:%S"
time1 = t1.strftime(format)
print(f"The scan began at {time1}")
file.write(f"The scan began at {time1}. \n")
# Using the range function to specify ports (here it will scans all ports between 1 and 1024)
try:
for port in range(75,150):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
# returns an error indicator
result = s.connect_ex((ip, port))
if result == 0:
print("Port {} is open".format(port))
file.write("Port open : {} \n".format(port))
s.close()
except KeyboardInterrupt:
print("\n Exitting Program !!!!")
sys.exit()
except socket.gaierror:
print("\n Hostname Could Not Be Resolved !!!!")
sys.exit()
except socket.error:
print("\ Server not responding !!!!")
sys.exit()
# Checking the time again
t2 = datetime.now()
format = "%m/%d/%Y %H:%M:%S"
time2 = t2.strftime(format)
print(f"The scan ended at {time2}.")
file.write(f"The scan ended at {time2}. \n")
# Calculates the difference of time, to see how long it took to run the script
total = t2 - t1
# Printing the information to screen and file
print('Scanning Completed in: ', total)
file.write(f"Scanning Completed in: {total}. \n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment