开发者

Android SSH Example Code

I want to create an andr开发者_如何学Gooid 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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