【问题标题】:Why is my Port Scanner not scanning ports in Python?为什么我的端口扫描器不扫描 Python 中的端口?
【发布时间】:2017-10-10 19:42:07
【问题描述】:

我的端口扫描器正在扫描(我假设)端口。但是,即使使用活动端口(例如端口 80),它仍然显示该端口已关闭。我做错了什么?

代码:

#!usr/bin/env python
import subprocess
import ipaddress
import socket


# Value to scan the network 192.168.2.0 till 192.68.2.14
net_addr = '192.168.2.0/28'

# Variables for the port numbers
portstart = 70
portend = 81

# Resolve hostname
host = socket.gethostname()

# Creates the network
ip_net = ipaddress.ip_network(net_addr)

# Get all hosts on the network
all_hosts = list(ip_net.hosts())

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

# Loop where the IP-address is being pinged.
for i in range(len(all_hosts)):
    output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE,
                              startupinfo=info).communicate()[0]

    if "Destination host unreachable" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    elif "Request timed out" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    else:
        print(str(all_hosts[i]), "is ONLINE!")
        print ("The hostname is:", host)
        for portnum in range (portstart, portend):
            try:
                s.connect(all_hosts,portnum)
                print("Port", portnum, "is OPEN!")
                s.close()

            except:
                print("Port", portnum, "is closed")

结果:https://gyazo.com/da7d1eebfe4c3ffe4082fafd519eced2

我关闭了防火墙和 Malwarebytes,但仍然无法正常工作。

【问题讨论】:

  • 变量 s 没有在你的 sn-p 中定义?

标签: python python-3.x networking


【解决方案1】:

找到了解决办法。我面临的问题是,IP 地址正在使用一个列表函数,我需要将它转换为字符串才能使用 s.connect_ex

#!usr/bin/env python
import subprocess
import ipaddress
from socket import *


# Value to scan the network 192.168.2.0 till 192.68.2.14
net_addr = '192.168.2.0/28'

# Variables for the port numbers
portstart = 79
portend =  140

# Resolve hostname
host = gethostname()

# Creates the network
ip_net = ipaddress.ip_network(net_addr)

# Get all hosts in the network
all_hosts = list(ip_net.hosts())

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

# Loop where the IP-address is being pinged.
for i in range(len(all_hosts)):
    output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE,
                              startupinfo=info).communicate()[0]

    if "Destination host unreachable" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")

    elif "Request timed out" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")

    else:
        print(str(all_hosts[i]), "is ONLINE!")
        print ("The hostname of", all_hosts[i], "is:", host)
        print ("Starting scan on host: ", host, "(", all_hosts[i], ")")

# Loop where it scans ports within a range.
        for portnum in range (portstart, portend):
                s = socket(AF_INET, SOCK_STREAM)

                result = s.connect_ex((str(all_hosts[i]), portnum))

                if (result == 0):
                    print ("Port", portnum, "is OPEN!")
                    s.close()

                else:
                    print("Port", portnum, "is closed")

【讨论】:

  • 发帖怎么样 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多