使用JSCH库进行两步验证 [英] Two-step verification using JSCH library

查看:87
本文介绍了使用JSCH库进行两步验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java连接到具有两步验证登录名的Linux服务器.我正在使用jsch库,这是到目前为止的代码:

I am trying to use java to connect to a Linux server which has a two-step verification login. I'm using the jsch library, this is the code I've got so far:

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);

很明显,当我运行此脚本时,由于未输入身份验证密钥,因此出现身份验证失败"错误.因此,如何使用验证码登录.如果无法做到这一点,那么有人可以建议一个具有此功能的库.

Obviously when I run this script I get an "Auth failed" error as I haven't enter the authentication key. So how do I login using the verification key. If this isn't possible could someone suggest a library that has this functionality.

这是使用腻子登录服务器的.因此,您输入用户名,然后输入基于时间的生成代码,然后输入密码.

This is logging into the server using putty. So you enter the username, then the time based generated code, then the password.

推荐答案

我最终解决了这个问题,您必须实现UserInfo和UIKeyboardInteractive.使用hintKeyboardInteractive方法,使其返回身份验证密钥,如以下适用于我的代码所示:

I worked this out eventually, you have to implement UserInfo and UIKeyboardInteractive. Use the promptKeyboardInteractive method, to make it return the authentication key, shown in the following code which works for me:

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()是生成密钥的静态方法)

(PasswordUtils.verif_code() is the static method which generates the key)

这篇关于使用JSCH库进行两步验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