本文的内容安排是:首先介绍如何编译各种依赖库,然后介绍如何把这些依赖库继集成到Visual Studio 2008中,接下来介绍基于SSH2协议的程序的一般框架,最后举一个实际的开发例子。附录A是我自己开发的一个实际例子,附录B列出了全部的 libssh2库函数。
一·准备一些工具
1、安装Visual Studio 2008开发环境(最好是英文版的,我的是VS2008版本是9.0.30729.1 SP, Windows SDK 6.1)。什么?你不会装!你去google上搜一下“Visual Studio 2008安装过程详解”或者点击参考文章:
http://dev.yesky.com/msdn/329/7823829.shtml
2、OpenSSL库。OpenSSL库网上只有源代码,我们首先必须编译。从
http://www.openssl.org/source/openssl-0.9.8k.tar.gz 下载源代码包,然后解压到目录C:/openssl-0.9.8k下(最终存在目录C:/openssl-0.9.8k/apps即表示正确)。进入 Visual Studio 2008的命令提示符(开始-->所有程序-->Microsoft Visual Studio 2008-->Visual Studio Tools-->Visual Studio 2008 Command Prompt),依次输入如下命令:
1、在Visual Studio 2008开发环境中新建一个工程: File-->New-->Project...-->展开Visual C++ --> 选择Win32 Console Application,Name处输入testsftp,Location处出入C:/projects,Solution Name处输入testftp,勾选Create directory for solution,最后点击OK进入下一步再点击Next。这一页只点选Console application,其他项都不要勾选或点选。最后点击Finish按钮完成新工程的创建。
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
if((rc = libssh2_session_startup(session, sock))){
fprintf(stderr, "Failure establishing SSH session: %d/n", rc);
libssh2_session_free(session);
goto CLOSESOCKET;
}
/* At this point we havn't yet authenticated. The first thing to do
* is check the hostkey's fingerprint against our known hosts Your app
* may have it hard coded, may go to a file, may present it to the
* user, that's your call
*/
if((fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5))){
printf("Fingerprint: ");
for(i = 0; i < 16; i++) {
printf("%02X ", (unsigned char)fingerprint[i]);
}
printf("/n");
}
if (auth_pw) {
/* We could authenticate via password */
if ((libssh2_userauth_password(session, "sftpuser", "abc123"))) {
printf("Authentication by password failed./n");
goto SHUTDOWN;
}
} else {
/* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, "sftpuser","/home/username/.ssh/id_rsa.pub","/home/username/.ssh/id_rsa","abc123")) {
printf("/tAuthentication by public key failed/n");
goto SHUTDOWN;
}
}
/* Since we have not set non-blocking, tell libssh2 we are blocking */
libssh2_session_set_blocking(session, 1);
fprintf(stderr, "libssh2_sftp_opendir()!/n");
//建目录
if(libssh2_sftp_mkdir(sftp_session, "sftpdir/cba",
LIBSSH2_SFTP_S_IRWXU|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP|
LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH)==-1)
fprintf(stderr,"Create dir failed!/n");
//浏览一个目录中的文件
if(!(sftp_handle = libssh2_sftp_opendir(sftp_session, "sftpdir"))){
fprintf(stderr, "Unable to open dir with SFTP/n");
goto SHUTDOWN;
}
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!/n");
do {
char mem[512];
char longentry[512];
LIBSSH2_SFTP_ATTRIBUTES attrs;
/* loop until we fail */
rc = libssh2_sftp_readdir_ex(sftp_handle, mem, sizeof(mem), longentry, sizeof(longentry), &attrs);
if(rc > 0) {
/* rc is the length of the file name in the mem
buffer */
if (longentry[0] != '/0') {
printf("%s/n", longentry);
} else {
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
/* this should check what permissions it
is and print the output accordingly */
printf("--fix----- ");
}
else {
printf("---------- ");
}
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
/* attrs.filesize is an uint64_t according to
the docs but there is no really good and
portable 64bit type for C before C99, and
correspondingly there was no good printf()
option for it... */