通过 SFTP 传输的文件

发布于 2024-12-21 02:39:54 字数 128 浏览 4 评论 0原文

我有一个 Linux (openSUSE 10.X) 机器,上面有 SFTP 服务。

当有人放置文件时,我必须编写一个脚本将文件移动到另一个目录。我不想写一个 cron 作业。是否有事件或其他内容我可以检查他们是否已发送文件?

I have a Linux (openSUSE 10.X) box and have a SFTP service on it.

When someone puts a file I have to write a script to move the files to another dir. I do not want to write a cron job. Is there an event or something I can check to see if they have sent the file?

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

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

发布评论

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

评论(2

百变从容 2024-12-28 02:39:54

您可以编写 ac 应用程序并挂钩 inotify 事件。

You can write a c application and hook into inotify events.

生活了然无味 2024-12-28 02:39:54

另请检查 SFTP 服务器 Net::SFTP::Server用 Perl 编写,可以扩展来完成您需要的事情。

一些代码:

#!/usr/bin/perl

use strict;
use warnings;
use File::Basename ();

my $server = Server->new(timeout => 15);
$server->run;
exit(0);

package Server;

use Net::SFTP::Server::Constants qw(SSH_FXF_WRITE);

use parent 'Net::SFTP::Server::FS';

sub handle_command_open_v3 {
    my ($self, $id, $path, $flags, $attrs) = @_;
    my $writable = $flags & SSH_FXF_WRITE;
    my $pflags = $self->sftp_open_flags_to_sysopen($flags);
    my $perms = $attrs->{mode};
    my $old_umask;
    if (defined $perms) {
    $old_umask = umask $perms;
    }
    else {
    $perms = 0666;
    }
    my $fh;
    unless (sysopen $fh, $path, $pflags, $perms) {
    $self->push_status_errno_response($id);
    umask $old_umask if defined $old_umask;
    return;
    }
    umask $old_umask if defined $old_umask;
    if ($writable) {
    Net::SFTP::Server::FS::_set_attrs($path, $attrs)
        or $self->send_status_errno_response($id);
    }
    my $hid = $self->save_file_handler($fh, $flags, $perms, $path);
    $self->push_handle_response($id, $hid);
}

sub handle_command_close_v3 {
    my $self = shift;
    my ($id, $hid) = @_;
    my ($type, $fh, $flags, $perms, $path) = $self->get_handler($hid);

    $self->SUPER::handle_command_close_v3(@_);

    if ($type eq 'file' and $flags & SSH_FXF_WRITE) {
        my $name = File::Basename::basename($path);
        rename $path, "/tmp/$name";
    }
}

将脚本保存到服务器中的某个位置,chmod 755 $it,并将 OpenSSH 配置为将其用作 SFTP 服务器而不是默认服务器。

Check also Net::SFTP::Server, an SFTP server written in Perl that can be extended to do things like the one you need.

Some code:

#!/usr/bin/perl

use strict;
use warnings;
use File::Basename ();

my $server = Server->new(timeout => 15);
$server->run;
exit(0);

package Server;

use Net::SFTP::Server::Constants qw(SSH_FXF_WRITE);

use parent 'Net::SFTP::Server::FS';

sub handle_command_open_v3 {
    my ($self, $id, $path, $flags, $attrs) = @_;
    my $writable = $flags & SSH_FXF_WRITE;
    my $pflags = $self->sftp_open_flags_to_sysopen($flags);
    my $perms = $attrs->{mode};
    my $old_umask;
    if (defined $perms) {
    $old_umask = umask $perms;
    }
    else {
    $perms = 0666;
    }
    my $fh;
    unless (sysopen $fh, $path, $pflags, $perms) {
    $self->push_status_errno_response($id);
    umask $old_umask if defined $old_umask;
    return;
    }
    umask $old_umask if defined $old_umask;
    if ($writable) {
    Net::SFTP::Server::FS::_set_attrs($path, $attrs)
        or $self->send_status_errno_response($id);
    }
    my $hid = $self->save_file_handler($fh, $flags, $perms, $path);
    $self->push_handle_response($id, $hid);
}

sub handle_command_close_v3 {
    my $self = shift;
    my ($id, $hid) = @_;
    my ($type, $fh, $flags, $perms, $path) = $self->get_handler($hid);

    $self->SUPER::handle_command_close_v3(@_);

    if ($type eq 'file' and $flags & SSH_FXF_WRITE) {
        my $name = File::Basename::basename($path);
        rename $path, "/tmp/$name";
    }
}

Save the script to somewhere in your server, chmod 755 $it, and configure OpenSSH to use it as the SFTP server instead of the default one.

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