简体   繁体   English

如何扫描所有IP地址范围?

[英]How I scan all the IP-Addresses range/s?

In MainActivity top I added array variable: 在MainActivity顶部,我添加了数组变量:

private final String[] ipaddresses = new String[]{
            "http://10.0.0.1:8098/?cmd=nothing",
            "http://10.0.0.3:8098/?cmd=nothing",
            "http://10.0.0.2:8098/?cmd=nothing"};

Then with a button click I use a for loop to iterate over each IP address . 然后单击按钮,使用for loop遍历每个IP address I check with a web server on my PC on which IP the PC is sitting. 我在PC上使用IP的PC上的Web服务器进行检查。 I send a command to the server and get back a result. 我向服务器发送命令并获取结果。

The question is, how I can build an array with all the logic available IP addresses? 问题是,如何构建具有所有逻辑可用IP地址的阵列? Since my PC can be sometimes on IP 10.0.0.2 and when I restart the pc or move to another PC the IP can be something else like 10.0.0.3 由于我的PC有时可以使用IP 10.0.0.2,因此当我重新启动PC或移动到另一台PC时,该IP可能是其他类似10.0.0.3的IP

This is the button code where I also loop over the IPs 这是我也在IP上循环的按钮代码

public void addListenerOnButton()
    {

        btnClick = (Button) findViewById(R.id.checkipbutton);

        btnClick.setOnClickListener(new OnClickListener()
        {
            byte[] response = null;
            @Override
            public void onClick(View arg0)
            {

                text = (TextView) findViewById(R.id.textView2);


                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < ipaddresses.length; i++)

                        {

                                try
                                {
                                    response = Get(ipaddresses[i]);
                                }
                                catch (Exception e)
                                {
                                    String err = e.toString();
                                }

                                if (response!=null)
                                {



                                    try
                                    {
                                        final String a = new String(response,"UTF-8");




                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                text.setText(a);
                                            }
                                        });

                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                    }

                                    Logger.getLogger("MainActivity(inside thread)").info("test1");
                                    break;

                                }

                                else
                                {

                                }

                            text.post(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    text.setText("Connection Failed");
                                }
                            });



                        }

                    }
                });
                t.start();
            }
        });

    }

New class: 新课程:

package com.test.webservertest;

/**
 * Created by bout0 on 8/4/2015.
 */
public class IpAddresses
{
        private static String ipMain = "10.0.0.";
        private static int minIpRange = 0;
        private static int maxIpRange = 255;
        private static String[] ipAddresses = new String[maxIpRange];

        public static void main(String[] args) {
            for (int i = minIpRange; i < maxIpRange; i++) {
                String ipRange = ipMain + i;
                //IP range in Array
                //ipAddresses[i] = ipRange;

                //if you need full IP range in Url, then unmark the following 2 lines and mark above line
                String UrlRange = "http://" + ipRange + ":8098/?cmd=nothing";
                ipAddresses[i] = UrlRange;
            }

            //testing array results
            for (int i = minIpRange; i < maxIpRange; i++)
                System.out.println(ipAddresses[i]);
        }

}

Generating a set of IP Addresses from 10.0.0.0 to 10.0.0.255 is as simple as iterating through the list of valid digits for the last segment: 生成一组从10.0.0.0到10.0.0.255的IP地址,就像遍历最后一个网段的有效数字列表一样简单:

List<String> addresses = new ArrayList<>();
int min = 0;
int max = 255;
for(int i = min; i <= max; i++) {
    String address = "10.0.0." + i;
    addresses.add(address);
}

Now the addresses list has all of the valid IP addresses in it. 现在, addresses列表中包含所有有效IP地址。 What addresses are generated can be tweaked by adjusting the min and max values to shrink the available range of addresses. 通过调整最小值和最大值以缩小可用地址范围,可以调整生成的地址。

Here is my suggestion: 这是我的建议:

public class IpRange {
    private static String ipMain = "10.0.0.";
    private static int minIpRange = 2;
    private static int maxIpRange = 255;
    private static String[] ipAddresses = new String[maxIpRange];

    public static void main(String[] args) {
        for (int i = minIpRange; i < maxIpRange; i++) {
            String ipRange = ipMain + i;
            //IP range in Array
            ipAddresses[i] = ipRange;

            //if you need full IP range in Url, then unmark the following 2 lines and mark above line
            //String urlRange = "http://" + ipRange + ":8098/?cmd=nothing";
            //ipAddresses[i] = urlRange;
        }

        //testing array results
        for (int i = minIpRange; i < maxIpRange; i++)
            System.out.println(ipAddresses[i]);
    }
}

IP addresses range in Array IP地址范围在阵列中

10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
etc.

Or the output will be like this as complete Url if required (just modify the code as noted): 否则,如果需要,输出将像这样作为完整的URL(只需按照说明修改代码):

http://10.0.0.2:8098/?cmd=nothing
http://10.0.0.3:8098/?cmd=nothing
http://10.0.0.4:8098/?cmd=nothing
http://10.0.0.5:8098/?cmd=nothing
http://10.0.0.6:8098/?cmd=nothing
etc.

Note: the class is made for demonstration and is just example how this task can be achieved, it is up the individual to modified it and implement it in the relative code, to fulfill the final requirement. 注意:该类仅供演示之用,仅是如何完成此任务的示例,由个人来修改它并在相关代码中实现,以满足最终要求。


EDIT I have since optimized the code here is updated version: 编辑以来,我已经优化了代码,这里是更新版本:

public class IpRange {
    private String ipMain = "10.0.0.";
    private int startRange = 2; // 2 = 10.0.0.2
    private int endRange = 5;   // 5 = 10.0.0.5
    private int ipRangeLength = endRange - startRange;
    private String urlParam = ":8098/?cmd=nothing";

    public static void main(String[] args) {

        IpRange ipRange = new IpRange();

        // true -> ip range url with parameters / false -> only ip range
        String[] results = ipRange.ipRangeGenerator(true);

        for (String output : results)
            System.out.println(output);

    }

    public String[] ipRangeGenerator(boolean link) {
        String[] ipAddresses = new String[ipRangeLength];
        String urlRange;

        for (int i = 0; i < ipRangeLength; i++) {
            String ipRange = ipMain + (startRange + i);
            urlRange = ipRange;
            if (link)
                urlRange = "http://" + ipRange + urlParam;
            ipAddresses[i] = urlRange;
        }
        return ipAddresses;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM