I am having a problem executing a batch file on a remote windows machine running openssh from a linux host. I am using the following test code which works except it never actually executes the batch file after copying the file over to the remote host.
Hopefully someone more learned in the ways of the force can help me see the light here.
#!/usr/bin/perl -w
use strict;
use Net::SFTP;
use Net::SSH::Perl;
my %credentials = ( user => 'admin', password => 'admin.password');
my @hosts = ('hosta', 'hostb');
my $localfile = 'test.bat';
my $remotefile = 'remote_testfile';
my $local_dir = '/home/admin';
my $remote_dir = 'c:\Documents and Settings\admin';
my $sftp;
my $ssh;
foreach (@hosts) {
$sftp = Net::SFTP->new($_, %credentials) || die;
$sftp->put($localfile, "$remote_dir\\$localfile");
$sftp->get("$remote_dir\\$remotefile","$_.$remotefile");
undef $sftp;
$ssh = Net::SSH::Perl->new($_) || die;
$ssh->login($credentials{'user'}, $credentials{'password'});
my ($out, $err, $exit) = $ssh->cmd("$remote_dir\\$localfile");
undef $ssh;
if ($out) { print "$out\n"; }
if ($err) { print "Error: $err\n"; }
print "Exit code: $exit\n";
}
The batch file contents are equally simple for testing purposes.
$ cat test.bat
echo Executing the removal of remote_testfile
del remote_testfile
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.