File: simpleftp.in

package info (click to toggle)
inn2 2.4.5-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,912 kB
  • ctags: 7,860
  • sloc: ansic: 85,104; perl: 11,427; sh: 9,863; makefile: 2,498; yacc: 1,563; python: 298; lex: 252; tcl: 7
file content (90 lines) | stat: -rw-r--r-- 1,812 bytes parent folder | download
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/perl -w

# simpleftp - Rudimentary FTP client.
#
# Author: David Lawrence <tale@isc.org>.
#         Rewritten by Julien Elie <julien@trigofacile.com> to use Net::FTP.
#
# Fetch files to the local machine based on URLs on the command line.
# INN's configure searches for ncftp, wget, linx, et caetera,
# but they're all system add-ons, so this is provided.
#
# Perl 5 is already required by other parts of INN; it only took a half hour
# to write this, so this was the easiest way to go for a backup plan.
#
# This script is nowhere near as flexible as libwww.  If you really need
# that kind of power, get libwww and use it.  This is just sufficient for what
# INN needed.

use strict;
use Net::FTP;
use Sys::Hostname;

$0 =~ s(.*/)();

my $usage = "Usage: $0 ftp-URL ...\n";

@ARGV
  or die $usage;

for (@ARGV) {
  m(^ftp://)
    or die $usage;
}

my ($lasthost, $ftp);

# This will keep track of how many _failed_.
my $exit = @ARGV;

for (@ARGV) {
  my ($host, $path) = m%^ftp://([^/]+)(/.+)%;
  my $user = 'anonymous';
  my $pass = (getpwuid($<))[0] . '@' . hostname;
  my $port = 21;

  unless (defined $host && defined $path) {
    warn "$0: bad URL: $_\n";
    next;
  }

  if ($host =~ /(.*):(.*)\@(.*)/) {
    $user = $1;
    $pass = $2;
    $host = $3;
  }

  if ($host =~ /(.*):(.*)/) {
    $port = $1;
    $host = $2;
  }

  if (defined $lasthost && $host ne $lasthost) {
    $ftp->quit;
    $lasthost = undef;
  }

  unless (defined $lasthost) {
    $ftp = Net::FTP->new($host, Port => $port)
      or next;
    $ftp->login($user, $pass)
      or next;
  }

  my $localfile = $path;
  $path =~ s([^/]+$)();
  $localfile =~ s(.*/)();

  $ftp->cwd($path)
    or next;
  $ftp->get($localfile)
    or next;

  $exit--;
  $lasthost = $host;
}

$ftp->quit
  if defined $lasthost;

exit $exit;