2

I am trying to establish an SSH connection using the Ganymed library. However, when using the library, it seems that after creating a Connection object to the host I want to connect to, it doesn't end up connecting to it and instead goes straight to the catch block of my code. This application will be connecting to my Raspberry Pi to execute a few commands.

Can anyone explain to me why this may be happening? I've looked everywhere online and on this site, and I can't seem to find any solutions for this. Do I need some additional files that it needs to reference for doing the SSH? I would really appreciate it if I could get some assistance on this. The following is the code for the class that tries to establish the SSH connection.

package com.sys.videostreamer; 
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

public class Options extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);
        Button startssh = (Button) findViewById(R.id.camera_start);
        System.out.println("pass");
        startssh.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String hostname = "10.X.X.XX";
                String username = "username";
                String password = "password";

                try {
                    // create a connection instance and connect to it
                    Connection ssh = new Connection(hostname);

                    ssh.connect();
                    boolean authorized = ssh.authenticateWithPassword(username,
                            password);
                    if (authorized == false)
                        throw new IOException(
                                "Could not authenticate connection, please try again.");

                    // if authorized, create the session
                    Session session = ssh.openSession();
                    session.execCommand("mkdir test");

                    // terminate the session
                    session.close();

                    // terminate the connection
                    ssh.close();
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                    System.out.println(e.getMessage());
                    //System.exit(2);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.options, menu);
        return true;
    }

}
4
  • 1
    What error are you getting? Can you provide the stack trace? Can you login via SSH to the RPi from the computer that you are running the program on? Commented Nov 30, 2013 at 16:24
  • Apparently it isn't throwing any errors, and the stack trace doesn't seem to be displaying anything. I have connected to the Raspberry Pi using Cygwin on the desktop I am running the application from. Could this issue be because that the application isn't accepting the certificate from the Raspberry Pi? Commented Nov 30, 2013 at 16:42
  • 1
    That was my first thought, that you should try adding the RPi's SSH certificate manually. I don't know anything about that library, but usually when you encounter a new cert when running the client form the CLI, it prompts for assent to accept the cert. So try adding the RPi's cert, or perhaps seeing if the library has a config param for auto-accepting new certs. Commented Nov 30, 2013 at 19:03
  • Chances are you're performing a network operation on the main thread, which is forbidden in Android since many versions. To be sure you should post the stacktrace of the exception. Commented Nov 21, 2018 at 14:57

1 Answer 1

1

It is probably not working because you do a networking task on main thread. In android all networking requests have to be done in a background thread like this:

final Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                // create a connection instance and connect to it
                Connection ssh = new Connection(hostname);

                ssh.connect();
                final boolean authorized = ssh.authenticateWithPassword(username,
                        password);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!authorized)
                            Toast.makeText(MainActivity.this, "could not establish connection", Toast.LENGTH_SHORT).show();
                        else {
                            Toast.makeText(MainActivity.this, "wohoo", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


                // if authorized, create the session
                Session session = ssh.openSession();

                // terminate the session
                session.close();

                // terminate the connection
                ssh.close();
            } catch (IOException e) {
                e.printStackTrace(System.err);
                System.out.println(e.getMessage());
                //System.exit(2);
            }
        }
    });

and of course you have to add the internet permission in your manifest

<uses-permission android:name="android.permission.INTERNET" />
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.