端口扫描返回在不存在的网络机上打开的所有端口

发布于 2025-01-31 14:36:53 字数 1005 浏览 6 评论 0原文

我正在为我的课程做一个项目,其中涉及为IP地址创建端口扫描仪。它涉及一个网络地址的用户输入,然后将生成地址列表和端口的TXT文件。我或多或少地工作了,但是注意到了一些我无法解释或找到答案的奇怪的东西。

基本上,当我测试我目前正在使用的网络(标准192.168家庭网络)时,它似乎可以按预期工作。扫描端口并报告它们是开放还是关闭的时间。例如,我的机器将返回打开的正确端口,而不存在的机器将列出其所有端口为关闭。

但是,如果我放置了另一个网络(例如192.169.0.0),它会立即循环通过所有报告所有端口的地址,似乎没有扫描它们。这对我来说没有意义,尤其是因为较早的不存在的机器返回了我的期望。

我已经尝试搜索它并在这里搜索,但找不到答案(一个问题没有解决)。这是我要调用的特定功能;

def sockCheck(x):
    for y in portList:
        try:
            location = (str(hosts[x]), int(y))
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            c = s.connect_ex(location)
            if c == 0:
                print("Port "+y+" open")
            else:
                print("Port "+y+" closed")
            print(" ")
            s.close()
        except:
            print("Port "+y+" unavailable")
            s.close()

我知道这不是很好(仍在学习)。作为参考,portlist是导入的port.txt文件和hosts [x]的端口列表。生成的IP地址(我需要单独检查其端口)。

如果要求,我将发布其他代码。

I'm doing a project for my course that involves creating a port scanner for IP addresses. It involves a user input for a network address that will then generate a list of addresses and a txt file for the ports. I've got it more or less working, but have noticed something strange that I can't explain or find an answer for.

Basically, when I'm testing the network I'm currently on (standard 192.168 home network) it seems to work as intended. It takes time to scan the port and reports whether they're open or closed. For example, my machine will return the correct ports being open while a non-existent machine will list all its ports as closed.

However, if I put in a different network (say 192.169.0.0) it instantly loops through all of the addresses reporting all ports are open, seemingly without scanning them. And that's what doesn't make sense to me, especially since the earlier non-existent machine returns closed as I'd expect.

I've tried googling it and searching around here but couldn't find an answer (one question went unanswered). This is the specific function I'm calling;

def sockCheck(x):
    for y in portList:
        try:
            location = (str(hosts[x]), int(y))
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            c = s.connect_ex(location)
            if c == 0:
                print("Port "+y+" open")
            else:
                print("Port "+y+" closed")
            print(" ")
            s.close()
        except:
            print("Port "+y+" unavailable")
            s.close()

I know it's not great (still learning). For reference, portList is the list of ports from the imported port.txt file and hosts[x] is how I'm looping through my generated IP addresses (that I need to individually check their ports).

I'll post other code if requested.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文