来自应用程序

时间:2017-03-03 17:54:40

标签: java shell android-studio jsch

我创建了一个通过wifi连接到我的覆盆子pi的应用程序并启动了一个python脚本,但出于某种原因,当我尝试在文本视图中打印终端响应时,他们不断提出奇怪的字符而不是可读的ascii。 主动发送shell命令的函数是我的代码末尾的“PyPiCommands”。有人可以解释我缺少用明文ASCII字符打印终端响应的内容:

//The following method starts the initial ssh connection======================

public String executeRemoteCommand(String usrname, String passwd, String hostname, int Port) throws Exception {

    responseArray.add("Attempting to Connect...");
    Properties prop = new Properties();
    prop.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    final Session session = jsch.getSession(usrname, hostname, Port);
    session.setPassword(passwd);
    session.setConfig(prop);
    session.connect();
    if (session.isConnected()) {
        piConnected = Boolean.TRUE;
    } else {
        responseArray.clear();
        Log.i("ssh", "Connection Failed");
        responseArray.add("Connection Failed.");
    }

    final ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    channelssh.setOutputStream(baos);
    channelssh.setOutputStream(baos);
    channelssh.setCommand(command);
    InputStream in = channelssh.getInputStream();
    channelssh.connect();

    if (channelssh.isConnected()) {
        System.out.println("channel connected");

        if (!channelssh.isClosed()) {
            System.out.println("channel not closed");
        }

        final BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
        String result = bufferReader.readLine();
        for (int count = 0; count < result.length(); count++) {
            System.out.println("outstring: " + result);
            result = bufferReader.readLine();
            this.responseArray.add(count, result);
            if (result == null) {
                break;
            }
        }

        System.out.println("");
        System.out.println("response array: " + responseArray.toString());

        //channelssh.disconnect();
        //session.disconnect();

        final Channel channel = session.openChannel("shell");

        final OutputStream inputStream = channel.getOutputStream();
        final PrintStream commander = new PrintStream(inputStream, true);

        channel.setOutputStream(System.out, true);
        channel.connect();
        final String finalResult = result;
        final Runnable checkCommand = new Runnable() {
            @Override
            public void run() {
                try {
                    PyPiCommands(channel, command, inputStream, commander);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSchException e) {
                    e.printStackTrace();
                }
                if (sessKill == Boolean.FALSE) {
                    handler1.postDelayed(this, 200);
                }
            }
        };
        handler1.postDelayed(checkCommand, 300);

    }
    return responseArray.toString();

}

以下方法是打开的shell会话,用于运行python脚本,该脚本请求用户输入执行不同的远程操作。在从应用程序发送的每个命令之后,我在终端中收到shell响应。

public void PyPiCommands(Channel channel, String command, OutputStream inputStream, PrintStream commander) throws IOException, JSchException {

        if (newCmdRcv == Boolean.TRUE) {
            System.out.println("running new handler");
            newCmdRcv = Boolean.FALSE;
            try {

                if(channel.isConnected() == Boolean.TRUE){
                    Log.i("ssh", "channel connected");
                }
                else{
                    Log.i("ssh", "channel NOT connected");
                }
                if(channel.isClosed() == Boolean.TRUE){
                    Log.i("ssh", "channel IS closed");
                }
                else{
                    Log.i("ssh", "channel not closed");
                }
                commander.println(command);
                System.out.println("printing rcv: " +commander.toString());
                do {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } while (channel.isEOF());


            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

当我在显示器中打印指令数据时,这就是我所看到的:

03-03 13:26:11.410 25133-25133/com.princton.litmo.prynsofpi I/System.out: printing rcv: java.io.PrintStream@d584d0a

但是我知道它正在收到,我只是不确定如何存储字符串并将其打印在应用程序上的textview而不是上面的文本中。

03-03 13:26:11.740 25133-26877/com.app I/System.out: 5
03-03 13:26:11.740 25133-26877/com.app I/System.out: 5
03-03 13:26:11.740 25133-26877/com.app I/System.out: running
03-03 13:26:11.740 25133-26877/com.app I/System.out: straight
03-03 13:26:11.910 25133-26877/com.app I/System.out: Gimme dat: 

0 个答案:

没有答案