2014-10-30 31 views
0

我使用libssh完成兩個系統之間的連接,ssh服務器是redhat,客戶端是CentOS。 我的代碼編譯和鏈接就好了,但在運行時我總是看到這個,未定義的符號錯誤使用libssh



    [1] libssh 0.5.5 (c) 2003-2010 Aris Adamantiadis ([email protected]) Distributed under the LGPL, please refer to COPYING file for information about your rights, using threading threads_noop 
    [2] Nonblocking connection socket: 4 
    [2] Socket connecting, now waiting for the callbacks to work 
    [1] Socket connection callback: 1 (0) 
    [1] SSH server banner: SSH-2.0-OpenSSH_5.3 
    [1] Analyzing banner: SSH-2.0-OpenSSH_5.3 
    [1] We are talking to an OpenSSH client version: 5.3 (50300) 
    [2] Received SSH_KEXDH_REPLY 
    [2] SSH_MSG_NEWKEYS sent 
    [2] Received SSH_MSG_NEWKEYS 
    ./sshclient: symbol lookup error: ./sshclient: undefined symbol: ssh_pki_import_privkey_file 

的代碼看起來是這樣的,



    int main() 
    { 
      ssh_session my_ssh_session; 
      ssh_key *pubkey,*privkey; 
      int verbosity = SSH_LOG_PROTOCOL; 
      int port = 5022,rc; 
      char username[20]; 

      strcpy(username,"yoda"); //assigned a user name 
      my_ssh_session = ssh_new(); 
      if (my_ssh_session == NULL) 
        exit(-1); 
      ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "XX.XX.XX.XX"); 
      ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); 
      ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port); 
      ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, username); 

      rc = ssh_connect(my_ssh_session); 
      if(rc != SSH_OK){ 
        fprintf(stderr, "Error connecting to host: %s\n",ssh_get_error(my_ssh_session)); 
        exit(-1); 
      } 
      rc = ssh_pki_import_privkey_file("/home/yoda/keys/id_rsa",NULL,NULL,NULL,privkey); 
      if(rc != SSH_OK){ 
        fprintf(stderr, "Custom Message: Private Key unacceptable.\n"); 
        exit(-1); 
      } 
      fprintf(stdout, "Custom Message: Here now.\n"); 
      rc = ssh_userauth_publickey(my_ssh_session, NULL, *privkey); 
      if(rc == SSH_AUTH_ERROR){ 
        fprintf(stderr, "Custom Message: A serious error has occured during authentication.\n"); 
        exit(-1); 
      } 
      if(rc == SSH_AUTH_DENIED){ 
        fprintf(stderr, "Custom Message: Unacceptable key or method.\n"); 
        exit(-1); 
      } 
      if(rc == SSH_AUTH_PARTIAL){ 
        fprintf(stderr, "Custom Message: Partially authenticated user.\n"); 
        exit(-1); 
      } 
      ssh_key_free(*pubkey); 
      ssh_key_free(*privkey); 
      ssh_disconnect(my_ssh_session); 
      ssh_free(my_ssh_session); 
      return(0); 
    } 

我知道有一堆的問題與此代碼,但在這一點上,我正在第一次嘗試這個圖書館,並且更加關心它如何運作。任何深入瞭解正在發生的事情將不勝感激。

謝謝,拉胡爾

+0

這看起來更像是一個編譯問題 - 您可以發佈用於編譯代碼的命令嗎? – 2014-10-30 14:18:58

+0

gcc -o sshclient sshRS.c -L/home/rs/libssh/lib -lssh -I/home/rs/libssh/include – RNS 2014-11-01 11:59:02

回答

0

libssh 0.5已停用維護。你應該使用libssh 0.6。如果我沒有記錯ssh_pki_import_privkey_file()僅適用於libssh 0.6 ...