File: sftp_tail.pl

package info (click to toggle)
libnet-sftp-foreign-perl 1.73%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 472 kB
  • sloc: perl: 5,377; sh: 48; makefile: 7
file content (41 lines) | stat: -rw-r--r-- 696 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl

use strict;
use warnings;

use Net::SFTP::Foreign;
use Fcntl qw(SEEK_END);

@ARGV == 1
    or usage();

my ($host, $file) = $ARGV[0] =~ /([^:]+):(.+)/ or usage();

my $sftp = Net::SFTP::Foreign->new($host);
$sftp->error and die "Unable to connect to remote host: ".$sftp->error."\n";

my $fh = $sftp->open($file)
    or die "Unable to open file $file: ".$sftp->error."\n";

# goto end of file
seek($fh, 0, SEEK_END);

my $sleep = 1;
while (1) {
    while (<$fh>) {
        print;
        $sleep = 1;
    }
    print "### sleeping $sleep\n";
    sleep $sleep;
    $sleep++ unless $sleep > 5;
}

sub usage {
    warn <<EOW;
Usage:
  $0 [user@]host:/path/to/file
EOW
    exit 0;

}