Passing shell scripts to run on the SFTP cmd object

i am not sure if Net::SFTP::Foreign is the correct module to use when running shell scripts on the remote machine. Please advise.

I get the below error.

Can't locate object method "cmd" via package "Net::SFTP::Foreign" at C:/eclipse/Perl Automation/SFTPServerConnect.pl line 21.


#!/usr/bin/perl -w
use warnings;
use strict;
use Net::SFTP::Foreign;
use Data::Dumper;
$ENV{HOME} = $ENV{USERPROFILE};
use Net::SSH::Perl;
my $host = 'lmmk.com';
my $user = "user";
my $passwd = "passwd";


my $sftp = Net::SFTP::Foreign->new($host,
                                   backend => 'Net_SSH2',
                                   user => $user,
                                   password =>$passwd);
$sftp->die_on_error("Unable to establish SFTP connection");

$sftp->setcwd('/fimsdev/SIT/') or die "unable to change cwd: " . $sftp->error;

$sftp->cmd('extractMetlifeTarFiles.ksh >> /logs/extractMetlifeTarFiles.ksh.log 2>&1');
sduser1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
Net::SFTP::Foreign doesn't have a cmd method but it does have an ssh_cmd method.

Change
$sftp->cmd('extractMetlifeTarFiles.ksh >> /logs/extractMetlifeTarFiles.ksh.log 2>&1'); 

Open in new window

To
$sftp->ssh_cmd('extractMetlifeTarFiles.ksh >> /logs/extractMetlifeTarFiles.ksh.log 2>&1'); 

Open in new window


However, that may not (probably won't) solve the problem because I think it's used to specify the name of the ssh client that is to be used.

I'm not on a system where I could run any tests.
0
sduser1Author Commented:
i tried the above.

I am getting the same error.

Can't locate object method "ssh_cmd" via package "Net::SFTP::Foreign"
0
FishMongerCommented:
When you need to run remote shell commands, you should probably do that with the Net::SSH2 module or the Net::OpenSSH module which is written by the same person that wrote Net::SFTP::Foreign.
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

sduser1Author Commented:
Thank you. I installed Net::OpenSSH and tried to use the below code.  I did not get any errors but the shell script is not being executed on the remote server. Can you see if anything is wrong?

#!/usr/bin/perl -w
use warnings;
use strict;
use Net::ssh2;
use Net::OpenSSH;
use Data::Dumper;
$ENV{HOME} = $ENV{USERPROFILE};

my $host = 'lmmk.fmr.com';
my $user = "user";
my $passwd = "password";


my $cmd =('/fimsdev/fimsconv/metlifeConversion/SIT/bin/extractMetlifeTarFiles.ksh >> /fimsdev/fimsconv/metlifeConversion/SIT/logs/extractMetlifeTarFiles.ksh.log 2>&1');
my $ssh = Net::OpenSSH->new($host);
$ssh = Net::OpenSSH->new(host=>$host, user=>$user,password=>$passwd);

$ssh->error and die "unable to connect to remote host: ". $ssh->error;

$ssh->system($cmd);

Thank you.
0
FishMongerCommented:
Try changing:
$ssh->system($cmd);

Open in new window

To:
$ssh->system($cmd) or die "remote command failed: " . $ssh->error;

Open in new window

0
sduser1Author Commented:
Tried the above. I did not get the error nor the command got executed.

Below is the one I tried


$ssh->system($cmd) or die "remote command failed: " . $ssh->error;

$ssh->system('ls -R /') or die "ls failed: " . $ssh->error;
0
FishMongerCommented:
I'm on vacation this week so I can't do any testing.

Enable debugging to see if that gives you any clues to the problem.
0
sduser1Author Commented:
I enabled debugging but did not see anything. Was wondering if this module works on windows.
0
FishMongerCommented:
How did you install the module and did you receive any errors?
0
sduser1Author Commented:
Sorry for the delay as I was out of office.

I did not install the  module.  I used the below debug in the arguments. Do I need to install the module for debug? let me know.

$ssh = Net::OpenSSH->new(host=>$host,user=>$user,password=>$passwd,debug=>1);
0
FishMongerCommented:
Try this:
#!/usr/bin/perl

use warnings;
use strict;
use Net::SSH2;

my $host   = 'lmmk.fmr.com';
my $user   = "user";
my $passwd = "password";
my $cmd    = '/fimsdev/fimsconv/metlifeConversion/SIT/bin/extractMetlifeTarFiles.ksh >> /fimsdev/fimsconv/metlifeConversion/SIT/logs/extractMetlifeTarFiles.ksh.log 2>&1';
my $ssh    = Net::SSH2->new();

$ssh->connect($host) or die $!;

if ($ssh->auth_password($user, $passwd)) {
    my $chan = $ssh->channel();
    $chan->exec($cmd) or die $!;
}
else {
    die "Failed to login to $host";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sduser1Author Commented:
Using Net::SSH2 module and the above code to connect to the remote server using shell commands worked good.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.