如何在具有本地Perl程序的远程计算机上运行shell脚本?

时间:2009-08-31 21:05:59

标签: perl

我正在编写一个Perl脚本,该脚本将在与运行Linux的计算机联网的PC上运行。该脚本必须为Linux机器上的一组shell脚本提供输入和接收结果。我能够从PC复制输入文件甚至在Linux机器上调用Perl脚本,但是当该脚本试图通过以下方式运行.sh文件时:

 system("./shell_script.sh");

它会输出以下错误:

 '.' is not recognized as an internal or external command, operable program or batch file.

我认为它意味着它试图在Windows下执行.sh文件。那么有没有办法让PC告诉Linux盒子,“嘿,伙计,自己运行吗?”

谢谢。

2 个答案:

答案 0 :(得分:6)

您可以使用Net::SSH::Perl在远程计算机上运行命令/脚本:

use warnings;
use strict;

use Net::SSH::Perl;

my $command = "command";
my $host = "host";
my $user = "username";
my $pass = "password";

my $ssh = Net::SSH::Perl->new( $host );
$ssh->login( $username, $pass );
my($stdout, $stderr, $exit) = $ssh->cmd( $command );

print $stdout;

答案 1 :(得分:5)

我认为您想使用Net::SSHExpect之类的东西登录Linux机器并运行shell脚本。 system()在本地计算机上运行;它不是一种向远程机器发送命令的方法。