Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如何编写CGI脚本以通过ssh执行远程脚本并显示远程执行的数据?_Perl_Apache_Ssh_Cgi_Ssh Keys - Fatal编程技术网

Perl 如何编写CGI脚本以通过ssh执行远程脚本并显示远程执行的数据?

Perl 如何编写CGI脚本以通过ssh执行远程脚本并显示远程执行的数据?,perl,apache,ssh,cgi,ssh-keys,Perl,Apache,Ssh,Cgi,Ssh Keys,我有一个特殊的例子,我必须将HTML页面和CGI脚本放在ServerA上,CGI脚本必须调用ServerB上的Perl脚本,它将从DB获取数据,ServerA上的HTML页面将显示数据 我成功地远程触发了perl脚本,但我无法决定最好的方法是如何将数据从ServerB返回到ServerA,并使用此体系结构显示它 我认为: ServerB将数据导出到一个平面文件,该平面文件使用scp传输回ServerA。可以在serverB上的perl文件中配置scp 问题1:如何让服务器A上的CGI等待服务器B

我有一个特殊的例子,我必须将HTML页面和CGI脚本放在ServerA上,CGI脚本必须调用ServerB上的Perl脚本,它将从DB获取数据,ServerA上的HTML页面将显示数据

我成功地远程触发了perl脚本,但我无法决定最好的方法是如何将数据从ServerB返回到ServerA,并使用此体系结构显示它

我认为: ServerB将数据导出到一个平面文件,该平面文件使用scp传输回ServerA。可以在serverB上的perl文件中配置scp

问题1:如何让服务器A上的CGI等待服务器B返回数据。我如何处理数据未返回或传递到ServerB ae的输入参数不正确的错误情况


问题2:是否有更好的方法围绕这一要求进行规划

我还没有测试过这个。为了添加注释,我从现有的一些代码中删掉了它

只要稍加修改,它就可以工作了

我希望这足以让你开始

use Net::SSH::Perl;
use Net::SCP qw(scp iscp);

$DEBUG = 0;

# Note: A public key exchange must first be created before this script will work
# without a password

$remHost = "machine.domain.sfx";
$remUser = "user_on_remote_machine";

# Name of file to send
$sndFile = "localfile.txt";

# scp is used to send the file to the remote host 
$scp = Net::SCP->new( $remHost, $remUser );
$scp->put( $sndFile ) or die $scp->{errstr};

$sndFile =~ s/.*\/(.*)/$1/;

# Use ssh to run a command remotely
$ssh = Net::SSH::Perl->new( $remHost );
eval{ $ssh->login( $remUser, "" ); }
    or $msg = "Cannot connect to $remHost - Net::SSH error: ".join( "", $@ );

# Copy the file to another directory on the remote host
$cmd = "cp $sndFile /opt/tmp";

if( $DEBUG > 0 )
    { print STDERR "cmd = $cmd\n"; }

# Execute the command
eval{ ($stdout, $stderr, $exit) = $ssh->cmd($cmd); };

if( $DEBUG > 0 )
{
    print STDERR "stderr = $stderr\n";
    if( $DEBUG > 1 )
        {   print STDERR "stdout = $stdout\n"; }
}

# Store the output of the ssh command in an array
@remArr = split( "\n", $stdout );

我还没有测试过这个。为了添加注释,我从现有的一些代码中删掉了它

只要稍加修改,它就可以工作了

我希望这足以让你开始

use Net::SSH::Perl;
use Net::SCP qw(scp iscp);

$DEBUG = 0;

# Note: A public key exchange must first be created before this script will work
# without a password

$remHost = "machine.domain.sfx";
$remUser = "user_on_remote_machine";

# Name of file to send
$sndFile = "localfile.txt";

# scp is used to send the file to the remote host 
$scp = Net::SCP->new( $remHost, $remUser );
$scp->put( $sndFile ) or die $scp->{errstr};

$sndFile =~ s/.*\/(.*)/$1/;

# Use ssh to run a command remotely
$ssh = Net::SSH::Perl->new( $remHost );
eval{ $ssh->login( $remUser, "" ); }
    or $msg = "Cannot connect to $remHost - Net::SSH error: ".join( "", $@ );

# Copy the file to another directory on the remote host
$cmd = "cp $sndFile /opt/tmp";

if( $DEBUG > 0 )
    { print STDERR "cmd = $cmd\n"; }

# Execute the command
eval{ ($stdout, $stderr, $exit) = $ssh->cmd($cmd); };

if( $DEBUG > 0 )
{
    print STDERR "stderr = $stderr\n";
    if( $DEBUG > 1 )
        {   print STDERR "stdout = $stdout\n"; }
}

# Store the output of the ssh command in an array
@remArr = split( "\n", $stdout );

您的脚本应该启动另一个进程并退出。另一个进程应将输出保留在文件中。您需要为用户设置一种方式,以便在其他流程结束时查看结果


如需灵感,请参阅Randal Schwartz的。现在,您可以使用。

您的脚本应该启动另一个进程并退出。另一个进程应将输出保留在文件中。您需要为用户设置一种方式,以便在其他流程结束时查看结果


如需灵感,请参阅Randal Schwartz的。现在,您可以使用。

我假设您正在分叉远程SSH命令,因此对于等待远程命令完成会产生混淆。以下内容符合您的要求:

open(my $F, "-|", "ssh $REMOTE_HOST $REMOTE_COMMAND") || die $!;

while(<$F>) {
  do_something_with_data($_);
}
open(my$F,“-”,“ssh$REMOTE\u HOST$REMOTE\u COMMAND”)| | die$!;
while(){
用数据($)做某事;
}

我假设您正在分叉远程SSH命令,因此对于等待远程命令完成会产生混淆。以下内容符合您的要求:

open(my $F, "-|", "ssh $REMOTE_HOST $REMOTE_COMMAND") || die $!;

while(<$F>) {
  do_something_with_data($_);
}
open(my$F,“-”,“ssh$REMOTE\u HOST$REMOTE\u COMMAND”)| | die$!;
while(){
用数据($)做某事;
}

就我个人而言,我很想从ServerA使用ServerB上的代理,并“直接”获取数据——允许您在CGI脚本中操作数据。当然,这是假设您需要的数据返回得足够快,不会超时。

就我个人而言,我很想从ServerA使用ServerB上的代理,并“直接”获取数据-允许您在CGI脚本中对其进行操作。当然,这是假设您需要的数据返回得足够快,不会超时。

您是否考虑过CGI脚本的web服务器超时?看起来我可能太急于回答-我没有完全阅读您的问题。我将把这个留在这里以防万一。谢谢大家的回复。我发现服务器B上的Perl在服务器A的CGI使用远程ssh命令调用流后将所有内容输出到流中-我不需要在文件中使用该输出,但操作按预期运行,我首先获得了我想要的所有Perl输出。您考虑过CGI脚本的web服务器超时吗?看起来我我可能太急于回答了——我没有完全阅读你的问题。我将把这个留在这里以防万一。谢谢大家的回复。我发现服务器B上的Perl在服务器A的CGI使用远程ssh命令调用流之后将所有内容输出到流中——我不需要在文件中使用该输出,但操作按预期运行,我首先获得了我想要的所有Perl输出。