Skip to content

Instantly share code, notes, and snippets.

@kwart
Created April 26, 2016 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwart/a230f535825d2cbde70815d6bfe7f9dc to your computer and use it in GitHub Desktop.
Save kwart/a230f535825d2cbde70815d6bfe7f9dc to your computer and use it in GitHub Desktop.
// http://unix.stackexchange.com/questions/30903/how-to-escape-quotes-in-shell
// http://unix.stackexchange.com/questions/4770/quoting-in-ssh-host-foo-and-ssh-host-sudo-su-user-c-foo-type-constructs
// see apache commons-lang3 StringEscapeUtils
// http://stackoverflow.com/questions/82256/how-do-i-use-sudo-to-redirect-output-to-a-location-i-dont-have-permission-to-wr?rq=1
// http://stackoverflow.com/questions/3034186/in-java-is-there-a-way-to-write-a-string-literal-without-having-to-escape-quote
public static void main(String[] args) throws IOException {
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
run(ssh, "nohup sh -c 'sh '\\''-c'\\'' '\\''sleep 30; echo ahoj >/tmp/ahoj'\\'' &' 1>'/dev/null' 2>&1 </dev/null");
// run(ssh, "nohup sh -c 'sleep 10 &' </dev/null &>/dev/null");
// run(ssh, "nohup sh -c 'sh -c '\\''sleep 30'\\'' </dev/null &>/dev/null &' </dev/null &>/dev/null");
// run(ssh, "sh -c 'echo -n ah\\\"'x\"\\'\"y'oj'");
// run(ssh, "sh -c 'echo -n '\\''ah'\\'''");
// run(ssh, "sh -c 'echo -n '\\''ah\"'\\''x\"'\\''\"y'\\''oj'\\'''");
} finally {
ssh.disconnect();
}
}
private static void run(final SSHClient ssh, String command) throws ConnectionException, TransportException, IOException {
final Session session = ssh.startSession();
// session.allocateDefaultPTY();
try {
final Command cmd = session.exec(command);
// System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join();
System.out.println("** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment