4

I was wondering whether there was a simple way to do this as part of a java program.

I would like to be able to ssh into another machine and perform commands on that machine.

A simple example would be: runtime.exec("say hello world"); would (on a mac) have a text-to-speech engine speak hello world.

Is there a way to have java run this method on another machine?

Also, assuming the above is possible, is there a way to ssh into more than one machine at the same time?

Thanks

Saad Attieh
  • 1,396
  • 3
  • 21
  • 42
  • 1
    I think [google](https://www.google.co.uk/search?q=java+ssh&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a&channel=fflbh) is working today. – Boris the Spider Mar 14 '13 at 17:18

4 Answers4

5

There are a lot of libraries to do this. I suggest Ganymed SSH-2, that is also mentioned in the official OpenSSH website. In the same site, you can also find other libraries that you can use for Java.

This is an example of ls -r command executed via SSH, using Ganymed SSH-2:

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

[...]

public static ArrayList<String> lsViaSSH(String hostname, String username, String password, String dir) {
    ArrayList<String> ls = new ArrayList<String>();

    try
    {
        Connection conn = new Connection(hostname);

        conn.connect();

        boolean isAuthenticated = conn.authenticateWithPassword(username, password);

        if (isAuthenticated == false) {
            return null;
        }

        Session sess = conn.openSession();

        sess.execCommand("ls -r " + dir);

        InputStream stdout = new StreamGobbler(sess.getStdout());

        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

        while (true)
        {
            String line = br.readLine();
            if (line == null)
                break;
            ls.add(line);
        }

        sess.close();
        conn.close();
    }
    catch (IOException e)
    {
        return null;
    }

    if(StringUtils.isEmpty(ls.get(0)))
        return null;

    return ls;
}

This is not the only function needed in order to execute a command via SSH, but I hope it could be a good starting point for you.

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
2

Have a look at JSch

As far as executing at the exact same time? Not really unless you intend to spawn multiple threads to deal with your list of remote machines.

rbedger
  • 1,177
  • 9
  • 20
1

given that you're on a system where ssh command is available and ssh keys are set up to avoid entering passwords the easiest would be just

runtime.exec("ssh remote_comp 'say hello world'");
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • Why do you need the single quotes? – Steve Nov 04 '15 at 20:14
  • because it's easier than double quotes that you'd have to escape (\") – Oleg Mikheev Nov 06 '15 at 06:04
  • Why do we need quotes at all? `man ssh` shows `ssh...[user@]hostname [command]` so I'd assume we could just write the command as we normally would. How can I determine when I need quotes in a scenario like this? You've been a big help :) thank you. – Steve Nov 20 '15 at 20:25
  • 1
    @Steve command has to be in quotes to make shell pass it to ssh as a single entity. To prevent shell from expanding parts of command on its own. `ssh somehost echo 1; ls` will run echo on remote and ls on local, `ssh somehost 'echo 1; ls'` will run both echo and ls on remote – Oleg Mikheev Nov 21 '15 at 08:02
0

I'd suggest you to take a look to the Apache MINA SSHD, it can be used to write both a client and a server with java

BackSlash
  • 21,927
  • 22
  • 96
  • 136