2011-03-09 143 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