4

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?

Andrew Keller
  • 3,198
  • 5
  • 36
  • 51
sid
  • 61
  • 6
  • 2
    Maybe this question belongs on superuser.com – hochl Dec 14 '11 at 01:28
  • 4
    answered here: http://stackoverflow.com/questions/7566569/how-to-continuosly-monitor-the-directory-using-dnotify-inotify-command – matiu Dec 14 '11 at 01:31
  • 1
    The [incron](http://inotify.aiken.cz/?section=incron&page=doc&lang=en) program is like a combination of `inotify(7)` and `crond(8)` -- it waits on filesystem events and then takes action. – sarnold Dec 14 '11 at 01:33

2 Answers2

2

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

jman
  • 11,334
  • 5
  • 39
  • 61
  • And if you don't want to write a program in c you can use inotify-tools right from the command line: https://github.com/rvoicilas/inotify-tools/wiki/ – Lee Netherton Dec 14 '11 at 15:01
1

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.

salva
  • 9,943
  • 4
  • 29
  • 57