经过设定范围后,端口扫描程序3.5 Python打印问题

时间:2016-09-03 12:59:00

标签: python sockets python-3.x

import socket
import sys
from datetime import datetime

#Ask the user for input, the form of a remote host entire in the IP address of the target machine
remoteServer =input ("Enter a remote host to scan:")
remoteServerIP =socket.gethostbyname(remoteServer)

#Print a block of text with information on which host we are about to scan.
#While scanning display a message so the user knows the program is working and isn't frozen
print ("_"*60)
print ("Please wait, currently scanning remote host", remoteServerIP)
print ("_"*60)

#Show/check the time scan started
t1 = datetime.now()

#range function to specify ports, this case I have set the pogram to go through ports 1 to 150
# port in range works like so
try:
     for port in range (1, 150):
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         result = sock.connect_ex((remoteServerIP, port))
         if result == 0:
            print ("Port {}:     Open".format(port))
         sock.close()

# Press Ctrl C to leave the application
except KeyboardInterrupt:
    print ("You pressed Ctrl+C")
    sys.exit()

except socket.gaierror:
    print ('Hostname could not be resolved. Exiting')
    sys.exit()

except socket.error:
    print ("Couldn't connect to server")
    sys.exit()

# Checking the time again
t2 = datetime.now()

# Calculates the difference of time, to see how long it took to run the script
total =  t2 - t1

# Printing the information to screen
print ('Scanning Completed in: ', total)

我的老板告诉我开始学习Metasploitable2和Kali Linux,因为我试图创建一个端口扫描器,但它似乎对大部分工作都很好。如果已完成扫描其设定范围内的端口,则完全关闭,然后打印('扫描已完成:',总计)与列出的结果。我在这做错了什么?而且我很清楚脚本速度很慢,我将在稍后尝试使其成为多线程。

0 个答案:

没有答案