Broke Perl Script Second pair of eyes NET::FTPSSL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Broke Perl Script Second pair of eyes NET::FTPSSL
# 1  
Old 01-12-2009
Question Broke Perl Script Second pair of eyes NET::FTPSSL

Hi all,

Let me first start out by saying I'm a perl newbie and hope somebody can help, for the life of me I can't figure out why my script will not find and download a remote file via FTPSSL. What it's supposed to do is find the latest file named simple-test-case_("dd-MM-yyyy-hh-mm-ss").csv i.e.
Quote:
simple-test-case_09-01-2009-08-17-51.csv
and download it. But when I run it (no error or warnings) it acts like it does not find the file and hence not download it. A second pair of eyes and guidance would be much appreciated. I personally feel it's my regex.

Code:
use strict;
use warnings;
use Net::FTPSSL;
use Getopt::Long qw(:config no_ignore_case);


my $hostname = '127.0.0.1';
my $username = 'testuser';
my $password = 'testpass';
my $port = '21';
my $directory = '/';
my $verbose = '1';
my $passive = '0';
my $timeout = "30";
my $version = "0.0.2";
my $input_dir = "input";
my $output_dir = "output";
my $input_trg_file = "simple-test-case.csv";
my $input_csv_file = "simple-test-case.trg";
#Bellow var will be used later
my $output_file = "simple-test-case*.csv";
#
my $encryption = "E";

# creating connection and starting the test

my $ftps = Net::FTPSSL->new($hostname, Debug => $verbose, Port => $port, Encryption => $encryption ) or die "ERROR: Cannot conect to $hostname\n";

if (!$ftps->login("$username","$password")) { 
  print "ERROR: Sever says: ", $ftps->last_message;
  exit 2;
}
if ($input_trg_file eq "") {
  if (!$ftps->list("$directory")) { 
    print "WARNING: server says: " , $ftps->last_message;
    exit 1;
  } else {
    print $ftps->last_message;
  }
} else {
    if (!$ftps->cwd("/$input_dir")) {
      print "WARNING: server says: " , $ftps->last_message;
    exit 1;
  } else {
      if (!$ftps->put("$input_trg_file","/$input_dir/$input_trg_file")) {
        print "WARNING: server says: " , $ftps->last_message;
        exit 1;  
    } else {
      if (!$ftps->put("$input_csv_file","/$input_dir/$input_csv_file")) {
        print "WARNING: server says: " , $ftps->last_message;
        exit 1;
      } else {
        if (!$ftps->cwd("/$output_dir")) {
          print "WARNING: server says: " , $ftps->last_message;
          exit 1;
          } else {
          sleep(10);
          my @lines = grep { /^simple-test-case./i } $ftps->list();
          foreach my $name (@lines) { 
          if (!$ftps->get("$name, /tmp/$name")) {
              print "WARNING: server says: " , $ftps->last_message;
            } else {
              my @message = $ftps->last_message;
              chomp @message;
              print "OK: ", "$message[0] $message[1]\n";
              exit 0;
             }
           }
         }
       }
     }
   }
 } 

$ftps->quit;

Thanks in advance,
Eric
# 2  
Old 01-12-2009
I would always recommend running the script with the Perl debugger and step through the code line by line to see where the script is going wrong, and interrogate variables before they're used. Change line one of the script to something like
Quote:
#! /usr/bin/perl -d
or run the script as
Quote:
perl -d <script>
Quick tutorial...
Quote:
p <variable> - display a value
x <variable> - display a hash or array
n - next (will step over a subroutine)
s - step (will step into a subroutine)
R - rerun the script from the top
b <line|subroutine> - add a breakpoint
c - continue to the next breakpoint
Any valid Perl command can be entered as well
First time through, just 'n' through the program and see if it follows the path you'd expect. If not, add a breakpoint or two and rerun to the breakpoint.

