2016-09-08 35 views
0

我想用java連接到具有兩步驗證登錄的Linux服務器。我使用的jsch庫,這是到目前爲止,我已經得到了代碼:使用JSCH庫的兩步驗證

session = jsch.getSession(username, ip); 
Properties config = new java.util.Properties(); 
config.put("StrictHostKeyChecking", "no"); 
session.setConfig(config); 

session.setPassword(password); 
session.connect(); 
System.out.println("Connected to " + ip); 

顯然,當我運行此腳本,因爲我還沒有進入認證密鑰我得到一個「驗證失敗」的錯誤。那麼,如何使用驗證密鑰登錄?如果這是不可能的,有人可能會建議一個具有此功能的庫。

這是使用putty登錄到服務器。所以你輸入用戶名,然後輸入基於時間的生成代碼,然後輸入密碼。 enter image description here

+0

你能夠使用命令行ssh工具登錄到該主機?請運行ssh登錄到此服務器,幷包含'-vvv'選項以使ssh輸出調試信息。然後編輯你的問題,包括你運行的ssh命令和它產生的輸出。這將有助於人們知道如何進行額外的驗證。 – Kenster

+0

這是不可能的,因爲額外的驗證是基於時間的,所以將一個祕密字符串傳遞給一個java方法,然後返回驗證第二部分的密鑰。 –

+0

我建議的要點是瞭解如何通過ssh協議完成驗證步驟。 – Kenster

回答

0

我最終這樣做了,你必須實現UserInfo和UIKeyboardInteractive。使用promptKeyboardInteractive方法,使其返回認證密鑰,在下面的代碼對我的作品顯示:

import java.security.InvalidKeyException; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 
import com.jcraft.jsch.UIKeyboardInteractive; 
import com.jcraft.jsch.UserInfo; 

public class UserAuthKI{ 
    public static void main(String[] arg){ 

try{ 
    JSch jsch=new JSch(); 

    String host=""; 
    String user=""; 

    Session session=jsch.getSession(user, host, 22); 

    // username and passphrase will be given via UserInfo interface. 
    UserInfo ui=new MyUserInfo(); 
    session.setUserInfo(ui); 
    session.connect(); 

    Channel channel =session.openChannel("exec"); 
    ((ChannelExec)channel).setCommand("echo 'hello'"); 

    channel.setInputStream(System.in); 
    channel.setOutputStream(System.out); 

    channel.connect(); 

} 
catch(Exception e){ 
    System.out.println(e); 
} 
    } 

    public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 
public String getPassword(){ return "passwordHere"; } 
public boolean promptYesNo(String str){ 
    return true; 
} 

public String getPassphrase(){return null;} 
public boolean promptPassphrase(String message){ return false; } 
public boolean promptPassword(String message){ 
    return true; 
} 
public void showMessage(String message){ 
    System.out.println(message); 
} 

public String[] promptKeyboardInteractive(String destination, 
              String name, 
              String instruction, 
              String[] prompt, 
              boolean[] echo){ 

    System.out.println("destination: "+destination); 
    System.out.println("name: "+name); 
    System.out.println("instruction: "+instruction); 
    System.out.println("prompt.length: "+prompt.length); 

    String[] str = new String[1]; 

    if(prompt[0].contains("Password:")){ 
     str[0] = getPassword(); 
    } 
    else if(prompt[0].contains("Verification code: ")){ 
     try { 
      str[0] = PasswordUtils.verify_code("CODEHERE"); 
     } catch (InvalidKeyException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    else{ 
     str = null; 
    } 

    return str; 

    } 
    } 
} 

(PasswordUtils.verif_code()是生成密鑰的靜態方法)