Newbie: Perl how evaluate files newer than an hour within ftp

Newbie: Perl how evaluate files newer than an hour within ftp

am 17.08.2010 14:37:51 von Atropo

Hi all, i have this simple script to check if a file has arrive since
the last hour

find2perl tmp -type f -eval '-M $_ < 1/24' -print |perl

but now i would like to check in a remote server, maybe on ftp
session. i have this ftp.pl

perl -w >> ftp.log -MNet::FTP -le'
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek
+, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$theGMTime = "$hour:$minute:$second, $months[$month] $dayOfMonth,
$year";
( $host, $user, $pass, $dir ) = @ARGV;
$ftp = Net::FTP->new($host) or die "$@\n"; #send mail if cannot
connect do not know how to do it
$ftp->login( $user, $pass ) or die $ftp->message;
$ftp->cwd($dir) or die $ftp->message;
$ftp->binary;
for $file ($ftp->ls){
push @files, $file;
}

for $file (@files) {
$quesesto=localtime($ftp->mdtm($file));
$localtimenoformat=localtime(time());
$mdtmnoformat=$ftp->mdtm($file);
print "MDTM with FORMAT = $quesesto\n";
print "Localtime No Format = $localtimenoformat\n";
print "MDTM No Format = $mdtmnoformat\n";
#envia_mail ();
}
$ftp->quit or die $ftp->message;

sub envia_mail {
print "Content-type: text/html\n\n";

#$title='';
$to="alexis_vasquez\@codetel\.com\.do";
$from= `hostname`;
#$subject='';

open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message \n";

close(MAIL);

print "$title\n\n\n";

## HTML content sent, let use know we sent an email
print "

$title



A message has been sent from $from to $to

";

}

'host user pass dir

Still cant't figure out how to check if file is newer that an hour. I
can get this:

MDTM with FORMAT = Fri Aug 21 12:23:15 2009
Localtime No Format = Fri Aug 13 10:17:10 2010
MDTM No Format = 1250871795

But don't know how to get the 'localtime with NO Format' to compare
with the 'MDTM with NO Format'


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Newbie: Perl how evaluate files newer than an hour within ftp

am 18.08.2010 14:39:54 von Shlomi Fish

Hi Atropo,

On Tuesday 17 August 2010 15:37:51 Atropo wrote:
> Hi all, i have this simple script to check if a file has arrive since
> the last hour
>=20
> find2perl tmp -type f -eval '-M $_ < 1/24' -print |perl
>=20
> but now i would like to check in a remote server, maybe on ftp
> session. i have this ftp.pl
>=20
> perl -w >> ftp.log -MNet::FTP -le'

Please:

1. Don't write your program as a gigantic -e '...' block. Instead - put it =
in=20
a separate file. You can't honestly expect us to debug it.

2. See https://www.socialtext.net/perl5/index.cgi?ancient_perl

3. "use strict;" and "use warnings;".

4. Quoting perlbot:

{{{
use strict; use warnings; use lexical filehandles instead of glob=
als=20
(open my $tmp ..vs.. open TMP), use three argument form of open, don't use=
=20
prototypes unless you really want to. don't call subs with & unless it's=20
required (&sub() ..vs.. sub()). check for errors (open (); ..vs.. open() o=
r=20
die $!)
}}}

> ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek
> +, $dayOfYear, $daylightSavings) =3D localtime();

You should use a good datetime module instead. I like=20
http://search.cpan.org/dist/DateTime/ but there are others.

> $year =3D 1900 + $yearOffset;
> $theGMTime =3D "$hour:$minute:$second, $months[$month] $dayOfMonth,
> $year";
> ( $host, $user, $pass, $dir ) =3D @ARGV;
> $ftp =3D Net::FTP->new($host) or die "$@\n"; #send mail if cannot
> connect do not know how to do it
> $ftp->login( $user, $pass ) or die $ftp->message;
> $ftp->cwd($dir) or die $ftp->message;
> $ftp->binary;
> for $file ($ftp->ls){
> push @files, $file;
> }
>=20
> for $file (@files) {
> $quesesto=3Dlocaltime($ftp->mdtm($file));
> $localtimenoformat=3Dlocaltime(time());
> $mdtmnoformat=3D$ftp->mdtm($file);
> print "MDTM with FORMAT =3D $quesesto\n";
> print "Localtime No Format =3D $localtimenoformat\n";
> print "MDTM No Format =3D $mdtmnoformat\n";
> #envia_mail ();
> }
> $ftp->quit or die $ftp->message;
>=20
> sub envia_mail {
> print "Content-type: text/html\n\n";
>=20
> #$title=3D'';
> $to=3D"alexis_vasquez\@codetel\.com\.do";
> $from=3D `hostname`;
> #$subject=3D'';
>=20
> open(MAIL, "|/usr/sbin/sendmail -t");
>=20
> ## Mail Header
> print MAIL "To: $to\n";
> print MAIL "From: $from\n";
> print MAIL "Subject: $subject\n\n";
> ## Mail Body
> print MAIL "This is a test message \n";
>=20
> close(MAIL);
>=20
> print "$title\n\n\n";
>=20
> ## HTML content sent, let use know we sent an email
> print "
>

$title


>=20
> A message has been sent from $from to $to
>=20
> ";


You should use a here-document here.

>=20
> }
>=20
> 'host user pass dir

Don't append the =ABhost=BB parameter to the end of hte '....' - it will be=
part=20
of the program.

>=20
> Still cant't figure out how to check if file is newer that an hour. I
> can get this:
>=20
> MDTM with FORMAT =3D Fri Aug 21 12:23:15 2009
> Localtime No Format =3D Fri Aug 13 10:17:10 2010
> MDTM No Format =3D 1250871795
>=20
> But don't know how to get the 'localtime with NO Format' to compare
> with the 'MDTM with NO Format'

Just use timestamps, or use the DateTime module. You can find if a timestam=
p=20
is in the last hour by "time() - $timestamp < 60*60".

Regards,

Shlomi Fish

=2D-=20
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/