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?
 
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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
 
sduser1Author Commented:
Using Net::SSH2 module and the above code to connect to the remote server using shell commands worked good.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.