2011-03-09 113 views
0

我正在寻找一个简单的例子,如何在Jsch中使用Expect4j(使用Shell不可执行) 我的意思是如何发送命令(〜8)到服务器,以及如何打印输出响应。Jsch shell/Expecdt4j简单例子?

到目前为止我有这:

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); 

我想发送命令是这样的:命令=( “lrp_list;使用newgrp XXX;日期”);发送(命令); 也有一些例子,我发现只有时间限制的工作;而且我需要像上面的代码那样,即使超过15分钟也会超出命令。

回答

0

您将Shell的InputStream/outputStream连接到本地java进程的相同流的代码。对于壳执行命令,你必须在外壳的InputStream中提交这些命令(即不将其连接到本地输入),像这样:

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"); 

然后等到的“日期”的结果来,并关闭频道。 (您可能首先要发送“退出”或“注销”)。

我不知道期望4j,但我假设您可以为它提供一对InputStream和OutputStream - 然后在此处使用getInputStream而不是setOutputStream


好了,在找到一个source for Expect4j.java,我会做这样的:

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 
+0

您还必须调用shellStream.flush()。 – jmpyle771 2013-08-15 14:21:47

1

设置一个SCPInfo对象来保存用户名,密码,端口:22和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); 
} 
+0

干得好,为我工作。 – 2013-01-08 20:50:12