Jsch shell/Expecdt4j simple example?
I'm looking for a simple example how to use Expect4j in Jsch(using Shell no开发者_如何学Ct exec) I mean how to send commands(~8) to the server, and how to printout the response.
so far I have this:
JSch jsch=new JSch();
String host="www.superserver.uk.com";
String user="tom1234";
String passwd="12345a";
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
session.connect();
Channel channel=session.openChannel("shell");//only shell
channel.setInputStream(System.in);// enter lrp_list
channel.setOutputStream(System.out);
I want to send commands like this: command=("lrp_list;newgrp xxx;date"); send(command); also some examples i have found only work with time restrictions; and i need something like in above code that would excecute a command even if excecution takes 15 min.
Setup an SCPInfo object to hold the username, password, port:22 and ip.
List<String> commands = new ArrayList<String>();
commands.add("touch test1.txt");
commands.add("touch test2.txt");
commands.add("touch test3.txt");
runCommands(scpInfo, commands);
public static void runCommands(SCPInfo scpInfo, List<String> commands){
try {
JSch jsch = new JSch();
Session session = jsch.getSession(scpInfo.getUsername(), scpInfo.getIP(), scpInfo.getPort());
session.setPassword(scpInfo.getPassword());
setUpHostKey(session);
session.connect();
Channel channel=session.openChannel("shell");//only shell
channel.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(channel.getOutputStream()); // printStream for convenience
channel.connect();
for(String command: commands) {
shellStream.println(command);
shellStream.flush();
}
Thread.sleep(5000);
channel.disconnect();
session.disconnect();
} catch (Exception e) {
System.err.println("ERROR: Connecting via shell to "+scpInfo.getIP());
e.printStackTrace();
}
}
private static void setUpHostKey(Session session) {
// Note: There are two options to connect
// 1: Set StrictHostKeyChecking to no
// Create a Properties Object
// Set StrictHostKeyChecking to no
// session.setConfig(config);
// 2: Use the KnownHosts File
// Manually ssh into the appropriate machines via unix
// Go into the .ssh\known_hosts file and grab the entries for the hosts
// Add the entries to a known_hosts file
// jsch.setKnownHosts(khfile);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
}
The code you have connects the Inputstream/outputStream of the Shell to the same streams of the local java process. For executing commands in the shell, you have to submit these commands in the inputstream of the shell (i.e. not connect it to the local input), like this:
JSch jsch=new JSch();
String host="www.superserver.uk.com";
String user="tom1234";
String passwd="12345a";
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
session.connect();
Channel channel=session.openChannel("shell");//only shell
channel.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(channel.getOutputStream()); // printStream for convenience
channel.connect();
shellStream.println("lrp_list");
shellStream.println("newgrp xxx");
shellStream.println("date");
Then wait until the result of "date" comes, and close the channel. (You may want to send an "exit" or "logout" first.)
I don't know expect4j, but I assume you can feed it a pair of InputStream and OutputStream - then use getInputStream
instead of setOutputStream
here.
Okay, having found the source for Expect4j.java, I would do it like this:
JSch jsch=new JSch();
String host="www.superserver.uk.com";
String user="tom1234";
String passwd="12345a";
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
session.connect();
Channel channel=session.openChannel("shell");//only shell
Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
// use expect methods
精彩评论