有没有办法通过 SSH/SFTP 进行 opendir()、readdir() 类型的调用?

发布于 2024-07-30 18:19:53 字数 284 浏览 9 评论 0原文

我有一些代码可以读取要处理的文件的目录。 我目前使用 dirent.h、opendir()、readdir() 方法来执行此操作。 我被告知我需要做同样的事情,只是现在它是具有 SSH/SFTP 访问权限的远程计算机上的目录。

(我能想到的)做同样的事情的唯一方法,除了我的源代码之外,不改变系统中的任何内容,是运行“ssh user@host ls”命令,解析并根据解析器结果进行处理。

这里真正的问题是,有没有办法通过 SSH/SFTP 执行 opendir()、readdir() (或类似的操作)?

I have a bit of code that reads a directory for files to process. I currently use the dirent.h,opendir(),readdir() methods of doing this. I have been told that I need to do the same thing, only now it is a directory on a remote machine with SSH/SFTP access.

The only way (that I can think of) to do the same thing, without changing anything in the system except my source code, is to run a "ssh user@host ls" command, parse, and process based on the parsers results.

The real question here is, are there ways of doing opendir(), readdir() (or something similar) through SSH/SFTP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜嗑 2024-08-06 18:19:54

如果您使用的是 Linux 或 Mac OS X(或某些 BSD),最简单的方法是使用 sshfs 将其附加到文件系统。 大多数文件系统调用将直接工作而无需修改。

否则,您可以查看 sshfs 的 来源 有关其工作方式的提示。

The easiest way, if you're on Linux or Mac OS X (or some BSDs) would be to use sshfs to attach it to the filesystem. Most filesystem calls will then just work directly without modification.

Otherwise you can look at sshfs's source for hints about how it does things.

血之狂魔 2024-08-06 18:19:54

是的,SFTP 有一个用于执行此操作的协议。 请阅读此处的最新文档:http://tools.ietf .org/wg/secsh/draft-ietf-secsh-filexfer/

Yes, SFTP has a protocol for doing this. Read through the most recent document here: http://tools.ietf.org/wg/secsh/draft-ietf-secsh-filexfer/

小…楫夜泊 2024-08-06 18:19:54

从 OpenSSH 5.1 开始,执行此操作的实现有限。 它没有完全实现文件 XFER 标准。 所以我无法检查文件类型之类的东西,但我可以扫描文件名本身...使用 OpenSSH 源代码中的“sftp.c”代码,我只是将一个临时 main() 放在一起,可以根据需要进行更改。我希望这个问题以后能得到更深入的回答。

/* Username is a NULL terminated string
   Host is a NULL terminated string indicating remote IP */
void setup_args(arglist *args, char *username, char *host)
{
        // Arg init allocation
        memset(args, '\0', sizeof(*args));
        args->list = NULL;

        // Arg setup
        addargs(args, "%s", "/usr/bin/ssh");
        addargs(args, "%s", "-oForwardX11 no");
        addargs(args, "%s", "-oForwardAgent no");
        addargs(args, "%s", "-oPermitLocalCommand no");
        addargs(args, "%s", "-oClearAllForwardings yes");
        addargs(args, "%s%s", "-l", username);
        addargs(args, "%s", "-oProtocol 2");
        addargs(args, "%s", "-s");
        addargs(args, "%s", host);
        addargs(args, "%s", "sftp");
}
void run_program()
{
        // Setup connection parameters.
        int in, out; // The IO file descriptors.
        arglist args;
        setup_args(&args, "chenz", "172.16.22.128");
        connect_to_server("/usr/bin/ssh", args.list, &in, &out);
        freeargs(&args);

        // Create connection
        struct sftp_conn *conn;
        conn = do_init(in, out, copy_buffer_len, num_requests);
        if (conn == NULL)
                fatal("Couldn't initialise connection to server");

        //Get the current directory
        char *pwd;
        pwd = do_realpath(conn, ".");
        printf("PWD: %s\n", pwd);
        if (pwd == NULL)
                fatal("Need cwd");

        SFTP_DIRENT** dirent;
        do_readdir(conn, "/home/chenz", &dirent);
        int i = 0;
        while (dirent[i] != 0) {
                printf("filename: %s\n", dirent[i]->filename);
                printf("longname: %s\n", dirent[i]->longname);
                printf("attrib:\n", dirent[i]->a);
                i++;
        }

        // Clean up file descriptors
        close(in);
        close(out);

        // Wait for kill signal and exit
        while (waitpid(sshpid, NULL, 0) == -1)
                if (errno != EINTR)
                        fatal("Couldn't wait for ssh process: %s",
                            strerror(errno));

        //exit(err == 0 ? 0 : 1);
        exit(0);
 }

As of OpenSSH 5.1, there is a limited implementation to do this. It does not fully implement the File XFER standard. So I cannot check for things like the file types, but I can scan for the filenames themselves... Using the "sftp.c" code in OpenSSH source I just threw together an adhoc main() that could be changed as needed... I hope that this question can be more thoughly answered at a later date.

/* Username is a NULL terminated string
   Host is a NULL terminated string indicating remote IP */
void setup_args(arglist *args, char *username, char *host)
{
        // Arg init allocation
        memset(args, '\0', sizeof(*args));
        args->list = NULL;

        // Arg setup
        addargs(args, "%s", "/usr/bin/ssh");
        addargs(args, "%s", "-oForwardX11 no");
        addargs(args, "%s", "-oForwardAgent no");
        addargs(args, "%s", "-oPermitLocalCommand no");
        addargs(args, "%s", "-oClearAllForwardings yes");
        addargs(args, "%s%s", "-l", username);
        addargs(args, "%s", "-oProtocol 2");
        addargs(args, "%s", "-s");
        addargs(args, "%s", host);
        addargs(args, "%s", "sftp");
}
void run_program()
{
        // Setup connection parameters.
        int in, out; // The IO file descriptors.
        arglist args;
        setup_args(&args, "chenz", "172.16.22.128");
        connect_to_server("/usr/bin/ssh", args.list, &in, &out);
        freeargs(&args);

        // Create connection
        struct sftp_conn *conn;
        conn = do_init(in, out, copy_buffer_len, num_requests);
        if (conn == NULL)
                fatal("Couldn't initialise connection to server");

        //Get the current directory
        char *pwd;
        pwd = do_realpath(conn, ".");
        printf("PWD: %s\n", pwd);
        if (pwd == NULL)
                fatal("Need cwd");

        SFTP_DIRENT** dirent;
        do_readdir(conn, "/home/chenz", &dirent);
        int i = 0;
        while (dirent[i] != 0) {
                printf("filename: %s\n", dirent[i]->filename);
                printf("longname: %s\n", dirent[i]->longname);
                printf("attrib:\n", dirent[i]->a);
                i++;
        }

        // Clean up file descriptors
        close(in);
        close(out);

        // Wait for kill signal and exit
        while (waitpid(sshpid, NULL, 0) == -1)
                if (errno != EINTR)
                        fatal("Couldn't wait for ssh process: %s",
                            strerror(errno));

        //exit(err == 0 ? 0 : 1);
        exit(0);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文