2015-07-22 389 views
3

我正在使用Jsch庫來連接我的服務器。連接後,我傳遞命令,需要密碼進一步進行,因此我只是在命令中傳遞我的密碼,但沒有任何反應。如何使用ssh命令在命令中發送密碼

代碼:

JSch jsch = new JSch(); 
      jsch.removeAllIdentity(); 
      Session session = jsch.getSession(user, host, port); 
      session.setPassword(password); 
      .setConfig("StrictHostKeyChecking", "no"); 
      session.setConfig("PubkeyAuthentication", "no"); 
      System.out.println("Establishing Connection..."); 
      session.setConfig("PreferredAuthentications", 
        "publickey,keyboard-interactive,password"); 
      session.connect(); 
      System.out.println("Connection established."); 
     System.out.println("Crating SFTP Channel.");`  
     Channel shellChannel = session.openChannel("shell"); 
     shellChannel.connect(); 
     ((ChannelShell) shellChannel).setPty(true); 
     shellChannel.setInputStream(System.in); 
     shellChannel.setOutputStream(System.out); 
     PrintStream shellStream = new PrintStream(
       shellChannel.getOutputStream()); 

     shellChannel.connect(); 
     shellStream 
       .println("cd /usr/local/apache2/; ls; cd ../www; ls; git fetch origin; <mypasssword>"); 
     shellStream.flush(); 
     System.out.println("SFTP Channel created.");` 

當我運行這段代碼的git問密碼,以進一步進行。 注意:我無法禁用git fetch原點的密碼。

回答

1

我試過你的代碼來訪問我的linux盒子 - 它確實登錄成功,但是卻無法發送任何命令。我不確定這是否是您遇到的問題 - 但我會在這裏添加我的解決方案,以防萬一。

移動shellStream.println();命令其自身的功能:

public static void sendCommand(String c) { 
    shellStream.print(c + "\n"); 
    shellStream.flush(); 
} 

不得不做出shellChannelshellStream在這個過程中的全局變量。

更改shellStream.println();shellStream.print("\n");,因爲上述拒絕工作。

這行代碼後:

shellStream = new PrintStream(shellChannel.getOutputStream()); 

加了我的命令序列:

Thread.sleep(1000); // wait for it to connect  
sendCommand("sudo su"); // the command I tried 
Thread.sleep(1000); // not sure how long you need to wait 
sendCommand("mypassword"); 
Thread.sleep(1000); 
// etc. 

順便說一句,你在你的代碼中調用shellChannel.connect();兩次 - 我刪除了最後一個。

這裏是你的代碼的最後工作版本:

import java.io.IOException; 
import java.io.PrintStream; 
import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelShell; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.JSchException; 
import com.jcraft.jsch.Session; 

public class MyShell { 

    static String user = "daniel"; 
    static String host = "localhost"; 
    static int port = 22; 
    static String password = "mypass"; 
    static Session session; 
    static Channel shellChannel; 
    static PrintStream shellStream; 

    public static void main(String[] args) throws JSchException, IOException, 
      InterruptedException { 

     JSch jsch = new JSch(); 
     jsch.removeAllIdentity(); 
     session = jsch.getSession(user, host, port); 
     session.setPassword(password); 
     session.setConfig("StrictHostKeyChecking", "no"); 
     session.setConfig("PubkeyAuthentication", "no"); 
     System.out.println("Establishing Connection..."); 
     session.setConfig("PreferredAuthentications", 
       "publickey,keyboard-interactive,password"); 
     session.connect(); 
     System.out.println("Connection established."); 
     System.out.println("Crating SFTP Channel."); 

     shellChannel = session.openChannel("shell"); 
     shellChannel.connect(); 
     ((ChannelShell) shellChannel).setPty(true); 
     shellChannel.setInputStream(System.in); 
     shellChannel.setOutputStream(System.out); 
     shellStream = new PrintStream(shellChannel.getOutputStream()); 

     Thread.sleep(1000); 
     sendCommand("sudo su"); 
     Thread.sleep(1000); 
     sendCommand("mypass"); 
     Thread.sleep(1000); 
     sendCommand("ls"); 

    } 

    public static void sendCommand(String c) { 
     shellStream.print(c + "\n"); 
     shellStream.flush(); 
    } 
} 
+0

謝謝丹尼爾!有效。 :d –

0
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); 

Keyboard-interactive這裏的意思是密碼必須用鍵盤打翻。即使有通過命令行傳遞密碼的方法,SSH也相當高峯。

最好的方法是使用一個PUBKEY權威性,但如果這不是一個選項,請嘗試使用僅password

session.setConfig("PreferredAuthentications", "password"); 

你也可以只發送使用

session.setPassword("password"); 
密碼登錄
+0

我用session.setConfig( 「PreferredAuthentications」, 「密碼」);但它仍然要求我的控制檯輸入密碼。我的控制檯輸出:[mPassword'gitlab.xxx.xxx': –

+0

編輯我的答案。 – blue112

+0

session.setPassword(「password」);這一個也不起作用。它仍在要求輸入密碼。即使我嘗試回聲'密碼',並再次失敗。 –