2015-07-22 127 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”);这一个也不起作用。它仍在要求输入密码。即使我尝试回声'密码',并再次失败。 –