如何扫描远程主机使用的端口?

发布于 2024-08-13 06:22:43 字数 753 浏览 7 评论 0原文

这是我到目前为止得到的代码...

import java.net.*;

public class PortScanner {

    public static void main(String args[]) {
        int startPortRange = 0;
        int stopPortRange = 0;

        startPortRange = Integer.parseInt(args[0]);
        stopPortRange = Integer.parseInt(args[1]);

        for (int i = startPortRange; i <= stopPortRange; i++) {
            try {
                Socket ServerSok = new Socket("127.0.0.1", i);

                System.out.println("Port in use: " + i);

                ServerSok.close();
            } catch (Exception e) {

            System.out.println("Port not in use: " + i);
        }
    }
}

} 例如,在 smtp 服务器上,系统使用端口 25,我需要扫描连接到 smtp 服务器的远程客户端上的端口。我怎样才能做到这一点?

请帮我。

this is the code that i get so far...

import java.net.*;

public class PortScanner {

    public static void main(String args[]) {
        int startPortRange = 0;
        int stopPortRange = 0;

        startPortRange = Integer.parseInt(args[0]);
        stopPortRange = Integer.parseInt(args[1]);

        for (int i = startPortRange; i <= stopPortRange; i++) {
            try {
                Socket ServerSok = new Socket("127.0.0.1", i);

                System.out.println("Port in use: " + i);

                ServerSok.close();
            } catch (Exception e) {

            System.out.println("Port not in use: " + i);
        }
    }
}

}
for example on smtp server the system use port 25, and i need to scan port on remote client that connected to the smtp server. how can i do that?

please help me.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-08-20 06:22:43

您的代码应如下所示:

    for (int i = startPortRange; i <= stopPortRange; i++) {
            try {
                    Socket serverSok = new Socket("127.0.0.1", i);

                    System.out.println("Port in use: " + i);

                    serverSok.close();
            } catch (Exception e) {
                    System.out.println("Port not in use: " + i);
            }
    }

Your code should look like this:

    for (int i = startPortRange; i <= stopPortRange; i++) {
            try {
                    Socket serverSok = new Socket("127.0.0.1", i);

                    System.out.println("Port in use: " + i);

                    serverSok.close();
            } catch (Exception e) {
                    System.out.println("Port not in use: " + i);
            }
    }
感悟人生的甜 2024-08-20 06:22:43

如果我理解你的问题:

  • 进程S在机器A上运行并侦听端口P上的传入连接
  • 进程C在机器B上运行并从端口T连接到A上的P
  • 你的程序F在机器A上运行
  • F不是S

你吗想用Java发现F中的T吗?你不能!

您必须阅读打开套接字的操作系统表。这是一个依赖于操作系统的操作,java 不支持它。

否则,如果F是S,则可以更改S的代码来发现T。如果S使用java.net.ServerSocket类,则可以在返回的Socket实例上使用getPort()搜索ServerSocket.accept()并发现T。

安德里亚律师事务所

If I understood your problem:

  • a process S runs on machine A and listens on port P for incoming connections
  • a process C runs on machine B and connects from port T to P on A
  • your program F runs on machine A
  • F is not S

Do you want to discover T in F using Java? You cannot!

You have to read the operative system table of open sockets. This is an operative system dependent operation and it is not supported by java.

Otherwise if F is S, you can change the code of S to discover T. If S uses java.net.ServerSocket class, you can search ServerSocket.accept() and discover T using getPort() on the returned Socket instance.

LLP, Andrea

梦旅人picnic 2024-08-20 06:22:43

首先,这种方法存在一些方法上的问题,因为端口可以被使用,但不能监听。

如果您实际上只是在寻找侦听端口,并且想要使用上面的方法,那么至少生成一些线程,这样您就不必等待每个端口依次超时。

最好的方法是模仿 syn 扫描之类的东西,而不是进行完整的 TCP 握手,但我不确定您是否可以在 Java 中达到如此接近的效果。

不过,这是一个很好的问题。

Firstly there are some methodological problems with this way of doing it since a port can be in use, but not listening.

If you're actually just looking for listening ports, and you want to use the approach above, at least spawn off some threads so that you don't have to wait for each port to time out sequentially.

The best way would be to mimic something like a syn scan rather than doing a full TCP handshake, but I'm not sure you can get that close to the metal in Java.

Neat question though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文