c - 如何在C代码中使用SFTP访问特定目录

标签 c sftp

我从未使用过 SFTP。请帮我用c语言使用SFTP访问目录。

编辑:

while ((dirp = readdir (directory)) != NULL) { 
  if ( strstr(dirp->d_name , ".txt" )) { 
    printf( "found a .txtfile: %s\n", dirp->d_name ); 
  } 
}

最佳答案

要使用SFTP访问目录,可以使用ssh库函数。

这是一些示例代码,

 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) {

            if (longentry[0] != '\0') {
                printf("%s\n", longentry);
            } else {
                if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
                  printf("--fix----- ");
                }
                else {
                    printf("---------- ");
                }

                if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
                    printf("%4ld %4ld ", attrs.uid, attrs.gid);
                }
                else {
                    printf("   -    - ");
                }

                if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
                    printf("%8" PRIu64 " ", attrs.filesize);
                }

                printf("%s\n", mem);
            }
        }
        else
            break;

    } while (1);

其他api信息,您可以查阅SFTP API文档并尝试一下。 http://www.libssh2.org//libssh2_sftp_readdir_ex.html

关于c - 如何在C代码中使用SFTP访问特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32599558/

相关文章:

c# - Renci ssh.net 无法连接,因为目标机器主动拒绝

python - 在 docker 容器中运行时 Python 中的 SFTP key 错误

c - 将字符添加到现有的字符/字符指针数组?

c - return 从指针目标类型中丢弃 ‘const’ 限定符

java - 如何使用spring从sftp服务器获取zip文件

azure - 如何使用公共(public) SSH key 在 Azure 数据工厂中创建 SFTP 连接

ubuntu - SFTP 文件上传非常慢并且在 Ubuntu 上停滞不前

c - iptables netfilter 队列 nfq_set_verdict_mark (nfq_set_verdict2) 似乎没有应用标记

C - wcstok() 错误结果

返回 malloc 的函数内出现 C 错误?