You have plenty of trace code and you say you're getting no output which tends to point to the $ftps->list or grep. In debug you can enter
Quote:
x $ftps->list()
to see what that returns and if that's OK try
Quote:
x grep { /^simple-test-case./i } $ftps->list()
HTH

Jerry
# 3  
Old 01-12-2009
Hi Jerry,

Thanks for the quick reply! Smilie Well stepping through like you suggested really now backs my suspicion the the regex is borked. I put 2 test files all named the same except the extension. i.e.

simple-test-case_12-01-2009-07-27-51.csv

and

simple-test-case_12-01-2009-07-27-51.err

I should of siad in my first post instead of getting no output, I'm not seeing the output as expected, here is the step through sequence, and you can see
Code:
my @lines = grep { /^simple-test-case./i } $ftps->list();

Steps through 4 times not finding the requested file and not downloading then exiting.

Code:
  DB<2> n
>>> CWD /output
<<< 250 Directory successfully changed.
main::(connect-monitor-draft-new.pl-09:98):
98:              sleep(10);
  DB<2> n
main::(connect-monitor-draft-new.pl-09:99):
99:              my @lines = grep { /^simple-test-case./i } $ftps->list();
  DB<2> n
>>> PBSZ 0
<<< 200 PBSZ set to 0.
>>> PROT P
<<< 200 PROT now Private.
>>> PASV
<<< 227 Entering Passive Mode 
>>> LIST
<<< 150 Here comes the directory listing.
<<< 226 Directory send OK.
main::(connect-monitor-draft-new.pl-09:99):
99:              my @lines = grep { /^simple-test-case./i } $ftps->list();
  DB<2> n
main::(connect-monitor-draft-new.pl-09:99):
99:              my @lines = grep { /^simple-test-case./i } $ftps->list();
  DB<2> n
main::(connect-monitor-draft-new.pl-09:99):
99:              my @lines = grep { /^simple-test-case./i } $ftps->list();
  DB<2> n
main::(connect-monitor-draft-new.pl-09:99):
99:              my @lines = grep { /^simple-test-case./i } $ftps->list();
  DB<2> n
main::(connect-monitor-draft-new.pl-09:100):
100:              foreach my $name (@lines) { 
  DB<2> n
main::(connect-monitor-draft-new.pl-09:117):
117:    $ftps->quit;
  DB<2> n
>>> QUIT
<<< 221 Goodbye.
Debugged program terminated.  Use q to quit or R to restart,

Smilie

Cheers,
Eric

Last edited by Styles; 01-12-2009 at 08:55 PM..
# 4  
Old 01-12-2009
Your reply suggests to me that ftps->list found 4 files as line 99 was repeated 4 times.

Stop when you get to that line and enter
Quote:
x $ftps->list()
You have a '^' in the regex...is this breaking it?
Try...
Quote:
x grep { /simple/ } $ftps->list();
to see what that gives.

Just thought...Smilie - why the braces {}?
How about...
Quote:
x grep /simple/ $ftps->list();
Just play in the debugger without moving on in the script.

Jerry
# 5  
Old 01-12-2009
Hey Jerry,

Got it working, it was my regex. Funny thing is it's ab;le to download all the files, and it does place them in my tmp dir, but the server is returning a 500 error
Code:
 WARNING: server says: 550 Failed to open file.

Were is that coming from? Strange ? Also the } brackets are needed, with out them it bombs out.

Code:
 DB<1> n
>>> PBSZ 0
<<< 200 PBSZ set to 0.
>>> PROT P
<<< 200 PROT now Private.
>>> PASV
<<< 227 Entering Passive Mode 
>>> RETR -rw-rw-r--    1 25020    26089           0 Jan 13 00:27 simple-test-case_12-01-2009-07-27-51.csv
<<< 550 Failed to open file.
main::(connect-monitor-draft-new.pl-09:102):
102:	              print "WARNING: server says: " , $ftps->last_message;
  DB<1> n
