0

I'm building an android app using JSCH library to execute a command in a remote linux server, and then read its output.

I followed the answer of this question, but didn't solve my problem.

The following is my activity code:

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);

    }

}

So, the output of the executed command should appear in the textview. But it doesn't. The textview changes to white!

Any suggestions !?

2
  • 2
    The first obvious problem is that you never call channel1.connect(), so the command is never executed. The second problem is that you have to wait for the command to complete before calling toByteArray. Commented Nov 22, 2017 at 15:28
  • Thanks @MartinPrikryl for your comment. I realized this problem earlier. Silly of me!! There were another linux-command issue, which I posted. Commented Nov 22, 2017 at 15:59

1 Answer 1

1

I changed the following section of the code, from this:

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());

to this:

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());

And the problem solved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.