CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
help with scp command

 



Mark1110
New User

Aug 19, 2011, 2:15 PM

Post #1 of 5 (1890 views)
help with scp command Can't Post

Hi,

I need to modify an scp command that is executed in a perl script. The current scp command is:

`scp /broker/user/csp/myTestFile.txt pushme\@bridge.myCompany.com:/secureftp/myCompany/compass/testDirectory/OUTBOUND/test`;

What I need to do is to add an date string to the file name. I wrote the following code:

$timeStamp = &TimeStamp;
$tranFileName = "myTestFile";

`scp /broker/user/csp/` .$tranFileName."_".$timeStamp.".txt". ` pushme\@bridge.myCompany.com:/secureftp/myCompany/compass/testDirectory/OUTBOUND/test`;

sub TimeStamp {

my ($sec,$min,$hour,$mday,$mon,$year,
$wday,$yday,$iddst) = localtime(time);
my $datestring;

$year += 1900;
$mon++;
$mon = insert0($mon);
$mday = insert0($mday);
$min = insert0($min);
$sec = insert0($sec);

$datestring = "$year$mon$mday";

return($datestring);

}


sub insert0 {
my ($date) = shift;

if ($date < 10) {
return "0$date";
}

return $date;
}


I would like to file transmitted to be myFileName_20110819.txt. Can I break up the scp command like this? If not, how would it be written.


Thanks,

Mark


FishMonger
Veteran / Moderator

Aug 19, 2011, 4:38 PM

Post #2 of 5 (1882 views)
Re: [Mark1110] help with scp command [In reply to] Can't Post

First, add the warnings and strict pragmas and declare your vars, which is done by prefixing them with the 'my' keyword. And don't use CamelCase naming. Use an _ underscore to separate the words.

Next, get rid of those 2 subs. They are a very messy way of formatting the date string. Use the strftime function from the posix module.

Don't use the backticks operator in void context. The backticks are used to capture the output of an external command, which you're not doing. If you don't want to capture the output of the scp command, then use the list form of the system function or one of the IPC::Open modules.


Code
use strict; 
use warnings;
use POSIX qw(strftime);

my $tran_filename = strftime("/broker/user/csp/myTestFile_%Y%m%d.txt", localtime);

my @cmd = qw($tran_filename pushme@bridge.myCompany.com:/secureftp/myCompany/compass/testDirectory/OUTBOUND/test);

system(@cmd) == 0 or die "system '@cmd' failed: $?;


In this case, I'd probably use the Net::SCP module instead of the system call.
http://search.cpan.org/~ivan/Net-SCP-0.08/SCP.pm


(This post was edited by FishMonger on Aug 19, 2011, 4:43 PM)


FishMonger
Veteran / Moderator

Aug 20, 2011, 6:26 AM

Post #3 of 5 (1866 views)
Re: [FishMonger] help with scp command [In reply to] Can't Post

While going over some of my thread postings, I noticed that I left out an important part of the system call. I didn't specify the scp command, I just specified its arguments.

Here's the corrected cmd.

Code
my @cmd = qw(scp $tran_filename pushme@bridge.myCompany.com:/secureftp/myCompany/compass/testDirectory/OUTBOUND/test);



Mark1110
New User

Aug 22, 2011, 9:39 AM

Post #4 of 5 (1822 views)
Re: [FishMonger] help with scp command [In reply to] Can't Post

Hi,

I tried inserting your code into my perl module and I am getting an error message on the system(@cmd) == 0 or die "system '@cmd' failed: $? statement. The message I am getting is failed: -1. I then tried putting the scp command in front of the $tran_filename and I got failed: 256. Do you know what I am doing wrong?

Thanks,

Mark


Mark1110
New User

Aug 22, 2011, 10:42 AM

Post #5 of 5 (1820 views)
Re: [FishMonger] help with scp command [In reply to] Can't Post

What I did to get it to work was the following:

my @cmd = "scp ".$tran_filename." ".
qw(pushme@bridge.myCompany.com:/secureftp/myCompany/compass/testDirectory/OUTBOUND/test);

What was happening was the qw function was deleting all the spaces so the @cmd was scp$tran_filenamepushme@bridge.myCompany.com:/secureftp/myCompany/compass/testDirectory/OUTBOUND/test

which was why I was getting an error. Thanks for all your help.

Mark

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives