使用http请求ifconfig.co获取与Java中私有IP相关联的公共IP

发布于 2025-01-28 05:22:15 字数 722 浏览 2 评论 0原文

我编写了一个代码,以获取与每个接口相关联的所有私人IP。我正在将所有私人IP存储在arraylist ips中。

List<String> ips = new ArrayList<>();
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
       NetworkInterface intf = en.nextElement();
       for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    ips.add(enumIpAddr.nextElement().toString().replace("/", ""));
       }
}

现在,我想在列表中迭代IPS,并想调用HTTP请求以获取公共IP。以下是Linux Curl命令,它将返回与私有IP 10.74.4.11关联的公共IP。

curl -i --interface 10.74.4.11 "ifconfig.co"

我只想将此linux命令转换为可以在程序中使用的Java HTTP请求。

I have written a code to get all the private ips associated with each interface. I'm storing all the private ips in ArrayList ips.

List<String> ips = new ArrayList<>();
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
       NetworkInterface intf = en.nextElement();
       for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    ips.add(enumIpAddr.nextElement().toString().replace("/", ""));
       }
}

Now I want to iterate over the List ips and want to call the http request to get the public ip. Below is the linux curl command that will return public ip associated with the private ip 10.74.4.11.

curl -i --interface 10.74.4.11 "ifconfig.co"

I just want convert this linux command to java http request which I can use in my program.

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

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

发布评论

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

评论(1

趴在窗边数星星i 2025-02-04 05:22:15

此方法使用Java Runtime API执行命令,然后将发现的公共IP添加到Hashmap中,作为内部Internip密钥的值。因此,最后,您可以通过搜索hashmap“ ips.get(“ internalip”)”来找到所需的公共IP。

private static void getIps() throws IOException {
            Map<String, String> ips = new HashMap<>();
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    ips.put(enumIpAddr.nextElement().toString().replace("/", ""), "");
                }
            }
            for (String ip: ips.keySet())
            {
                String command="curl --interface "+ ip+" ifconfig.co";
                System.out.println("Excecuting: "+command);
                Process process = Runtime.getRuntime().exec(command);
                try {
                    process.waitFor();
                    final int exitValue = process.waitFor();
                    if (exitValue == 0) {
                        System.out.println("Successfully executed the command: " + command);
                       List<String> result = new BufferedReader(new InputStreamReader(process.getInputStream()))
                        .lines().collect(Collectors.toList());
                       ips.put(ip, result.get(result.size()-1));//the public IP will be the last item in the result stream
                    }
                    else {
                        System.out.println("Failed to execute the following command: " + command + " due to the following error(s):");
                        try (final BufferedReader b = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                            String line;
                            if ((line = b.readLine()) != null)
                                System.out.println(line);
                        } catch (final IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }



            }
            System.out.println(ips);

        }

This method uses Java Runtime API to execute the command and then adds the found public ip to the hashmap as a value to the internalIp key. So finally you can find the desired public ip by searching through the hashmap "ips.get("internalIp")"

private static void getIps() throws IOException {
            Map<String, String> ips = new HashMap<>();
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    ips.put(enumIpAddr.nextElement().toString().replace("/", ""), "");
                }
            }
            for (String ip: ips.keySet())
            {
                String command="curl --interface "+ ip+" ifconfig.co";
                System.out.println("Excecuting: "+command);
                Process process = Runtime.getRuntime().exec(command);
                try {
                    process.waitFor();
                    final int exitValue = process.waitFor();
                    if (exitValue == 0) {
                        System.out.println("Successfully executed the command: " + command);
                       List<String> result = new BufferedReader(new InputStreamReader(process.getInputStream()))
                        .lines().collect(Collectors.toList());
                       ips.put(ip, result.get(result.size()-1));//the public IP will be the last item in the result stream
                    }
                    else {
                        System.out.println("Failed to execute the following command: " + command + " due to the following error(s):");
                        try (final BufferedReader b = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                            String line;
                            if ((line = b.readLine()) != null)
                                System.out.println(line);
                        } catch (final IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }



            }
            System.out.println(ips);

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