Saturday, April 11, 2015

Expect for Java


https://github.com/dacr/jassh

#!/bin/sh
exec java -jar jassh.jar "$0" "$@"
!#
jassh.SSH.once("localhost", "test", "testtest") { ssh =>
  print(sh.execute("""echo "Hello World from `hostname`" """))
}

#!/bin/sh
exec java -jar jassh.jar "$0" "$@"
!#
jassh.SSH.shell("localhost", "test", "testtest") { sh =>
  import sh._
  println(s"initial directory is ${pwd}")
  cd("/tmp")
  println(s"now it is ${pwd}")
} 


https://github.com/ronniedong/Expect-for-Java
try {
    JSch jsch = new JSch();
    Session session = jsch.getSession(USER, HOST);
    session.setPassword(PASSWD);
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect(60 * 1000);
    Channel channel = session.openChannel("shell");
    Expect expect = new Expect(channel.getInputStream(),
            channel.getOutputStream());
    channel.connect();
    expect.expect("$");
    System.out.println(expect.before + expect.match);
    expect.send("ls\n");
    expect.expect("$");
    System.out.println(expect.before + expect.match);
    expect.send("exit\n");
    expect.expectEOF();
    System.out.println(expect.before);
    expect.close();
    session.disconnect();
} catch (JSchException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} 
 
http://www.jcraft.com/jsch/