【问题标题】:How to read the output of jsch command in Android?如何在 Android 中读取 jsch 命令的输出?
【发布时间】:2017-11-22 15:16:46
【问题描述】:

我正在使用 JSCH 库构建一个 android 应用程序,以在远程 linux 服务器中执行命令,然后读取其输出。

我按照this question的回答,但没有解决我的问题。

以下是我的活动代码:

package com.example.pic_controller_2;

        import android.graphics.drawable.Drawable;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.app.Activity;
        import java.io.BufferedInputStream;
        import java.io.BufferedOutputStream;
        import java.io.ByteArrayOutputStream;
        import java.io.File;
        import java.io.FileOutputStream;
        import java.io.OutputStream;
        import com.jcraft.jsch.Channel;
        import com.jcraft.jsch.ChannelExec;
        import com.jcraft.jsch.JSch;
        import com.jcraft.jsch.JSchException;
        import com.jcraft.jsch.Session;
        import com.jcraft.jsch.ChannelSftp;
        import android.view.View;
        import android.os.AsyncTask;
        import android.widget.Button;
        import android.widget.ImageView;
        import android.widget.Toast;

public class TestActivity extends AppCompatActivity {

    private String aaaa;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void show_picture_func (View v) {   

        new AsyncTask<Integer, Void, Void>(){    

            @Override
            protected Void doInBackground(Integer... params) {

                String SFTPHOST = "192.168.0.1";
                int SFTPPORT = 22;
                String SFTPUSER = "pi";
                String SFTPPASS = "raspberry";
                String SFTPWORKINGDIR = "/home/pi/pic/";

                Session session = null;
                Channel channel = null;
                ChannelSftp channelSftp = null;

                try {
                    JSch jsch = new JSch();
                    session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
                    session.setPassword(SFTPPASS);
                    session.setConfig("StrictHostKeyChecking", "no");
                    session.setTimeout(10000);
                    session.connect();

                    ChannelExec channel1 = (ChannelExec)session.openChannel("exec");
                    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    channel1.setOutputStream(baos);
                    channel1.setCommand("ls /home/pi/pic | sed -n 5p");
                    aaaa = new String(baos.toByteArray());

                } catch (Exception ex) {
                    ex.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error Connecting", Toast.LENGTH_LONG).show();
                }
                return null;
            }


            @Override
            protected void onPostExecute(Void unused) {
                TextView textView1_2 = (TextView)findViewById(R.id.textView1);
                textView1_2.setText(aaaa);
            }


        }.execute(1);

    }

}

所以,执行命令的输出应该出现在文本视图中。但事实并非如此。文本视图变为白色!

有什么建议吗!?

【问题讨论】:

  • 第一个明显的问题是你从不调用channel1.connect(),所以命令永远不会执行。第二个问题是在调用toByteArray之前必须等待命令完成。
  • 感谢@MartinPrikryl 的评论。我很早就意识到了这个问题。我傻了!!我发布了另一个 linux-command 问题。

标签: android ssh jsch


【解决方案1】:

我更改了以下代码部分,来自:

ChannelExec channel1 = (ChannelExec)session.openChannel("exec");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
channel1.setOutputStream(baos);
channel1.setCommand("ls /home/pi/pic | sed -n 5p");
aaaa = new String(baos.toByteArray());

到这里:

ChannelExec channel1 = (ChannelExec)session.openChannel("exec");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
channel1.setOutputStream(baos);
channel1.setCommand("ls /home/pi/pic | sed -n 5p | xargs echo -n");
channel1.connect();
try{Thread.sleep(1000);}catch(Exception ee){}
aaaa = new String(baos.toByteArray());

问题解决了。

【讨论】:

    猜你喜欢
    • 2011-10-17
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2015-07-06
    • 2013-07-30
    • 2019-05-29
    相关资源
    最近更新 更多