2. compiler use Visual Studio 2019
open .\libssh2\win32\libssh2.dsw
creat .lib/.dll
use coff2omf.exe conversion for bcc74.
demo.prg connect remote server.
代碼: 選擇全部
// ssh2 test
#define AF_INET 2
#define SOCK_STREAM 1
#define IPPROTO_IP 0
Func Test()
Local nSession, nSock
Local cUsername := 'username',;
cPassword := 'password',;
cIp := '127.0.0.1'
nSession := ssh2_session_init()
If nSession == 0
msgStop( 'ssh2 init Fail' )
return
EndIf
msginfo( 'ssh2 init ok', 'ok' )
do while .t.
// 建立與遠端連線的 socket
if WSASTARTUP() != 0 // 初始化
msgStop('sock init fail!!','error')
exit
endif
//
if ( nSocket := Socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) == 0 // 建立 socket
MsgStop( "Socket creation error: " + Str( WsaGetLastError() ) )
exit
endif
//
if ConnectTo( nSocket, 22, cIp ) <> 0
msgStop( 'create connect fail!!' )
exit
endif
//
if ssh2_session_handshake( nSession, nSocket ) > 0
msgstop( 'create handshake fail!!' )
else
msginfo( 'create handshate success!!' )
endif
//
do while .t.
fingerprint = ssh2_hostkey_hash(nSession ) // , LIBSSH2_HOSTKEY_HASH_SHA1)
msginfo( fingerprint, 'fingerprint' )
userauthlist = ssh2_userauth_list(nSession, cUsername ) // 檢查需要那些認證資料?
msginfo( userauthlist, 'userauthlist' )
if 'password' $ userauthlist
// 可以使用密碼方式登入
if SSH2_USERAUTH_PASSWORD( nSession, cUsername, cPassword ) > 0 // 設定密碼
msgStop( 'authentication by password fail' )
exit
else
msgInfo( '認證成功!!' )
endif
else
if 'keyboad-interactive' $ userauthlist // 使用鍵盤方式輸入
// 強制 client 每次都使用鍵盤交互輸入,client 不能儲存密碼,避免被破解
else
if 'publickey' $ userauthlist
// 使用認證檔案方式
else
msgStop( 'no support any authentication method!!' )
endif
endif
endif
//
exit
enddo
CloseSocket( nSock )
//
exit
//
enddo
//
ssh2_session_disconnect( nSession ) // 關閉連線
ssh2_session_free( nSession )
msginfo( 'ssh2 free ok', 'ok' )
//
return NIL
ssh2.c
代碼: 選擇全部
/*
Copyright by WenSheng from Taiwan!!
*/
#include "hbapi.h" // add:WenSheng:2015.10.21
#include <hbapiitm.h>
// libssh2
#include <libssh2.h>
//-------------------------------------------------------------------------
// LIBSSH2_AGENT *libssh2_agent_init(LIBSSH2_SESSION *session);
//-------------------------------------------------------------------------
// 初始化
HB_FUNC( SSH2_SESSION_INIT ){
LIBSSH2_SESSION *session;
session = libssh2_session_init();
if(session == NULL){
hb_retnl(0);
}else{
hb_retnl( (LONG) session);
}
}
// 關閉連線
// int libssh2_session_disconnect(LIBSSH2_SESSION *session, const char *description);
HB_FUNC( SSH2_SESSION_DISCONNECT ){
LIBSSH2_SESSION *session = (LIBSSH2_SESSION *)hb_parnl(1);
char * desc = (char *)(HB_ISCHAR(2) ? hb_parc( 2 ) : "Normal Shutdown" );
hb_retni( libssh2_session_disconnect(session, desc)); // 0:success
}
// 釋放
HB_FUNC( SSH2_SESSION_FREE ){
LIBSSH2_SESSION *session = (LIBSSH2_SESSION *)hb_parnl(1);
hb_retni( libssh2_session_free( session ));
}
// 建立 handshake
HB_FUNC( SSH2_SESSION_HANDSHAKE ){
LIBSSH2_SESSION *session = (LIBSSH2_SESSION *)hb_parnl(1); // session
int sock = (HB_ISNUM(2) ? hb_parnl(2) : 0 ); // socket
hb_retni( libssh2_session_handshake(session, sock));
}
// 取得遠端主機金鑰驗證方式
// const char * libssh2_hostkey_hash(LIBSSH2_SESSION *session, int hash_type);
HB_FUNC( SSH2_HOSTKEY_HASH ){
LIBSSH2_SESSION *session = (LIBSSH2_SESSION *)hb_parnl(1);
int hash_type = (HB_ISNUM(2) ? hb_parni(2) : LIBSSH2_HOSTKEY_HASH_SHA1);
const char * fingerprint;
fingerprint = libssh2_hostkey_hash(session, hash_type);
hb_retc( fingerprint );
}
// 列出支援的認證方式
// char * libssh2_userauth_list(LIBSSH2_SESSION *session, const char *username, unsigned int username_len);
HB_FUNC( SSH2_USERAUTH_LIST ){
LIBSSH2_SESSION *session = (LIBSSH2_SESSION *)hb_parnl(1);
char * username = (char *)hb_parc( 2 );
LONG iUlen = hb_parclen(2);
char * userauthlist;
userauthlist = libssh2_userauth_list(session, username, iUlen);
hb_retc( userauthlist );
}
// 設定驗證密碼
// int libssh2_userauth_password(LIBSSH2_SESSION *session, const char *username, const char *password)
HB_FUNC( SSH2_USERAUTH_PASSWORD ){
LIBSSH2_SESSION *session = (LIBSSH2_SESSION *)hb_parnl(1);
char * username = (char *)hb_parc( 2 );
char * password = (char *)hb_parc( 3 );
hb_retni( libssh2_userauth_password(session, username, password)); // 0:success
}
file is libssh2.lib and libssh2.dll for bcc74.