Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a project that I am struggling to complete. Please could anyone write python code for port scanning. The program would ask user to

I have a project that I am struggling to complete. Please could anyone write python code for port scanning. The program would ask user to input a host IPV4 address, and then ping the host IP address to check if the host is UP or DOWN. If host is down the program will print() Host is down. you may enter another IPV4 address butIf the host is up, it will return Host UP and ask user to enter the port rang Start, end) for port scanning. After the port scanning the result should print and include the current time the port scan was performed and the total time it took to complete the scan. Please, can anyone assist me with the python code?

The code below didn't work:

import os

import re

import socket

import time

import threading

from queue import Queue

regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(

25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(

25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(

25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$'''

def check(Ip):

if(re.search(regex, Ip)):

print("Valid Ip address")

return True

else:

print("Invalid Ip address")

return False

print("Enter an Ip address")

while(1):

s=input()

b=check(s)

if(b==1):

host_up= True if os.system("ping -c 5"+s.strip(";")) is 1 else True

if(host_up):

print("The Host is down")

print("Enter X to exit")

c=input()

if(c=='X'):

continue;

else:

print("Host is up")

print("Are you want to continue port Scanning? y/n")

d=input()

if(d=='y'):

print("enter the range of port to be scanned")

#start=input()

#end=input()

#port_scan(start,end)

socket.setdefaulttimeout(0.25)

print_lock = threading.Lock()

target = input('Enter the host to be scanned: ')

t_IP = socket.gethostbyname(target)

print ('Starting scan on host: ', t_IP)

def portscan(port):

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:

con = s.connect((t_IP, port))

with print_lock:

print(port, 'is open')

con.close()

except:

pass

def threader():

while True:

worker = q.get()

portscan(worker)

q.task_done()

q = Queue()

startTime = time.time()

for x in range(10):

t = threading.Thread(target = threader)

t.daemon = True

t.start()

print("enter port range")

x=int(input())

y=int(input())

for worker in range(x,y):

q.put(worker)

q.join()

print('start time is:', starttime)

print('end time is:', time.time())

print('total time is:', starttime-time.time())

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions