check_ftpget.pl 911 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/perl -w
  2. ## Written 12/5/00 Jeremy Hanmer
  3. # $Id$
  4. use strict;
  5. use Net::FTP;
  6. use Getopt::Std;
  7. use vars qw($opt_H $opt_u $opt_p $opt_f);
  8. getopts("H:u:p:f:");
  9. my $host = $opt_H ||
  10. die "usage: check_ftp.pl -h host [<-u user> <-p pass> <-f file>]\n";
  11. my $username = $opt_u || 'anonymous';
  12. my $pass = $opt_p || "$ENV{'LOGNAME'}\@$ENV{'HOSTNAME'}" ;
  13. my $file = $opt_f;
  14. my $status = 0;
  15. my $problem;
  16. my $output = "ftp ok";
  17. my $ftp = Net::FTP->new("$host") ||
  18. &crit("connect");
  19. $ftp->login("$username", "$pass") ||
  20. &crit("login");
  21. $ftp->get($file) ||
  22. &crit("get") if $file;
  23. sub crit()
  24. {
  25. $problem = $_[0];
  26. $status = 2;
  27. if ( $problem eq 'connect' ) {
  28. $output = "can't connect";
  29. } elsif ( $problem eq 'login' ) {
  30. $output = "can't log in";
  31. } elsif ( $problem eq 'get' ) {
  32. $output = "cant get $file";
  33. }
  34. }
  35. print "$output\n";
  36. exit $status;