简体   繁体   English

Android java InputStreamReader

[英]Android java InputStreamReader

I am trying to make an application that will allow me to issue commands in android and give me the results. 我正在尝试制作一个允许我在android中发出命令并给我结果的应用程序。 This seems to be working fine with the exception that commands that wait a while for more output do not get processed correctly. 这似乎工作得很好,除了等待一段时间以等待更多输出的命令没有得到正确处理。 I am trying to issue the command "su && nmap -sS 192.168.1.1" and all I get for output is that nmap has started. 我正在尝试发出命令“ su && nmap -sS 192.168.1.1”,而我得到的输出只是nmap已启动。 Does anyone know of a way to not only get the output that nmap has started, but the results of the scan using a modified version of the code below. 有谁知道一种方法,不仅可以获取nmap已启动的输出,还可以获取使用以下代码的修改版本的扫描结果。

try {
        EditText inputTxt = (EditText) findViewById(R.id.input);
        String str = inputTxt.getText().toString();
        Process command = Runtime.getRuntime().exec(str);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(command.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(output);

    }
    catch (IOException e) {
        Log.e("Run_Command", Log.getStackTraceString(e));
    }

I came up with another version of the code. 我想出了另一个版本的代码。 This one uses thread.sleep and does not have a fixed size buffer. 这一个使用thread.sleep,并且没有固定大小的缓冲区。

Still all I get for output is that nmap has started, not the results of the scan even though the scan would have completed by the time the final thread.sleep was finished. 我得到的输出仍然是nmap已经启动,而不是扫描的结果,即使扫描将在最终thread.sleep完成时完成。

public void command(View view)
{
    String me;
    int a = 0;
    try {
        EditText inputTxt = (EditText) findViewById(R.id.input);
        String str = inputTxt.getText().toString();
        Process command = Runtime.getRuntime().exec(str);

        BufferedReader bReader = new BufferedReader(
                new InputStreamReader(command.getInputStream(), "UTF8"));
        //char [] me = new char[40960];
        String inputLine;
        while(1 == 1){
            if(a == 5) {
                break;}

        while ((inputLine = bReader.readLine()) != null) {
            me += inputLine + "\n";
        }
            while(bReader.readLine() == null) {
                a++;
                Thread.sleep(5000);
                if (a == 5) {
                    break;
                }
            }
                    }
        bReader.close();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(me);

    }
    catch (IOException e) {
        Log.e("Run_Command", Log.getStackTraceString(e));
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
}

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

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