Search code examples
javaandroidsshubuntu-server

Is it possible to execute commands remotely (ssh), through a Java program


I am looking to ssh into an Ubuntu Server via some (java) API. Is that even possible?

Here is what I am looking for exactly:

  • I have setup OpenStack on an ubuntu machine.
  • Launched an instance with ubuntu server.
  • Installed android SDK on this Virtual Machine.
  • By ssh-ing into this Virtual Machine, I am able to create an AVD and run an emulator (headless).

What I plan to do is provide some web client to start the emulator at the click of a button (considering everything is already set up i.e the Virtual Machine is up and running with the SDK installed).

I guess I need to execute the command to start the emulator. But is it possible to do this via some (java) API? I had a look into openstack4j and JClouds but could not find much. I tried to figure out it is using some ssh client library. Is that right?

Anything I am missing here?


Solution

  • Have a look at this CloudServersPublish.java example, in particular the configureAndStartWebserver() method.

    Also note how the SshjSshClientModule is added to the ComputeServiceContext in the constructor.

    You'll also need to make sure you have the jclouds-sshj dependency.

    <dependency>
      <groupId>org.apache.jclouds.driver</groupId>
      <artifactId>jclouds-sshj</artifactId>
      <version>1.9.0</version>
    </dependency>