Python端口扫描仪卡在循环中

时间:2017-01-11 18:51:22

标签: python sockets port-scanning

我一直在关注如何制作一个简单的端口扫描程序的指南,我正在尝试扫描自己的IP,但它陷入循环并且没有打印端口。很难弄清楚它没有给出任何错误并陷入循环中。

非常感谢任何帮助。

import socket
import subprocess
import sys
from datetime import datetime

#clears the shell screen
subprocess.call('clear', shell=True)

#ask for input
remoteServer = raw_input("Please enter a host to scan:")
remoteServerIP = socket.gethostbyname(remoteServer)

#print a banner saying we are scanning
print "-" * 60
print "now scanning your host...", remoteServerIP
print "-" * 60

#Check what time the scan started
t1 = datetime.now()

# Using the range function to specify which ports (1 - 1025)

#Errors.

try:
  for port in range(1, 1025):
    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    result = sock.connect_ex((remoteServerIP, port))
    if result == 0:
        #if the socket is listening it will print out the port
      print("Port{}:\t Open".format(port))
    sock.close()

except KeyboardInterrupt:
  print "You pressed ctrl+c"
  sys.exit()

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

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

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

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

#printing the info to screen
print "scanning compelte in :", total

2 个答案:

答案 0 :(得分:1)

您可以使用sock.timeout(0.1),因此不会等待连接。

我放print port查看扫描的端口。

您可以尝试8.8.8.8 - 没有sock.timeout(0.1)它挂在第一个端口上。

也许您拥有良好的安全计算机并阻止与关闭端口的连接。

import sys
from datetime import datetime
import socket

#ask for input
remoteServer = raw_input("Please enter a host to scan: ")
remoteServerIP = socket.gethostbyname(remoteServer)

#print a banner saying we are scanning
print "-" * 60
print "now scanning host ...", remoteServerIP
print "-" * 60

#Check what time the scan started
t1 = datetime.now()

# Using the range function to specify which ports (1 - 1025)

#Errors.

try:
  for port in range(1, 1025):
    print port
    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    sock.settimeout(0.1)

    result = sock.connect_ex((remoteServerIP, port))
    if result == 0:
        #if the socket is listening it will print out the port
        print("Port {:4d}: Open".format(port))
    sock.close()

except KeyboardInterrupt:
  print "You pressed ctrl+c"
  sys.exit()

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

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

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

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

#printing the info to screen
print "scanning compelte in:", total

BTW:

您可以将结果与nmap

等工具的结果进行比较

请参阅scapy - python模块以使用网络包。 (书:Black Hat Python

答案 1 :(得分:-1)

至少在我的机器上(Ubuntu 16.something)它确实有效。输出:

Please enter a host to scan:localhost
------------------------------------------------------------
now scanning your host... 127.0.0.1
------------------------------------------------------------
Port21:  Open
Port22:  Open
Port25:  Open
Port80:  Open
Port139:         Open
Port443:         Open
Port445:         Open
Port631:         Open
scanning compelte in : 0:00:00.047478

但是,它只扫描端口1-1024,而端口最多可达65535。 要使其扫描所有端口,请将for port in range(1, 1025):更改为for port in range(1, 65536):