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