sftp 연결 문제
1799 단어 ssh
개발 과정에서 문제가 발생하지 않았습니다. 연결 단계:
SshClient ssh = null;
ConfigurationLoader.initialize(false);
// Make a client connection
ssh = new SshClient();
// Connect to the host
ssh.connect(host,port);
KBIAuthenticationClient kbi = new KBIAuthenticationClient();
kbi.setUsername(username);
kbi.setKBIRequestHandler(new KBIRequestHandler(){
public void showPrompts(String arg0, String arg1,KBIPrompt[] prompts)
{
if(prompts != null)
{
prompts[0].setResponse(password);
}
}
});
// Try the authentication
int iresult = ssh.authenticate(kbi);
// Evaluate the result
if (iresult == AuthenticationProtocolState.COMPLETE) {
// The connection is authenticated we can now do some real work!
sftp = ssh.openSftpClient();
attrs = sftp.stat(dir);
}
else
{
ssh = null;
}
if(sftp != null && !sftp.isClosed())
{
try {
sftp.quit();
} catch (IOException e) {
Tracer.error("sFTP connect quit failed.Exception:" + e.toString());
}
}
if(ssh != null && ssh.isConnected())
{
ssh.disconnect();
} 나중에 테스트 기기에서 실행하면 연결할 때마다 서버에 물어봅니다. Do you want to allowthis host key?[Yes|No|Always]:
코드를 보면 연결할 때 HostKeyVerification을 추가하면 연결을 기본적으로 선택하고 사용자에게 문의할 필요가 없습니다.
ssh.connect(host,port,new HostKeyVerification(){
/**
* Host key,
*/
public boolean verifyHost(String arg0, SshPublicKey arg1) throws TransportProtocolException {
return true;
}
});