main::(connect-monitor-draft-new.pl-09:101):
101:	           if (!$ftps->get($name, "/tmp/$name")) {

Cheers,
Eric
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Net::IP not working

Experts - I have a snippet of code I can't figure out. I was hoping someone could help me here. I have a file of IPv6 address that I need to format correct. Example in: 2620:0:2d0:200::7 2620:0:2d0:200:a0:c 2620:0a:3f0:200:a0:c I need to convert them to:... (3 Replies)
Discussion started by: timj123
3 Replies

2. Shell Programming and Scripting

Bash script to create rsa key pair

Hello all, I am setting up a cluster of Mac Pro's which need to be able to talk to a master computer, traffic between the nodes and the master needs to take place without a ssh key. I need a script that will create a security key, save it to the default place, enter the password as no password.... (2 Replies)
Discussion started by: sdl27789
2 Replies

3. Shell Programming and Scripting

Unable to get the size of remote file using Net::FTP Perl Script

Hi, I am using below piece of code to get the size of the remote file. $ftp->cwd($destination) or $error=$ftp->message; if(!$error) { $ftp->put($file) or $error=$ftp->message; print "FTP size = \n"; ... (3 Replies)
Discussion started by: FarooqOnline
3 Replies

4. Shell Programming and Scripting

Have a find/replace perl script thats broke

Hello Folks, #!/usr/bin/perl use File::Find; open F,shift or die $!; my %ip=map/(\S+)\s+(\S+)/,<F>; close F; find sub{ if( -f ){ local @ARGV=($_); local $^I=""; while( <> ){ !/#/ && s/(\w+)\.fs\.rich\.us/$ip{$1}/g; print; } }... (8 Replies)
Discussion started by: richsark
8 Replies

5. Windows & DOS: Issues & Discussions

Running perl script from a VB.NET windows service

Here is the snippet of the code that I'm trying to execute. Stat of the service does not launch perl script. OnStop works fine. Please could you help here. Public Class Service1 Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This... (0 Replies)
Discussion started by: hansini
0 Replies

6. Shell Programming and Scripting

Would appreciate a quick second set of eyes on a script (regarding doing things in the background)

What I'm trying to do is leave a tcpdump running all the time on a server, and at the end of every day kill it and start a new one. For some reason my boss doesn't want to set this up as a cron job, so I've come up with the following.: #!/bin/bash PCAPFILE=/tmp/mgmt.$(date... (8 Replies)
Discussion started by: DeCoTwc
8 Replies

7. Shell Programming and Scripting

Net::Ftp in perl

I am trying to execute a script in another server, i used Net::Ftp module How to execute unix command in another server by using Net::Ftp module.. #!/usr/bin.perl ### Perl script to $ftp->login($user_name,'password') or die "Cannot login ", $ftp->message;... (2 Replies)
Discussion started by: pritish.sas
2 Replies

8. Shell Programming and Scripting

Net::SSLeay or Net::FTPSSL

Hello, I ran into an issue in one of my monitoring scripts. If I use the public ip address in my connection string everything works, but if I switch the connection string ip to 127.0.0.1 or the internal ip I get, " Connection refused at... (1 Reply)
Discussion started by: Styles
1 Replies

9. Shell Programming and Scripting

Need some help with this script -- extra eyes

I have two issues with this script. 1. I cannot seem to get my counters to count correctly. 2. My function to eject to CAP1 or CAP2 is hung in a loop and doens't exit back to the previous function. I would like to be able to select which cap to eject to . Each cap holds only 40 tapes, so when one... (15 Replies)
Discussion started by: gzs553
15 Replies

10. Shell Programming and Scripting

Best place on the net to get Perl tutorials ?

I am looking for a full Perl course on internet (tutorial) to learn everything about Perl. I already know a bunch of programing languages. I have started Unix interests this year ever since I installed Ubuntu Linux on my PC AND I also got a job at IBM doing software distributions with... (1 Reply)
Discussion started by: Browser_ice
1 Replies
Login or Register to Ask a Question