-
Notifications
You must be signed in to change notification settings - Fork 57
/
check_ftp.pl
77 lines (69 loc) · 1.68 KB
/
check_ftp.pl
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
#! /usr/bin/perl
# nagios: -epn
# anders@fupp.net, 2014-05-05
# Test active/passive FTP + upload/download
use Net::FTP;
use Getopt::Std;
use File::Basename;
getopts('h:u:p:df:');
sub usage {
print "Usage: check_ftp_alt -h <host> -u <user> -p <pass> [-f <file to upload>]\n";
exit(1);
}
usage unless ($opt_h && $opt_u && $opt_p);
if ($opt_d) {
$debug = 3;
} else {
$debug = 0;
}
my @chars = ("A".."Z", "a".."z");
my $string;
my $upload_message=' ';
$string .= $chars[rand @chars] for 1..8;
$randfile = "/tmp/$string.dat";
$|=1; # Flush stdout after every write.
sub errexit {
my $txt = shift;
print "$txt\n";
exit(2);
}
sub doftp {
my $passive = shift;
if ($passive == 0) {
$modetxt = "actively";
# print "Active.\n";
# $ENV{'FTP_PASSIVE'} = 0;
} else {
# print "Passive.\n";
$modetxt = "passively";
# $ENV{'FTP_PASSIVE'} = 1;
}
# print "passive=$passive debug=$debug\n";
$ftp = Net::FTP->new($opt_h, Timeout => 5, Passive => $passive, Debug => $debug);
#, Debug => 1) {
unless (defined $ftp) {
errexit("Could not $modetxt connect with FTP to $opt_h: $@");
}
unless ($ftp->login($opt_u, $opt_p)) {
errexit("Could not login as FTP user $opt_u.");
}
if ($opt_f) {
$upload_message=' and upload/delete ';
$basef = basename($opt_f);
unless ($ftp->put($opt_f)) {
errexit("Could not $modetxt upload file $basef.");
}
if ($ftp->get($basef, $randfile)) {
unlink($randfile);
} else {
errexit("Could not $modetxt download file $basef after uploading.");
}
unless ($ftp->delete($basef)) {
errexit("Could not $modetxt delete file $basef.");
}
}
}
doftp(1);
doftp(0);
print "All FTP tests, active/passive login${upload_message}performed OK.\n";
exit(0);