Perl CGI,使用 system() 启动 ssh 并传递清理后的变量

Perl CGI,使用 system() 启动 ssh 并传递清理后的变量

我正在寻找一种方法让 perl 启动一个程序,其中的变量取自上一个网页。这是我可怕的 Perl 代码:

#!/usr/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
my $guy = param('number');
$guy =~ s/\D//g;
if ($guy == 0){
    die("Please enter number for employee");
}
my $sides = param('clock');
my $finger =  param('finger');

if ($sides == "left"){
system($finger, $guy |  "sudo ssh [email protected]: fprintd-enroll -f");
exit 0;
}
 if ($sides == "right"){
 system($finger, $guy |  "sudo ssh [email protected]: fprintd-enroll -f");
exit 0;
}

但我不断收到如下错误:“读取 CGI 回复时发生错误(未收到回复)”

有没有更好的方法在两个 Debian 盒子之间做这种事情?有没有办法让“ssh”方式工作?

谢谢!

编辑: 根据下面的建议,我在过去一周更改了代码,但我仍然没有完全做到这一点。

#!/usr/bin/perl -w                                                              
use CGI qw(:standard -debug);                                                 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);                          
use Net::SSH2;                                                                
use strict;                                                                    
my $ssh = Net::SSH2->new();                                                    
my $q = new CGI;                                                              
my $style = get_style();                                                                                                                                  
my $guy = param('number');                                                    
$guy =~ s/\D//g;                                                               
if ($guy == 0){                                                                
    die("Please enter number for employee, broken");                     
}
my $sides = param('clock');
my $finger =  param('finger');

print $q->start_html(
-title => "Enroll Finger Print".
-style => {-code => $style},
);
print $q->h2("Enrolling Finger...");
if ($sides eq "left"){
    $ssh->connect("172.16.4.10") or $ssh->die_with_error;
    $ssh->auth_publickey("root", "../files/id_rsa.pub", "../files/id_rsa") or $ssh->die_with_error;
    my $chan = $ssh->channel;
    $chan->exec("fprintd-enroll  -f ${finger} ${guy}");
    $ssh->disconnect();
    print $q->h2("Sent request for ", $guy,"'s ", $finger);
    print end_html;
    exit 0;
}

if ($sides eq "right"){
    $ssh->connect("172.16.4.11") or $ssh->die_with_error;
    $ssh->auth_publickey("root", "../files/id_rsa.pub", "../files/id_rsa") or $ssh->die_with_error;
    my $chan = $ssh->channel;
    $chan->exec("fprintd-enroll -f ${finger} ${guy}");
    $ssh->disconnect();
    print "Sent request for ", $guy,"'s ", $finger;
    print end_html;
    exit 0;
}
else{
print $q->h4("Timeclock not selected\n");
print $q->h4("Please select a timeclock to use for finger enrollment.");
exit 1;
}

## Subs

sub get_style {
my $style = <<"EOT";
body {
font-family: verdana, arial, sans-serif;
bgcolor: white;
padding-left: 5%;
}
h2 {
colors: purple;
border-bottom: 1pt solid;
}
h4 {
color: blue;
}
EOT
return $style;
}

答案1

我创建了一个快速 HTML 表单来执行以下代码。虽然不是完全重写;和绝对不完整,它确实有效。请注意print $q->header(...)在行之前添加的行print $q->start_html(...)。如果我要完成清理工作,我将删除对的调用die并替换为类似于对错误号码输入所做的操作。在某些情况下,您可能希望针对失败的 ssh 连接/登录发送 HTML 服务器错误响应 (5xx),而不是调用 die。重复的多行代码部分可能应该放入一个函数中。我也没有生成更清晰的 HTML 输出,因为时钟不存在左边或者正确的,就像我说的,这不是我认为的完整程序。编写 CGI 时,您需要注意这样一个事实:输出实际上不是终端或控制台。您应该捕获几乎所有可能的失败(登录、连接、查询、命令输出等)并相应地生成 HTML(并且可能刷新标头以返回到原始表单或另一个网页)。

