Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Problems with ftp scripts

by LiTinOveWeedle (Scribe)
on May 11, 2001 at 21:55 UTC ( [id://79788]=note: print w/replies, xml ) Need Help??


in reply to Problems with ftp scripts

#!/usr/local/bin/perl # -------------------------------------------------------------------- +----- #global config $local_dir = "C:/Temp/"; $transfered_dir = "C:/Temp/Transfered/"; @file_types = ("\.doc", "\.pdf", "\.prn", "\.ps"); $ftp_site = "ftp.domain.com"; $ftp_timeout = 30; $ftp_retry = 3; $ftp_user = "test"; $ftp_pass = "1234"; $log = "1"; $log_file_path = "logs/ftp.log"; $file_lock = ""; $show_logs_lines = 20; $ok_redirect_to = "modules/templates/template4.html"; $ko_redirect_to = "modules/templates/template3.html"; $run_redirect_to = "modules/templates/template2.html"; $indicator_file_path = "logs/ftptransfer.tmp"; $ftp_template[0] = "modules/templates/template1.html"; # -------------------------------------------------------------------- +----- # main program body - no changes needed under this line use Net::FTP; use CGI; $|++; $query = new CGI; if ( $query -> param('method') eq "generate" ) { &ftp_generate; } elsif ( $query -> param('method') eq "transfer" ) { &ftp_transfer; } else { die; } # -------------------------------------------------------------------- +----- sub ftp_generate { $html_template_path = $ftp_template[$query -> param('template')]; if ( $html_template_path eq "" ) { $html_template_path = $ftp_temp +late[0]; } &ftp_read_html; $ftp_html =~ s/<<log>>/&ftp_read_log/isge; $ftp_html =~ s/<<dir>>/&ftp_read_dir/isge; print $ftp_html."\n"; } sub ftp_transfer { if ( &ftp_read_indicator ) { &ftp_write_indicator(1); &ftp_logging("#Transfer script started - from web"); if ( &ftp_read_file ) { if ( fork() ) { print $query->redirect( -uri => $ok_redirect_to ); exit; } else { close STDIN; close STDOUT; if ( &ftp_connect_to ) { &ftp_transfer_file; } } } else { &ftp_logging("No files to transfer!"); print $query->redirect( -uri => $ko_redirect_to ); } &ftp_write_indicator(0); } } # -------------------------------------------------------------------- +----- # read file in dir and filter files to transfer sub ftp_read_file { my ( $type, $name, $temp, @temp ); if ( not opendir(DIR, $local_dir) ) { &ftp_logging("Can't open local dir"); die; } @temp = readdir(DIR); closedir(DIR); foreach $name ( sort @temp ) { foreach $type ( @file_types ) { $temp = $local_dir . $name; if ( $name =~ /^[^\.]*($type)$/is and -f $temp ) { push( @files, $name ); } } } $temp = @files; if ( $temp != 0 ) { return 1; } else { return 0; } } # -------------------------------------------------------------------- +----- # logging sub ftp_logging { my ( @time, $i ); if ( $log ne "" ) { if ( not open(LOG, ">>".$log_file_path) ) { die; } @time = localtime(time); $time[4] = $time[4] + 1; $time[5] = $time[5] + 1900; for ( $i=0; $i<@time; $i++ ) { if ( $time[$i] < 10 ) { $time[$i] = "0" . $time[$i]; } } if ( $file_lock ) { flock(LOG, 2); } print LOG "$time[3]/$time[4]/$time[5] $time[2]:$time[1]:$time[ +0] \t @_ \r\n"; close(LOG); } } # -------------------------------------------------------------------- +----- # conect to ftp server sub ftp_connect_to { if ( $ftp = Net::FTP->new($ftp_site) ) { &ftp_logging("Conected to $ftp_site"); } else { &ftp_logging("Can't connect to $ftp_site"); return 0; } if ( $ftp->login($ftp_user, $ftp_pass) and $ftp->binary() ) { &ftp_logging("Authentificated, transfer mode set"); } else { return 0; } return 1; } # -------------------------------------------------------------------- +----- # ftp transfer file sub ftp_transfer_file { my ( $name, $counter, $attemp, $error ); $counter = 0; &ftp_logging("Transfering files..."); foreach $name ( @files ) { $error = 1; $attemp = 1; while ( $attemp <= $ftp_retry ) { if ( $ftp->put( $local_dir. $name ) and &ftp_sum_check($name) +) { $attemp = $ftp_retry + 1; $counter++; if ( not -d "$transfered_dir" ) { mkdir( $transfered_dir ); } rename ( $local_dir. $name, $transfered_dir. $name ); &ftp_logging("$name transfered"); } else { &ftp_logging("$name not transfered - attemp $attemp"); $attemp++; $ftp->quit; sleep 10; &ftp_connect_to; } } } $ftp->quit; &ftp_logging("Transfer complete, $counter file transfered"); } # -------------------------------------------------------------------- +----- # check transfered file sub ftp_sum_check { my ( $name, @file ); $name = @_[0]; @file = stat( $local_dir. $name ); if ( $ftp->size($name) == $file[7] ) { &ftp_logging("Control size of file $name validated - $file[7] byte +s"); return 1; } else { &ftp_logging("File $name has bad control size - should be $file[7] + bytes"); return 0; } } # -------------------------------------------------------------------- +----- # read data from log sub ftp_read_log { my ( $line, $temp, $temp1, $html ); if ( not open(FILE, $log_file_path) ) { &ftp_logging( "Can't open log file." ); die; } if ( $file_lock ) { flock(FILE, 1); } @logdata = (); while ( $line = <FILE> ) { chomp($line); push(@logdata, $line); } close(FILE); if ( $query->param('log') > $show_logs_lines ) { $show_logs_lines += $query->param('log'); } $temp = @logdata; if ( $temp > $show_logs_lines ) { $temp = $temp - $show_logs_lines +; } splice ( @logdata, 1, $temp ); foreach $line ( @logdata ) { $html .= $line."<br>"; } return $html; } # -------------------------------------------------------------------- +----- # read file in dir and filter files to transfer sub ftp_read_dir { my ( $type, $name, $temp, @temp, $html ); $html .= "<b>Files to transfer:</b><br>"; if ( not opendir(DIR, $local_dir) ) { $html .= "nemohu otevrit adr +esar<br>"; } @temp = readdir(DIR); closedir(DIR); foreach $name ( sort @temp ) { $temp = $local_dir . $name; if ( -f $temp ) { $html .= $name."<br>"; } } $html .= "<p><b>Transfer files:</b><br>"; if ( not opendir(DIR, $transfered_dir) ) { $html .= "nemohu otevri +t adresar<br>"; } @temp = readdir(DIR); closedir(DIR); foreach $name ( sort @temp ) { $temp = $transfered_dir . $name; if ( -f $temp ) { $html .= $name."<br>"; } } return $html; } # -------------------------------------------------------------------- +----- # read indicatot file and check if scripts allready not running sub ftp_read_indicator { my ( $line ); if ( not open(IND, $indicator_file_path) ) { &ftp_logging( "Can't open indicator file." ); die; } if ( $file_lock ) { flock(IND, 1); } @logdata = (); $line = <IND>; close(IND); if ( $line eq "0" ) { return 1; } else { return 0; } } # -------------------------------------------------------------------- +----- # write indicator to file sub ftp_write_indicator { my ( $line ) = @_[0]; if ( not open(IND, ">".$indicator_file_path) ) { &ftp_logging( "Can't open indicator file." ); die; } if ( $file_lock ) { flock(IND, 2); } print IND "$line"; close(IND); } # -------------------------------------------------------------------- +----- # read html template file sub ftp_read_html { my ( @html ); if ( open(HTML, $html_template_path) ){ @html=<HTML>; close(HTML); } else { &ftp_logging( "Can't open html read template file. file wasn't fou +nd at $html_template_path." ); die; } $ftp_html = join( "", @html ); }

I made some changes before posting, hope that code is fine.

Li Tin O've Weedle
mad Tsort's philosopher

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://79788]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found