1QuickQuestion
1QuickQuestion

Reputation: 748

Jsch ssh to Rasperry Pi does nothing

I have been searching all over the internet for answers and I keep ending up with the same results, but all I want to do is send command line instructions to my raspberry pi via ssh. Forgive me if I have completely missed something really obvious, but I know I am able to connect because if I type in the incorrect username or password I get an auth failure. So I am connected, but nothing seems to be happening on my raspberry pi. This following is my current code:

package com.name.app.prynsofpi;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.ByteArrayOutputStream;
import java.util.Properties;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new AsyncTask<Integer, Void, Void>(){
            @Override
            protected Void doInBackground(Integer... params){
                try {
                    executeRemoteCommand("pi", "green1", "10.1.1.47", 22);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

        }.execute(1);

    }

    public static String executeRemoteCommand(String usrname, String passwd, String hostname, int Port) throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(usrname, hostname, 22);
        session.setPassword(passwd);

        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);

        session.connect();

        ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        channelssh.setOutputStream(baos);

        channelssh.setCommand("ls");
        channelssh.connect();
        channelssh.disconnect();
        return baos.toString();
    }
}

Can someone please show me what is wrong??? Should I see the commands on the raspberry pi in terminal?

Upvotes: 0

Views: 524

Answers (2)

Ros&#225;rio P. Fernandes
Ros&#225;rio P. Fernandes

Reputation: 11344

Can someone please show me what is wrong???

I don't see anything wrong.

Should I see the commands on the raspberry pi in terminal?

Like ChrisStillwell said, you are creating a new terminal process and the terminal running on your pi's screen won't show the sent commands. If you're trying to see the result of your sent command, you can store it in a String variable (To later display it on a TextView or Toast):

@Override
            protected Void doInBackground(Integer... params){
                try {
                    String result = executeRemoteCommand("pi", "green1", "10.1.1.47", 22);
                    Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

Upvotes: 1

Chris Stillwell
Chris Stillwell

Reputation: 10547

You won't see the commands on the pi's terminal. When you establish an ssh connection you are creating a new terminal process, the terminal running on your pi's screen won't show typing or commands, that's not how ssh works. You will need to capture the output and display it on your Android device.

You can run who on your pi to see what accounts are connected via ssh.

Upvotes: 2

Related Questions