Skip to content

Instantly share code, notes, and snippets.

@grational
Last active October 7, 2015 13:31
Show Gist options
  • Save grational/dc07a665ba18a14d0609 to your computer and use it in GitHub Desktop.
Save grational/dc07a665ba18a14d0609 to your computer and use it in GitHub Desktop.
Execute remotely a long running command in java/groovy
#!/bin/bash
//bin/true && SCRIPT_DIR="$(dirname $(readlink -f "${0}"))"
//bin/true && exec groovy -cp "${SCRIPT_DIR}/lib" "${0}" "${@}"; exit $?
@Grab(group='ch.ethz.ganymed', module='ganymed-ssh2', version='262')
import ch.ethz.ssh2.*
String hostname = "localhost";
String username = "gsus";
File keyfile = new File("/home/gsus/.ssh/id_rsa");
String keyfilePass = "";
try {
Connection conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated=conn.authenticateWithPublicKey(username,keyfile,keyfilePass);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
Session sess=conn.openSession();
sess.execCommand("nohup sudo ping -c 100 www.yahoo.com 2>&1 >nohup.out </dev/null &");
sess.close();
conn.close();
}
catch ( IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment