【问题标题】:Display Output In Browser Perl CGI SSH在浏览器 Perl CGI SSH 中显示输出
【发布时间】:2012-08-05 18:11:04
【问题描述】:

我正在使用 Web 前端使用 Net::OpenSSH 执行远程命令。我的命令在命令行上没有失败地返回,但我在网络浏览器中什么也得不到。我做了几个小时的研究无济于事——有什么想法吗?

这里有一些代码给你一个例子(一些明显的原因被删除了)。

#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use Net::OpenSSH;
# Here in the code is just the header and standard tags
print "1";
print "2"; # both display
my $ssh = Net::OpenSSH->new($host, user => $uname, key_path => $key); # all works
$ssh- error and die "Can't ssh to host" . $ssh->error;
print "3";
$ssh->system("uname -a") or
die "remote command failed: " . $ssh->error;
my @lsa = $ssh->capture("ls -a");
$ssh->error and
die "remote ls command failed: ". $ssh->error;

print "4";
print "5"; 
print @lsa; # won't display in browser, just terminal/CLI

干杯!

【问题讨论】:

  • $ssh- error and die ?样本中的错字或更糟? :)
  • 另外,服务器错误日志说什么?
  • 服务器和操作系统是什么?

标签: perl ssh cgi openssh


【解决方案1】:

我维护 CGI.pm。我建议在您的简单脚本中添加这些内容:

  1. 在打印任何其他内容之前,请打印标准 HTTP 标头:print header();
  2. 在use CGI 行之后添加:use CGI::Carp qw(fatalsToBrowser); ...这将在浏览器中显示任何运行时问题。如果您在这些更改后没有得到任何输出,请检查脚本是否使用 perl -cw script.pl 编译

【讨论】:

    【解决方案2】:

    下面是关于在 Debian 机器上为我工作的最少 Perl 代码。我建议您仔细阅读它并将其与您的实际代码进行比较。

    但是,它在我的 Debian 上并不是开箱即用的,我做出了一些决定,其中大部分可能不是很安全,但这更多的是关于特定环境:

    • 为服务器运行可写的用户创建主页 (/var/www)
    • 预先将主机添加到 ~/.ssh/known_hosts
    • 使用strict_mode => 0 绕过Net::OpenSSH 的安全检查,而不是找到合适的 ctl_dir(Net::OpenSSH 要求文件夹及以上所有文件夹为 0755 或更严格, 所以我使用的 /tmp 通常不够好)

    我相信有比这更安全的做法,但正如我所说,这是特定于环境的。

    所以代码:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Net::OpenSSH;
    use File::Temp qw/ tempdir /;
    
    # necessary minimum for CGI
    print "Content-type: text/plain\n\n";
    
    # prepare temp dir
    my $temp = tempdir("/tmp/sshme.pl-XXXXXXXX", CLEANUP => 1);
    
    # open SSH session
    my %opts = (
        user        => "user",
        password    => "password",
        ctl_dir     => $temp,
        strict_mode => 0            ## NOT recommended - see my comments
    );
    my $ssh = Net::OpenSSH->new("host", %opts);
    $ssh->error
        and die "Couldn't establish SSH connection: ". $ssh->error;
    
    # perform command and print output
    my @lines = $ssh->capture("ls")
        or die "remote command failed: " . $ssh->error;
    print @lines;
    

    【讨论】:

      【解决方案3】:

      也许您的错误被定向到标准错误,而不是标准输出。在这种情况下,它们通常会出现在服务器日志中,而不是浏览器窗口中。也许您可以使用POSIX::dup2 来避免这种情况:

      use POSIX;
      
      # Make sure to send HTTP headers before redirecting streams!
      
      POSIX::close(2);  # close original stderr stream. No more output to apache logs!!!
      POSIX::dup2(1,2); # redirect error stream to standard output, so errors will appear in browser.
      

      perl 启动的进程,例如一些 ssh 二进制文件,将继承这些流。

      【讨论】:

        猜你喜欢
        • 2012-11-30
        • 2011-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-18
        • 1970-01-01
        • 1970-01-01
        • 2014-01-07
        相关资源
        最近更新 更多