2012-12-06 94 views
3

我想知道如何使用j2ssh執行多個命令。我從網上得到的代碼如下:使用j2ssh執行多個命令

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 

import com.sshtools.j2ssh.io.IOStreamConnector; 
import com.sshtools.j2ssh.io.IOStreamConnectorState; 
import com.sshtools.j2ssh.connection.*; 

import com.sshtools.j2ssh.SshClient; 

import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient; 
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState; 
import com.sshtools.j2ssh.session.SessionChannelClient; 

import com.sshtools.j2ssh.configuration.SshConnectionProperties; 

import com.sshtools.j2ssh.transport.ConsoleHostKeyVerification; 

public class MySSHClient { 

    SshClient ssh = null; 
    SshConnectionProperties properties = null; 
    SessionChannelClient session = null; 

    public MySSHClient(String hostName, String userName, String passwd) 
    { 

    try 
    { 
     // Make a client connection 
     ssh = new SshClient(); 
     properties = new SshConnectionProperties(); 
     properties.setHost(hostName); 

     // Connect to the host 
     ssh.connect(properties, new ConsoleHostKeyVerification()); 

     // Create a password authentication instance 
     PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); 

     pwd.setUsername(userName); 
     pwd.setPassword(passwd); 

     // Try the authentication 
     int result = ssh.authenticate(pwd); 

     // Evaluate the result 
     if (result==AuthenticationProtocolState.COMPLETE) { 

     System.out.println("Connection Authenticated"); 
     } 
    } 
    catch(Exception e) 
    { 
     System.out.println("Exception : " + e.getMessage()); 
    } 

    }//end of method. 


    public String execCmd(String cmd) 
    { 
    String theOutput = ""; 
    try 
    { 
     // The connection is authenticated we can now do some real work! 
     session = ssh.openSessionChannel(); 

     if (session.executeCommand(cmd)) 
     { 
     IOStreamConnector output = new IOStreamConnector(); 
     java.io.ByteArrayOutputStream bos = new 
     java.io.ByteArrayOutputStream(); 
     output.connect(session.getInputStream(), bos); 
     session.getState().waitForState(ChannelState.CHANNEL_CLOSED); 
     theOutput = bos.toString(); 
     } 
     //else 
     //throw Exception("Failed to execute command : " + cmd); 
     //System.out.println("Failed to execute command : " + cmd); 
    } 
    catch(Exception e) 
    { 
     System.out.println("Exception : " + e.getMessage()); 
    } 

    return theOutput; 
    } 


} 

我試圖去到一個目錄,然後列出使用ls命令的文件,但它沒有工作

MySSHClient sshc = new MySSHClient(<hostName>, <userName>, <passwd>); 
System.out.println(sshc.execCmd("cd test")); 
System.out.println(sshc.execCmd("ls")); 

任何幫助嗎?

+0

你知道,如果它不執行命令,或不輸出結果(在執行然後打開輸出流)。嘗試使用「touch」命令或類似的命令在遠程主機上創建一個文件,並查看它是否有效 –

+0

@Brian Agnew 它執行命令並輸出結果,但問題是它丟失了目錄,因此顯示以前的目錄本身的內容 – Raoul

+0

accoruding to this link http://javaconfessions.com/2008/09/getting-started-with-j2ssh.html在「執行多個命令的一個字」部分它說 你不能舉例執行命令來更改目錄,然後執行另一個命令來執行該目錄中的腳本,因爲當會話關閉並且新命令在默認工作目錄中重新開始時,更改目錄會丟失。當然,你總是可以把cd命令放入腳本中?或者使用shell來執行這兩個命令。 我不明白的是如何使用腳本 – Raoul

回答

0

嘗試用分號分隔命令。例如:System.out.println(sshc.execCmd("cd test; ls"));

0

添加運算符(&&)命令之間:

System.out.println(sshc.execCmd("cd test && ls")); 

&&;更好,因爲如果cd失敗,不執行ls

0

代替使用executeCommand,嘗試在輸出流中編寫您的命令 - session.getOutputStream()並在'\n'的基礎上添加。 然後讀取輸入流。

例如:

session.getOutputStream().write((command + "\n").getBytes());