Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

ActivePerl - perl.exe works, wperl.exe does not...

by spstansbury (Monk)
on Jan 25, 2012 at 02:39 UTC ( #949805=perlquestion: print w/replies, xml ) Need Help??
spstansbury has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks.

I'm hoping someone with more ActivePerl experience that I can shed some light on the behavior that I'm seeing.

I have a script ( below ) that copies some Nessus scans to another machine for further processing.

When I run the script via perl.exe ( >perl copy_files.wpl ) it runs perfectly.

When I run it via wperl.exe, either on the command line ( >wperl copy_files.wpl ) or via Windows Task Scheduler, it fails with the error: Unable to connect to remote host: Bad ssh command: Invalid argument at C:\Users\Administrator\RiskView\copy_files.wpl line 76.

I believe I have all the required tweaks for wperl - suppressing the window, redirecting STDERR and STDOUT to files, but as wperl.exe is supposed to be identical to perl.exe except for the need to launch a window, I'm at a loss to explain why it won't run the Net::SFTP::Foreign module without error.

As always, thanks for any input!

Best regards, Scott...


use strict; use warnings; use Date::Manip; use Archive::Zip; use Net::SFTP::Foreign; use Data::Dumper; BEGIN { Win32::SetChildShowWindow(0) if defined &Win32::SetChildShowWindow +; } my $producer = "file_copy"; my $output_log = $producer . "_output_log.log"; my $error_log = $producer . "_error_log.log"; my $copy_log = $producer . "_log.log"; unlink( $error_log ); unlink( $output_log ); close(STDOUT); open(STDOUT, ">>$output_log"); close(STDERR); open(STDERR, ">>$error_log" ); # Nessus directory my $nessus_dir = '/opt/sc4/orgs/1/VDB'; my $incoming_dir = 'C:\\Users\\Administrator\\Incoming'; # Data file directory my $data_dir = 'C:\\Users\\Administrator\\Data'; my $host = ''; my $user = 'user'; my $password = 'password'; # Check the copylog for the last set of files loaded my $last_loaded_date; open ( LOG_DATA, "<$copy_log"); while ( my $line = <LOG_DATA>) { chomp $line; next unless( $line =~ /Last Loaded/ ); my @last_loaded_time_line = split( /:/, $line ); $last_loaded_date = $last_loaded_time_line[1]; last; } close ( LOG_DATA); print "Getting files more recent than $last_loaded_date\n"; my $dm1 = new Date::Manip::Date; my $dm2 = $dm1->new_date; my $now_date = $dm1->new_date; my $time_stamp = time; my $err = $now_date->parse( "epoch $time_stamp" ); my $current_date = $now_date->printf( "%Y-%m-%d %H:%M:%S" ); $last_loaded_date =~ s/^\s+//; $last_loaded_date =~ s/\s+$//; $dm1->parse( $last_loaded_date ); my $sftp = Net::SFTP::Foreign->new( $host, user => $user, password => +$password, ssh_cmd => 'plink' ); $sftp->error and die "Unable to connect to remote host: " . $sftp->err +or; # Get the files chdir $incoming_dir; my @ls1 = @{ $sftp->ls( $nessus_dir, names_only => 1, ordered => 1 ) } +; my $last_loaded; foreach my $dir_date ( @ls1 ) { next unless $dir_date =~ /\d{4}-\d{2}-\d{2}/; $dm2->parse( $dir_date ); if ( $dm1->cmp( $dm2 ) == -1 ) { $sftp->mget( "$nessus_dir/$dir_date/*" ) or +die "Unable to transfer files: " . $sftp->error; $last_loaded = $dir_date; print "Getting files from directory $dir_date\n"; } } undef $sftp; open( LOG,">../$copy_log ") or die $!; print LOG "Copied Nessus reports from SecurityCenter\n"; print LOG "Last copy run on: $current_date\n"; print LOG "Last Loaded (directory name): $last_loaded\n"; close( LOG );

Replies are listed 'Best First'.
Re: ActivePerl - perl.exe works, wperl.exe does not...
by chessgui (Scribe) on Jan 25, 2012 at 03:04 UTC
    I've had a similar problem with wperl (I think it has nothing to do with ActiveState - it fails on Strawberry as well). In my particular case it was an Open2 call that hanged wperl.

    The workaround was to create a script which does nothing else but launches perl.exe without a window through create process with creation flag 'CREATE_NO_WINDOW'. You run this script via wperl.exe instead of your original prog.

    Mine is a Win32 solution: I don't know whether any similar thing works on Unix/Linux or not.

    The launching script:

    se Win32::Process; Win32::Process::Create($perl,'c:\strawberry\perl\bin\perl.exe','perl.e +xe',0,CREATE_NO_WINDOW,'.');

    The only modification to the original program itself that you include the following lines:

    use Win32 qw(SW_HIDE); Win32::SetChildShowWindow(SW_HIDE);

    From here on no window is created (not even for child processes opened with Open2) and the main program is safe since it is run via perl.exe.

      Thanks for the resonse!

      I'll give this a try. Scott...

Re: ActivePerl - perl.exe works, wperl.exe does not...
by Anonymous Monk on Jan 25, 2012 at 03:28 UTC

    Add this at the top of your program

    BEGIN { use Win32 qw(SW_HIDE); Win32::SetChildShowWindow(SW_HIDE); my $perl = $^X; if( $perl =~ s/wperl.exe/perl.exe/i ){ system(1, $perl, $0, @ARGV ); exit; } }

      This works...

      Given that I can run perl.exe with no child window, I can scrap wperl.exe... ;)

      Clearly there's some other difference between the two binaries - I have other wperl scripts that run fine, it was just theNet::SFTP::Foreign line:

      my $sftp = Net::SFTP::Foreign->new( $host, user => $user, password => $password, ssh_cmd => 'plink' );

      that wperl choked on that perl.exe handled without a problem...

      Thanks much for your help! Scott...

        The difference is STDIN/STDOUT/STDERR/

        wperl.exe, being a windows GUI application, doesn't have these

        perl.exe, being a windows console (cmd.exe) application, does have these, but it also has a console window

        So you start wperl because it has no console window, make sure the children get hidden, then launch a hidden perl.exe , because it has a console and STDIN/STDOUT/STDERR

        Apparently Net::SFTP ... needs a STDIN/STDOUT/STDERR for pipes and such

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://949805]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2017-08-23 03:01 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (345 votes). Check out past polls.