0
我正在使用libssh庫創建與在遠程服務器上創建的ssh服務器的連接。一切順利,直到代碼從服務器獲取公鑰並詢問用戶是否要保存它。那裏的代碼崩潰的原因不明。我正在使用這個link給出的教程。VS2008中的Libssh服務器身份驗證不順利
這是我迄今爲止編寫的代碼。任何關於這方面的幫助都會很棒。
#include <libssh/libssh.h>
#include <iostream>
using namespace std;
int verifyServer(ssh_session);
int main(){
ssh_session my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
int connect;
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "hostname");
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username");
connect = ssh_connect(my_ssh_session);
if(connect != SSH_OK){
cout<<"Connection error!"<<endl;
ssh_free(my_ssh_session);
exit(-1);
}
int result = verifyServer(my_ssh_session);
if(result < 0){
ssh_disconnect(my_ssh_session);
exit(-1);
}
connect = ssh_userauth_password(my_ssh_session, NULL, "password");
if(connect != SSH_AUTH_SUCCESS){
cout<<"Password authentication failed!"<<endl;
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
ssh_free(my_ssh_session);
}
int verifyServer(ssh_session session){
int state, hlen;
unsigned char* hash = NULL;
char* hexa;
char buffer[10];
state = ssh_is_server_known(session);
hlen = ssh_get_pubkey_hash(session, &hash);
if(hlen < 0){
cout<<"Server public key hash error!"<<endl;
return -1;
}
switch(state){
case SSH_SERVER_KNOWN_OK:
break;
case SSH_SERVER_KNOWN_CHANGED:
cout<<"Host key for the server has been changed to: \n";
ssh_print_hexa("Public Key Hash", hash, hlen);
cout<<"For security reasons, connection will now be stopped.\n";
free(hash);
return -1;
case SSH_SERVER_FOUND_OTHER:
cout<< "The host key for this server was not found but an other"
"type of key exists.\n";
cout<<"An attacker might change the default server key to"
"confuse your client into thinking the key does not exist\n";
free(hash);
return -1;
case SSH_SERVER_FILE_NOT_FOUND:
cout<<"Could not find known host file.\n";
cout<<"If you accept the host key here, the file will be"
"automatically created.\n";
case SSH_SERVER_NOT_KNOWN:
hexa = ssh_get_hexa(hash, hlen);
cout<<"The server is unknown. Do you trust the host key?\n";
cout<<"Public key hash : "<<hexa<<endl;
free(hexa);
if (fgets(buffer, sizeof(buffer), stdin) == NULL)
{
free(hash);
return -1;
}
if (_strnicmp(buffer, "yes", 3) != 0)
{
free(hash);
return -1;
}
if (ssh_write_knownhost(session) < 0)
{
fprintf(stderr, "Error %s\n", strerror(errno));
free(hash);
return -1;
}
break;
case SSH_SERVER_ERROR:
cout<<ssh_get_error(session);
free(hash);
return -1;
}
free(hash);
return 0;
}
以下是出現錯誤的輸出窗口。
我做到了。它工作正常,沒有在Linux中的ssh_init()。窗戶必須明確地調用這個函數嗎? – pslayer89