Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SSH Example Code

Tags:

android

ssh

I want to create an android activity for setting up an SSH Session with a remote device (through Wifi) and executing some linux commands on the remote device. Anyone got a quick, short example for connecting, authenticating and sending remote commands using Trilead libraries in Android ? Connectbot source (the only place to find the source for the unmaintained library) is quite vast and time consuming to go through if one is just trying to do a quick SSH Connection/ Command execution. I had found the sshJ library earlier, which had nicely documented examples and tips but unfortunately Android lacks some Java.Util classes required for sshJ.

I am looking for something in Trilead like (this is from the sshJ example I found earlier) :

final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();

ssh.connect("localhost");
try {
    ssh.authPublickey(System.getProperty("user.name"));
    final Session session = ssh.startSession();
    try {
        final Command cmd = session.exec("ping -c 1 google.com");
        System.out.print(cmd.getOutputAsString());
        System.out.println("\n** exit status: " + cmd.getExitStatus());
    } finally {
        session.close();
    }

} finally {
    ssh.disconnect();
}
like image 818
TanB Avatar asked Nov 05 '22 03:11

TanB


1 Answers

If you are okay with restricting your app to Android 2.3+ (Gingerbread), then you can use sshj. You will have to create the SSHClient object with this Config.

like image 191
shikhar Avatar answered Nov 11 '22 10:11

shikhar