Help - Search - Member List - Calendar
Full Version: writting a detailed log
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Tim Wolak
All,

I am writtting this script for a client that will transfer their files
via SFTP and write a small log to their system. I would also like it to
write a log on our server as well, other than just doing what I have,
writting the date and the file transfered. What can I do to capture
erros from SFTP so that if the transfer fails I can write it to the log
file? Script is below:

Thanks for the help.
Tim

#Here is my rules to keep me strict
use strict;
use Net::SFTP;

#Who am I and where am I going?
my $user = 'user';
my $pass = 'password';
my $sftp;
my $home = "/home/user/test.txt";
my $path = "/home/user/test.txt";
my $host = '0.0.0.0';

#Where the real action takes place...
$sftp = Net::SFTP->new($host, "user" => $user, "password" => $pass)
or die "Can't login $!n";
$sftp->put($home,$path) || die "Can't open $!n";

# Writting log files for transfers
#
open LOGS, ">> cshareTransfer.log" or die "Can't open file, $!n";

# Get my dates
#
my $date;
my ($sec,$min,$hour,$mday,$mon,$year) = (localtime) [0,1,2,3,4,5];
$year=$year+1900;
$mon=$mon+1;
$date = sprintf("%02d%02d%02d", $year,$mon,$mday);
#
# Write my logs so I can track myself
#
print LOGS "Transfer date:$date, File transfered:$pathn";
close LOGS;

Zentara
On Fri, 04 Mar 2005 15:43:56 -0600, [Email Removed] (Tim Wolak)
wrote:

QUOTE
All,

I am writtting this script for a client that will transfer their files
via SFTP and write a small log to their system.  I would also like it to
write a log on our server as well, other than just doing what I have,
writting the date and the file transfered.  What can I do to capture
erros from SFTP so that if the transfer fails I can write it to the log
file?  Script is below:

Thanks for the help.
Tim

There are a couple of ways you can do it. Read perldoc Net::SFTP

You could just retrieve the same file you just transferred, and see
if the md5sums match. Or you could take an ls of the directory.

Here is the idea for the ls. It returns an arraoy-of-hashes, so it
gets a little complex, you made need to adjust the regex I used.
I modified your script slightly.

#!/usr/bin/perl
use warnings;
use strict;
use Net::SFTP;

#Who am I and where am I going?
my $user = 'z';
my $pass = 'foofum';
my $sftp;
my $file = "test.txt";
my $put_to_dir = "/home/z/2";
my $put_from = $0; #upload this file
my $host = '0.0.0.0';
my $result;

#Where the real action takes place...
$sftp = Net::SFTP->new($host,
"user" => $user,
"password" => $pass,
) or die "Can't login $!n";

$sftp->put($put_from, "$put_to_dir/$file") || die "Can't open $!n";

#do check here
my @AOH = $sftp->ls($put_to_dir);
foreach my $href(@AOH){
foreach my $key( %{$href} ){
if(defined ${$href}{$key}){
if( ${$href}{$key} =~ /-rwxr-xr-x(.*)$file$/ ){
$result = ${$href}{$key};

}
}
}
}

# Writting log files for transfers
open LOGS, ">> $0.log" or die "Can't open file, $!n";
# Get my dates
my $date;
my ($sec,$min,$hour,$mday,$mon,$year) = (localtime) [0,1,2,3,4,5];
$year=$year+1900;
$mon=$mon+1;
$date = sprintf("%02d%02d%02d", $year,$mon,$mday);
#
# Write my logs so I can track myself
print LOGS
"Transfer date:$date, File transfered:$put_to_dir/$filen$resultn";
close LOGS;
__END__




--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

Wiggins d'Anconia
Tim Wolak wrote:
QUOTE
All,

I am writtting this script for a client that will transfer their files
via SFTP and write a small log to their system.  I would also like it to
write a log on our server as well, other than just doing what I have,
writting the date and the file transfered.  What can I do to capture
erros from SFTP so that if the transfer fails I can write it to the log
file?  Script is below:

Thanks for the help.
Tim

If you are looking for better logging interfaces, check out log4perl,
not aware of anything that can top it.

http://log4perl.sf.net

HTH,

http://danconia.org

[snip code]


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.