check_dl_size.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #! /usr/bin/perl -wT
  2. # (c)2001 Patrick Greenwell, Stealthgeeks, LLC. (patrick@stealthgeeks.net)
  3. # Licensed under the GNU GPL
  4. # http://www.gnu.org/licenses/gpl.html
  5. #
  6. # check_dl_size: Attempts to download a specified file via FTP and verify
  7. # it is a specified size.
  8. # Requires Net::FTP
  9. # Version 1.0
  10. # Last Updated: 8/31/01
  11. BEGIN {
  12. if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
  13. $runtimedir = $1;
  14. $PROGNAME = $2;
  15. }
  16. }
  17. require 5.004;
  18. use strict;
  19. use Getopt::Long;
  20. use vars qw($opt_H $opt_f $opt_s $opt_t $verbose $PROGNAME);
  21. use lib $main::runtimedir;
  22. use utils qw($TIMEOUT %ERRORS &print_revision &usage &support &is_error);
  23. use Net::FTP;
  24. sub help ();
  25. sub print_help ();
  26. sub print_usage ();
  27. sub version ();
  28. sub display_res($$);
  29. my ($ftpfile, $ftpdir, $filesize, $answer) = ();
  30. my $state = $ERRORS{'UNKNOWN'};
  31. # Directory to place file. If your machine is not secure DO NOT USE /tmp.
  32. my $dir = "/usr/local/netsaint/etc/tmp";
  33. # Username for login
  34. my $user = "anonymous";
  35. # Password (PLEASE TAKE APPROPRIATE PRECAUTIONS TO SECURE THIS)
  36. my $pass = "guest\@example.com";
  37. delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
  38. Getopt::Long::Configure('bundling', 'no_ignore_case');
  39. GetOptions
  40. ("V|version" => \&version,
  41. "h|help" => \&help,
  42. "v|verbose" => \$verbose,
  43. "H|hostname=s" => \$opt_H,
  44. "f|filename=s" => \$opt_f,
  45. "s|filesize=s" => \$opt_s,
  46. "t|timeout=s" => \$opt_t,
  47. );
  48. ($opt_H) || ($opt_H = shift) || usage("Host address or name not specified\n");
  49. my $host = $1
  50. if ($opt_H =~ m/^(([0-9]{1,3}\.){3}[0-9]{1,3}|(([a-z0-9]+(\-+[a-z0-9]+)*|\.))+[a-z])$/i);
  51. usage("Please provide a valid IP address or host name\n") unless ($host);
  52. ($opt_f) || ($opt_f = shift) || usage("File name not specified\n");
  53. if ($opt_f =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
  54. $ftpdir = $1;
  55. $ftpfile = $2;
  56. }
  57. ($opt_s) || ($opt_s = shift) || usage("File size not specified\n");
  58. usage("File size must be numeric value") unless ($opt_s =~ m/^[0-9]+$/);
  59. (($opt_t) && ($TIMEOUT = $opt_t)) || ($TIMEOUT = 120);
  60. usage("TIMEOUT must be numeric value") unless ($TIMEOUT =~ m/^[0-9]+$/);
  61. # Don't hang if there are timeout issues
  62. $SIG{'ALRM'} = sub {
  63. print ("ERROR: No response from ftp server (alarm)\n");
  64. exit $ERRORS{'UNKNOWN'};
  65. };
  66. alarm($TIMEOUT);
  67. # Make certain temporary directory exists
  68. if ( ! -e "$dir" ) {
  69. display_res("Temporary directory $dir does not exist.\n", "CRITICAL");
  70. }
  71. # Remove existing file if any
  72. if ( -e "$dir/$ftpfile") {
  73. unlink "$dir/$ftpfile" or
  74. display_res("Can't remove existing file $dir/$ftpfile.\n", "CRITICAL");
  75. }
  76. # Snarf file
  77. my $ftp = Net::FTP->new("$host", Passive => 1, Timeout => $TIMEOUT) or
  78. display_res("Can't connect to $host.\n", "CRITICAL");
  79. $ftp->login("$user","$pass") or
  80. display_res("Login to $host failed", "CRITICAL");
  81. $ftp->cwd("$ftpdir") or
  82. display_res("Can't change to directory $ftpdir.\n", "CRITICAL");
  83. $ftp->get($ftpfile, "$dir/$ftpfile") or
  84. display_res("Can't retrieve file $ftpfile.\n", "CRITICAL");
  85. $ftp->quit;
  86. # If file exists and is correct size we are happy.
  87. if (( -e "$dir/$ftpfile" ) && (($filesize = -s "/tmp/$ftpfile") eq $opt_s)) {
  88. display_res("File $ftpfile size OK: $filesize bytes.\n", "OK");
  89. } else {
  90. # Otherwise we are not happy.
  91. display_res("File $ftpfile size incorrect: $filesize bytes", "CRITICAL");
  92. }
  93. exit;
  94. sub print_usage () {
  95. print "Usage: $PROGNAME -H <host> -f <filename> -s <file size in bytes> -t <timeout> \n";
  96. }
  97. sub print_help () {
  98. print_revision($PROGNAME,'$ Revision: 1.0 $ ');
  99. print "Copyright (c) 2001 Patrick Greenwell, Stealthgeeks, LLC.\n\n";
  100. print_usage();
  101. support();
  102. }
  103. sub version () {
  104. print_revision($PROGNAME,'$ Revision: 1.0 $ ');
  105. exit $ERRORS{'OK'};
  106. }
  107. sub help () {
  108. print_help();
  109. exit $ERRORS{'OK'};
  110. }
  111. sub display_res ($$) {
  112. my ($answer, $state) = @_;
  113. print $answer;
  114. exit $ERRORS{$state};
  115. }