#!/usr/bin/perl -w                                                              
#
# vi: set ft=perl syntax=perl noai ts=4 sw=4:       
use CGI qw(:standard -debug);                                                 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);                          
use Net::SSH2;                                                                
use strict;                                                                    
my $ssh2 = Net::SSH2->new();                                                    
my $q = new CGI;                                                              
my $formurl = "http://webhost.example.com/testform.html";
my $lefthost = "leftclock.example.com";
my $righthost = "rightclock.example.com";
my $publickey = "/home/user/.ssh/id_rsa2.pub";
my $privatekey = "/home/user/.ssh/id_rsa2";
my $username = "user";

my $style = get_style();                                                                                                                                  
my $guy = $q->param('number');                                                    
$guy =~ s/\D//g;                                                               
if ($guy == 0){                                                                
#    die("Please enter number for employee, broken");                     
    print $q->header( -type => "text/html", -Refresh=>"5; URL=$formurl" );
    print $q->start_html( -title => "Bad Number" );
    print $q->h2("Missing Employee Number");
    print "Please return to the the form and enter employee number<BR>\n";
    print "If you are not redirected in 5 seconds, click on the following link.<BR>\n";
    print $q->a({-href => $formurl},"$formurl");
    print $q->end_html;
    exit 0;
}

my $sides = $q->param('clock');
if ($sides ne "left" && $sides ne "right" ) {
    print $q->header( -type => "text/html", -Refresh=>"5; URL=$formurl" );
    print $q->start_html( -title => "Bad Clock" );
    print $q->h2("Incorrect Clock Chosen");
    print "Please return to the form and enter \"left\" or \"right\" for clock.<BR>\n";
    print "If you are not redirected in 5 seconds, click on the following link.<BR>\n";
    print $q->a({-href => $formurl},"$formurl");
    print $q->end_html;
    exit 0;
}

my $finger =  $q->param('finger');
#
# Add code here to validate value entered for finger
# Follow logic from $sides and $guy above
#

#
# At this point all the field values are validated and we can begin
# to process
#

print $q->header(-type => "text/html" );

print $q->start_html(
-title => "Enroll Finger Print",
-style => {-code => $style},
);
print $q->h2("Enrolling Finger...");
if ($sides eq "left"){
    $ssh2->connect($lefthost) or die;
    if (! $ssh2->auth_publickey($username, $publickey, $privatekey, undef)) {
        print $ssh2->error;
    }
    my $chan = $ssh2->channel;

    $chan->exec("fprintd-enroll  -f ${finger} ${guy}");
    $chan->send_eof;
    while (<$chan>) {
        chomp;
        print "line read: $_</BR>\n";
    }

    $chan->close;

    $ssh2->disconnect();
    print $q->h2("Sent request for " . $guy . "'s " . "$finger");
    print $q->end_html;
    exit 0;
}

if ($sides eq "right"){
    $ssh2->connect($righthost) or die;
    if (! $ssh2->auth_publickey($username, $publickey, $privatekey, undef)) {
        print $ssh2->error;
    }
    my $chan = $ssh2->channel;

    $chan->exec("fprintd-enroll  -f ${finger} ${guy}");
    $chan->send_eof;
    while (<$chan>) {
        chomp;
        print "line read: $_</BR>\n";
    }

    $chan->close;

    $ssh2->disconnect();
    print $q->h2("Sent request for " . $guy . "'s " . "$finger");
    print $q->end_html;
    exit 0;
}
else{
print $q->h4("Timeclock not selected\n");
print $q->h4("Please select a timeclock to use for finger enrollment.");
exit 1;
}

## Subs

sub get_style {
my $style = <<"EOT";
body {
font-family: verdana, arial, sans-serif;
bgcolor: white;
padding-left: 5%;
}
h2 {
colors: purple;
border-bottom: 1pt solid;
}
h4 {
color: blue;
}
EOT
return $style;
}

相关内容