Project B: Python Programming
Project B of Information Security Bootcamp we are introduced to Python programming thru pycharm 3.9 with an interpreter. Goal for this Python program project was to test the given host for all TCP Open Ports within a range of 1-1025. I was required to accomplish this task by using the standard Python's "socket" library, which took a lot of research for the proper updates and font packages. Please see code below for more details.
# Danyon Port Scanner
# Importing needed Libraries
import datetime
import socket
# Create a txt file for our results
scn_fl = open("Danyon Port Scan.txt", "w")
scn_fl.write("Greetings people\n\n")
# Ask the user for a target & obtain the IP address of the target
target = input("Pick a host, any host:")
host_ip = socket.gethostbyname(target)
print("{} : {}".format(target, host_ip))
# Timestamp the start of the scan
t1 = datetime.datetime.today()
print(t1)
scn_fl.write("Starting time is " + str(t1) +"\n\n")
# Initial set up for Scan loop
port = 1
choice = "y"
# Run our Scan loop
while choice =="y":
pn_cnt = 0
for port in range(1, 1026):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
status = sock.connect_ex((host_ip, port))
socket.setdefaulttimeout(1)
if status == 0:
print("{} : {} code= {} ".format(target, port, status))
scn_fl.write("{} : {} is open\n".format(target, port))
pn_cnt = 1
port += 1
scn_fl.write("Total open ports: {}" .format(pn_cnt))
# Ask for an additional target
Choice = input("Do you want to scan another target? (y/n)")
if choice == "y":
target = input("Pick a host, any host:")
host_ip = socket.gethostbyname(target)
# Ending Timestamp
t2 = datetime.datetime.today()
#Calculate the total time as t3
t3 = t2 - t1
scn_fl.write("the start time was {}\nThe ending time was {}\nThe total scan time was{} "
"\nTHANK YOU!!!".format(t1,t2,t3))
print("the start time was {}\nThe ending time was {}\nThe total scan time was{} \nTHANK YOU!!!".format(t1,t2,t3))
scn_fl.close()