2011-04-06 2 views
5

ssh 도구를 사용하여 학교 서버에 ssh 연결을 만들고 있습니다. 예를 들어 ssh 연결을 만드는 소스에서 예제를 사용하고 있습니다. 내 문제는 어떤 사용자 입력 프롬프트를 원하지 않는다는 것입니다. 그러나 프롬프트가 나타나 호스트의 진위 여부를 확인할 수 없으며 사용자가 계속하려면 예를 눌러야합니다. 어떻게 프로그램에 대해 프롬프트를 받아들이도록 코딩 할 수 있습니까? 그 자체. 가장 중요한UserInfo 전달 예 클릭하여 기능으로 이동

try { 
    session = jsch.getSession(user, host, port); 
} 
catch (JSchException e) { 
    throw new TransferException("Failed to open session - " + params, e); 
} 

session.setPassword(password); 

// Create UserInfo instance in order to support SFTP connection to any machine 
// without a key username and password will be given via UserInfo interface. 
UserInfo userInfo = new SftpUserInfo(); 
session.setUserInfo(userInfo); 

try { 
    session.connect(connectTimeout); 
} 
catch (JSchException e) { 
    throw new TransferException("Failed to connect to session - " + params, e); 
} 

boolean isSessionConnected = session.isConnected(); 

과 :

Session session = jsch.getSession(user, host, 22); 
//username and host I can input directly into program so thats not a problem 
// username and password will be given via UserInfo interface. 
UserInfo ui = new MyUserInfo(); 

//this is the part that uses the UserInfo, which pulls up a prompt 
//how can I code the prompt to automatically choose yes?  
session.setUserInfo(ui); 
session.setPassword("password"); 
session.connect(); 
String command = JOptionPane.showInputDialog("Enter command", "set|grep SSH"); 

답변

3

우리는 아래의 코드를 사용했다

/** 
* Implements UserInfo instance in order to support SFTP connection to any machine without a key. 
*/ 
class SftpUserInfo implements UserInfo { 

    String password = null; 

    @Override 
    public String getPassphrase() { 
     return null; 
    } 

    @Override 
    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String passwd) { 
     password = passwd; 
    } 

    @Override 
    public boolean promptPassphrase(String message) { 
     return false; 
    } 

    @Override 
    public boolean promptPassword(String message) { 
     return false; 
    } 

    @Override 
    public boolean promptYesNo(String message) { 
     return true; 
    } 

    @Override 
    public void showMessage(String message) { 
    } 
} 
+0

이 도움을 주셔서 감사합니다. – user541597

+0

@ user541597 당신은 가장 환영합니다 – Yaneeve

+0

@ Yaneeve이 ​​프로그램은 사용자 프롬프트를 제거하지 않습니다. 코드를 실행할 때 해당 사용자가 아직 입력을 클릭해야하는 것을 볼 수 있습니다. 원격 서버 연결을 위해이 코드를 작성했는지 확인하십시오. 그러나 인증 프로세스를 자동화하는 데는 도움이되지 않습니다. – MKod

관련 문제