Line # Revision Author
1 110 dpavlin #!/usr/bin/perl -w
2
3 # FTP badwidth tester
4 #
5 # 2006-09-02 Dobrica Pavlinusic <dpavlin@rot13.org>
6 #
7 # This scripts expect input from STDIN (so you can pipe configuration to it)
8 # in following format:
9 #
10 # descriptive name [tab] ftp://username:password@host/path/ [tab] file_to_transfer
11 #
12 # it will try to upload and download file_to_transfer from current directory
13
14 use Net::FTP;
15 use Time::HiRes qw/time/;
16 use POSIX qw/strftime/;
17
18 my $debug = 0;
19
20 sub dump_stat($$$) {
21 my ($what, $size, $dur) = @_;
22
23 printf("%s %d bytes in %.2f s (%.2f K/s)\n",
24 $what, $size, $dur,
25 ($size / 1024) / $dur
26 );
27 }
28
29 while(<>) {
30 chomp;
31
32 my ($name, $uri, $file) = split(/\t+/,$_,3);
33
34 print STDERR "name: $name uri: $uri file: $file\n" if ($debug);
35
36 unless ($uri =~ m!^ftp://(?:(?:([^:]*):?)?([^@]*)@)?([^/]+)/?(.*)$!i) {
37 print STDERR "SKIPPED: $_\n";
38 next;
39 }
40
41 my ($user, $passwd, $host, $path) = ($1,$2,$3,$4);
42
43 print STDERR "user: $user passwd: $passwd host: $host path: $path\n" if ($debug);
44
45 my $ftp = Net::FTP->new($host, Debug => 0) or die "Cannot connect to $host: $@";
46
47 $ftp->login($user, $passwd) or die "Cannot login ", $ftp->message;
48
49 $ftp->cwd($path) or die "Cannot change working directory ", $ftp->message;
50
51 die "File $file doesn't exist" unless (-e $file);
52
53 my $size = (stat($file))[7];
54
55 print STDERR "file: $file [$size bytes]\n" if ($debug);
56
57 print strftime('%Y-%m-%d %H:%M:%S', localtime()), "\t$name\n";
58
59 my $t = time();
60
61 $ftp->put($file) or die "put failed ", $ftp->message;
62
63 my $dur = time() - $t;
64
65 dump_stat('PUT', $size, $dur);
66
67 111 dpavlin my $tmp = '.' . $file . '.tmp';
68
69 110 dpavlin $t = time();
70
71 111 dpavlin $ftp->get($file, $tmp) or die "get failed ", $ftp->message;
72 110 dpavlin
73 $dur = time() - $t;
74
75 dump_stat('GET', $size, $dur);
76
77 111 dpavlin unlink $tmp or die "can't erase $tmp: $!";
78
79 110 dpavlin $ftp->quit;
80 }