import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import net.sf.expectit.Expect;
import net.sf.expectit.ExpectBuilder;
import net.sf.expectit.echo.EchoOutput;
import java.io.IOException;
import java.util.Properties;
import static net.sf.expectit.filter.Filters.removeColors;
import static net.sf.expectit.filter.Filters.removeNonPrintable;
import static net.sf.expectit.matcher.Matchers.contains;
/**
* An example of interacting with SSH server
*/
public class SshExample {
public static String host = "10.149.127.141";
public static String user = "support";
public static String password = "somepassword";
public static void main(String[] args) throws JSchException, IOException {
JSch jSch = new JSch();
Session session = jSch.getSession(user, host, 22);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("shell");
Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoInput(System.out)
.withEchoOutput(new EchoOutput() {
@Override
public void onReceive(int input, String string) throws IOException {
System.out.println("RECEIVE:" + string);
}
@Override
public void onSend(String string) throws IOException {
System.out.println("SEND:" + string);
}
})
.withInputFilters(removeColors(), removeNonPrintable())
.withErrorOnTimeout(true)
.build();
try {
channel.connect();
expect.expect(contains("$"));
System.out.println("PWD:" + expect.sendLine("pwd").expect(contains("\n")).getBefore());
} finally {
channel.disconnect();
session.disconnect();
expect.close();
}
}
}
And the output:
RECEIVE:Last login: Fri Aug 1 16:04:05 2014 from 10.112.142.198
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
The Regents of the University of California. All rights reserved.
FreeBSD 6.1-RELEASE (GENERIC) #0: Fri Mar 21 13:32:37 UTC 2008
Welcome to FreeBSD!
Before seeking technical support, please use the following resources:
o Security advisories and updated errata information for all releases are
for your release first as it's updated frequently.
along with the mailing lists, can be searched by going to
been installed, they're also available formatted in /usr/share/doc.
If you still have a question or problem, please take the output of
`uname -a', along with any relevant error messages, and email it
as a question to the ques...@FreeBSD.org mailing list
RECEIVE:. If you are
unfamiliar with FreeBSD's directory layout, please refer to the hier(7)
manual page. If you are not familiar with manual pages, type `man man'.
You may also use sysinstall(8) to re-enter the installation and
configuration utility. Edit /etc/motd to change this login announcement.
$
SEND:pwd
RECEIVE:pwd
PWD: pwd
Disconnected from the target VM, address: '
127.0.0.1:52490', transport: 'socket'
Process finished with exit code 0