Skip to content

Instantly share code, notes, and snippets.

@JAMIEFRASER675
Created May 1, 2023 16:42
Show Gist options
  • Save JAMIEFRASER675/70b8a51c37511a7f94d14802376d5d5f to your computer and use it in GitHub Desktop.
Save JAMIEFRASER675/70b8a51c37511a7f94d14802376d5d5f to your computer and use it in GitHub Desktop.
Port scan on host IP
import socket
import time
from datetime import datetime
#function to use connect command for host and port
def portVerify(ipadd,port):
soc = socket.socket()
try:
soc.connect((ipadd,port))
except:
return False
else:
return True
scanFile = open("ScanFile", 'w')
#user input
ipadd = input("Enter a host IP address: ")
MinRange = int(input("Enter starting port number: "))
MaxRange = int(input("Enter ending port number: "))
verify = range(MinRange,MaxRange+1,1)
#current time
now = datetime.now()
dt_format = now.strftime("%m-%d-%Y %H:%M:%S")
start = time.time()
#write current time to file
with open("ScanFile", 'a') as scanFile:
scanFile.write("Scan started at: ")
scanFile.write(str(dt_format))
scanFile.write("\n")
scanFile.write("Host: ")
scanFile.write(ipadd)
scanFile.write("\n")
scanFile.close()
#verify valid ip address was entered
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try:
hostIP = socket.gethostbyname(ipadd)
print("You have entered a VALID IP address!" + "\n", "Continuing with scan...")
break
except:
print("You have entered an INVALID IP address.")
exit()
try:
soc.connect((hostIP, 10))
# socket.settimeout(400)
print("This should not connnet...ALERT!!")
except socket.error as e:
result = str(e)
# print("Couldn't connect. Aborting.")
print(result)
soc.close()
except KeyboardInterrupt:
print("You pressed CTRL + C")
exit()
if result.startswith("[WinError 10061]"):
print("Host is UP. SCANNING....", dt_format, "\n")
with open("ScanFile", 'a') as scanFile:
scanFile.write(ipadd)
scanFile.write(" is responding.")
scanFile.write("\n")
scanFile.write("Scan beginning at ")
scanFile.write(str(dt_format))
scanFile.write("\n")
scanFile.close()
else: #WindowsError(10060):
print("Host is not available. Aborting scan.")
with open("ScanFile", 'a') as scanFile:
scanFile.write(ipadd)
scanFile.write(" is unreachable. Aborted.")
scanFile.write("\n")
scanFile.write(str(dt_format))
scanFile.write("\n")
scanFile.close()
exit()
#check for each port in range
for x in verify:
if portVerify(ipadd,x):
print("Port", x, "is OPEN")
with open("scanFile", 'a') as scanFile:
scanFile.write("Port ")
scanFile.write(str(x))
scanFile.write(" is OPEN \n")
scanFile.close()
else:
print("Port", x, "is CLOSED")
with open("scanFile", 'a') as scanFile:
scanFile.write("Port ")
scanFile.write(str(x))
scanFile.write(" is CLOSED \n")
scanFile.close()
print("Scan completed. Port range", MinRange, "-", MaxRange, "has been scanned.")
end = time.time()
duration = end - start
now = datetime.now()
dt_format = now.strftime("%m-%d-%Y %H:%M:%S")
print("Scan completed at",dt_format)
print("Total scan time: ", duration, "seconds.")
with open("ScanFile", 'a') as scanFile:
scanFile.write("Scan completed at ")
scanFile.write(str(dt_format))
scanFile.write("\n")
scanFile.write("Total scan time: ")
scanFile.write(str(duration))
scanFile.write(" seconds.")
scanFile.write("\n")
scanFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment