works.
authorroot@py2.ccos.de
Fri, 16 Oct 2009 23:23:26 +0200
changeset 0 07bc300bc0f5
child 1 f5d804981be8
child 5 f9e6018b4a54
works.
ftpipe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ftpipe	Fri Oct 16 23:23:26 2009 +0200
@@ -0,0 +1,129 @@
+#! /usr/bin/perl
+use strict;
+use warnings;
+use File::Temp;
+use Pod::Usage;
+use Getopt::Long;
+use if $ENV{DEBUG} => qw(Smart::Comments);
+use feature qw(say switch);
+
+# host: backup.ccos.de
+# user: 54117
+# pass: aOUN9I6v
+
+my $opt_put = 0;
+my $opt_get = 0;
+my $opt_debug = 0;
+my $opt_cmd = "";
+
+sub exec_create(@);
+
+
+sub parse_url($);
+
+MAIN: {
+    GetOptions(
+	"p|put!" => sub { $opt_cmd = "put" },
+	"g|get!" => sub { $opt_cmd = "get" },
+	#"l|ls!"  => 
+	#"r|rm!"  => 
+	"debug!" => \$opt_debug,
+	"h|help" => sub { pod2usage(-verbose => 1, -exit => 0) },
+	"m|man" => sub { pod2usage(-verbose => 2, -exit => 0) },
+    ) and @ARGV or pod2usage();
+
+    my %ftp = parse_url(shift);
+
+    ### %ftp
+    die "need hostname" if not defined $ftp{host};
+    die "need filename" if not defined $ftp{file};
+
+    my $ftp = new FTP($ftp{host}, Debug => $opt_debug, Passive => 1);
+    $ftp->login($ftp{user}, $ftp{pass});
+    $ftp->cwd($ftp{dir}); 
+
+    given ($opt_cmd) {
+	when("put")  { $ftp->put(*STDIN, $ftp{file}) }
+	when("get")  { $ftp->get($ftp{file}, *STDOUT) }
+	default      { pod2usage }
+    }
+
+    exit;
+    
+    $ftp->put(*STDIN, $ftp{file});
+    print $ftp->dir, "\n";
+
+}
+
+sub parse_url($) {
+    $_[0] =~ m{^ftp://
+	    (?:(?<user>.*?)(?::(?<pass>.*))?@)?
+	    (?<host>[a-z\d_\.]+)/
+	    (?:(?<dir>.+)/)?(?<file>\S+)?}x;
+    return my %r = %+;
+}
+
+{ package FTP;
+  use base ("Net::FTP");
+
+  sub new {
+    my $class = shift;
+    my $self = bless $class->SUPER::new(@_)
+	or die $@;
+    return bless $self => $class;
+  }
+
+  sub login {
+    my $self = shift;
+    $self->SUPER::login(@_) or die "Can't login: " . $self->message;
+  }
+
+  sub cwd {
+    my $self = shift;
+    $self->SUPER::cwd(@_) or die "Can't chdir: " . $self->message;
+  }
+
+  sub put {
+    my $self = shift;
+    $self->SUPER::put(@_) or die "Can't put: " . $self->message;
+  }
+
+  sub get {
+    my $self = shift;
+    $self->SUPER::get(@_) or die "Can't put: " . $self->message;
+  }
+}
+
+
+__END__
+
+=head1 NAME
+
+ftpipe - FTP via a pipe
+
+=head1 SYNOPSIS
+
+    ftpipe --put ftp://[user[:pass]]@server/dir/file
+    ftpipe --get ftp://[user[:pass]]@server/dir/file
+
+=head1 OPTIONS
+
+=over
+
+=item B<-g>|B<--get>
+
+Get the file from the remote server and print it to stdout.
+
+=item B<-p>|B<--put>
+
+Put the data read from stdin to the remote file.
+
+
+=back
+
+=head1 FILES
+
+The F<~/.netrc> is used.
+
+=cut
+# vim:sts=4 sw=4 aw ai sm: