I have a script which should send a file via ftp, it fails after the ftp->put() call with a bad file descriptor error.

As you can tell from the code I am NOT a programmer, I'm a sys admin trying to automate this task.

Thanks for your help, Sal P.

#!/usr/bin/perl use strict; use Time::Local; use Net::FTP; #-------------# # Variables #-------------# my $logfile="c:\\FTP_Scripts\\logfile.txt"; my $numArgs= $#ARGV + 1; my $argnum; my $error_time_stamp; my $err; my $debug=0; my $xmlfile; #name of xml file including file path my $xmlfh; #xml file handle my $csvfile; #name of csv output file my $csvfh; #csv file handle my $csvdir; #csv file directory my $current_line; my $field; my @data_set; # Array to hold all the records in the xml + file my @record_xml_str; # Array to hold current record in XML form +at my @record; # Data extracted from the xml string my $dataset_id = 'REC id'; #sub-string used to identify the lines wit +h data my @RecordID; my @MeterPointName; my @Unit; my $DateTime; my @PulseCount; my @Multiplier; my @epoch_local; my $epoch_gmt; my $Interval; my $facility_code = "BLANK"; my $site; my $file; my $ftp; my $err; my $serverFTP="ftp.server.com"; my $serverID = "server"; my $serverPWD = "server"; my $putdir = "/"; #get current date time, $isdst holds Daylight Saving Time my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(ti +me); #-------------# # Main #-------------# ## Get command line parameters if($numArgs < 1){ print_usage(); exit; } $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp\n\n"); if($ARGV[0]){ $xmlfile=$ARGV[0]; $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Input XML file: $xmlfile"); } if($ARGV[1]){ $csvdir=$ARGV[1]; $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Output directory: $csvdir"); } if($ARGV[2]){ $site=$ARGV[2]; $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Site name: $site"); } ## Open XML file for reading if( open($xmlfh, '<', "$xmlfile") ){ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp >Successfully opened file: $xmlfile"); } else{ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Failed to open file: $xmlfile"); } my $result; my $line_num = 0; ## Get the lines with data, string starts with "^REC id.*" while( $current_line = <$xmlfh> ){ chomp ($current_line); #Get Facility and BuildingCode $result = index($current_line,"METER_ADDR"); if( $result == 1 ){ ($facility_code) = ($current_line =~ /^.\w+_\w+.(\w+)./); } # Identify the lines that begin with "REC id" $result = index($current_line,$dataset_id); if( $result == 1 ){ $data_set[$line_num] = $current_line; $line_num = $line_num + 1; } } my $x; my $field_num; for($x==0; $x < $line_num; $x++){ @record_xml_str = split /\>\</, $data_set[$x]; $field_num = 2; foreach $field (@record_xml_str){ # Identify the lines that begin with "REC id" $result = index($field,$dataset_id); if( $result ==1 ){ ( $RecordID[$x][0] ) = ( $field =~ /^.\w+\s\w+=\"(\d+)\"/); } #Identify time stamp $result = index($field, "TS"); if( $result ==0 ){ ($mon, $mday, $year, $hour, $min, $sec) = ($field =~ /(\d+)\/( +\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/); $mon = $mon - 1; $epoch_local[$x][1] = timelocal($sec,$min,$hour,$mday,$mon,$ye +ar); } #Identify data point $result = index($field, "XX"); if( $result ){ #Identify data fields ($MeterPointName[$x][$field_num], $Unit[$x][$field_num], $ +Multiplier[$x][$field_num], $PulseCount[$x][$field_num]) = ($field =~ + /(\w+)XX(\w+)XX(\d+).(\d+)/); $field_num++; } } } ## Identify the 2 latest records by comparing epoch_local my $y; my $current_rec=0; my $prev_rec=1; my $current_epoch_tmp = $epoch_local[0][1]; my $prev_epoch_tmp = $epoch_local[1][1]; for($x=0; $x < $line_num-1; $x++){ if($current_epoch_tmp < $prev_epoch_tmp){ $current_rec = $x+1; $prev_rec = $x; $current_epoch_tmp = $epoch_local[$x+1][1]; $prev_epoch_tmp = $epoch_local[$x][1]; } } ## Identify time interval $Interval = ($epoch_local[$current_rec][1] - $epoch_local[$prev_rec][1 +]) / 60; ## Create string for CSV file name $epoch_gmt = $epoch_local[$current_rec][1] + (5 * 60 * 60) - ($isdst * + 60 * 60); ($sec,$min,$hour,$mday,$mon,$year) = localtime($epoch_gmt); $year = $year + 1900; if($mon < 10){$mon = join('','0',$mon)}; if($mday < 10){$mday = join('','0',$mday)}; if($hour < 10){$hour = join('','0',$hour)}; if($min < 10){$min = join('','0',$min)}; if($sec < 10){$sec = join('','0',$sec)}; $csvfile = join('',$csvdir,'\\',$facility_code,'-',$year,$mon,$mday,'- +',$hour,$min,$sec,'.ast'); my $astfile = join('',$facility_code,'-',$year,$mon,$mday,'-',$hour,$m +in,$sec,'.ast'); $DateTime = join('',$mon,'/',$mday,'/',$year,' ',$hour,':',$min,':',$s +ec); ## Open file for writing if( open($csvfh, '>', "$csvfile") ){ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp >Successfully created file: $csvfile"); } else{ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Failed to create file: $csvfile"); } ## Write data to file $csvfh "\"MeterPointName,Unit,Interval,DateTime,PulseCount,Multiplier\ +"\n"; for($y=4; $y < $field_num-1; $y++){ if($Unit[$current_rec][$y] eq 'k'){$Unit[$current_rec][$y] = "kWH" +}; if($Unit[$current_rec][$y] eq 'g'){$Unit[$current_rec][$y] = "CF"} +; if($Unit[$current_rec][$y] eq 'p'){$Unit[$current_rec][$y] = "LBS" +}; if($MeterPointName[$current_rec][$y] ne ""){ if($MeterPointName[$current_rec][$y] eq "LT_Totalizer"){ $MeterPointName[$current_rec][$y] = "NYPH-Low Tension Totalize +r"; } print $csvfh "\"$MeterPointName[$current_rec][$y],$Unit[$curre +nt_rec][$y],$Interval,$DateTime,$PulseCount[$current_rec][$y],$Multip +lier[$current_rec][$y].0\"\n"; } } ## Close XML File close $xmlfh; ## Close CSV File close $csvfh; #---- Send file via FTP ----# #open FTP session with remote server $ftp=Net::FTP->new($serverFTP, Timeout=>20, Debug=>0, Passive=>1) or $err=1; if($err){ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp >Unable to connect to remote FTP serv +er $serverFTP\n$!"); die; } #login to to remote FTP server $ftp->login("$serverID", "$serverPWD") or $err=1; if($err){ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Unable to login to remote FTP server $s +erverFTP as $serverID\n$!"); die; } #change directory on FTP server $ftp->cwd($putdir) or $err=1; if ($err) { $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Unable to change directory $putdir on $ +serverFTP server\n$!"); die; } $ftp->binary(); # set binary mode my $localfile = join('',$csvdir,'\\',$astfile); $ftp->put($localfile) or $err=1; if ($err) { $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Error transferring $localfile\n$!"); die ; } # Log successfull upload $error_time_stamp = geterror_time_stamp(); logit("\n\n$error_time_stamp > Transfer successfull: FTPserver: $eluti +onsFTP\n File: $csvfile\n$!"); # End session $ftp->quit(); exit 0; #--------------# # Sub-routines #--------------# sub logit { my $line=shift; open(LOG,">>$logfile") or die("$! Can't open $logfile"); if($debug){ print "$line\n"; } print LOG "$line\n"; close LOG; } sub geterror_time_stamp { my ($sec, $min, $hour, $day, $month, $yr19, @rest) = localtime(time) +; $error_time_stamp = $month . "-" . $day . "-" . ($yr19 + 1900) . " " + . $hour . ":" . $min . ":" . $sec; return $error_time_stamp; } sub print_usage{ print "\nNo files were specified,\nCorrect usage is:\n"; print "xml-.st-convert.pl <XML input file> [output directory, opti +onal]\n\n"; return; }
Data written to log file

11-1-2010 21:46:23 > Input XML file: C:\Program Files\Ipswitch\WS_FTP Server\ftp-dr-1\users\eis_accounts\nyph2\FTP_111100409D45C8DD_1202100245_42.xml

11-1-2010 21:46:23 > Output directory: C:\Program Files\Ipswitch\WS_FTP Server\ftp-dr-1\users\eis_accounts\nyph2

11-1-2010 21:46:23 > Site name: nyph2

11-1-2010 21:46:23 >Successfully opened file: C:\Program Files\Ipswitch\WS_FTP Server\ftp-dr-1\users\eis_accounts\nyph2\FTP_111100409D45C8DD_1202100245_42.xml

11-1-2010 21:46:23 >Successfully created file: C:\Program Files\Ipswitch\WS_FTP Server\ftp-dr-1\users\eis_accounts\nyph2\NYPH-20101102-074500.ast

11-1-2010 21:46:24 > Error transferring C:\Program Files\Ipswitch\WS_FTP Server\ftp-dr-1\users\eis_accounts\nyph2\NYPH-20101102-074500.ast

Bad file descriptor


In reply to ftp->put file error by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.