CGI execute command via ssh



Aloha

I',m trying to execute a command via ssh on a remote maschien via a perl-cgi script. The app i'm building does this a lot, with 99% of success. The missing 1% i'm trying to understand and after that to solve.

I use backstick to run the ssh command something like:

my $data = `ssh root\@192.168.10.8 /root/somewhere/something $args`

This work fine. But the 1% is a command where up to 5 MB are in my $data. --- Only if i run it local on the 192.168.10.8. Remote i only fetch 1.6 MB. While i doesn't matter if i should get 2,3, or 5 MB. Most annoying is that the data are strings .. and the missing one are randomly placed somewhere.

After a lot of wasted time with no success i kick that solution and tried to put all the data in a MySQL DB on the remote maschine ( 192.168.10.8 ) again this works better since all data get into that DB.
When execute that script from the cgi script, i get an error message.


While the error message was generate from within the script from the remote maschine - this error "could no happen". I couldn't get rid of that error so i again tried a different way.

This time the remote scripte run as a daemon process, with no report back to the caller, just fire and forget. Again it work direkt from commandline and via commandline call over ssh and again not from within the cgi script.

What did i miss?
What das CGI what works so nice agains me?

here the shorted scripts;

CGI Script

--------------------------------------------------------------------

use strict;
use warnings;
use CGI::FormBuilder;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use constant DEBUG => 0;

my $host = $ENV{'QUERY_STRING'};
die unless ($host=~/^[\w-]+$/);

my $page = CGI->new;
print $page->header;
print $page->start_html;
print `ssh root\@192.168.10.8 /root/nodedateien $host`;
print $page->end_html;

----------------------------------------------------------------------

Remote script

-----------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use DBI;
use POSIX;

my $arg = shift or die ;

die "Can't fork" unless defined ( my $child = fork);
exit 0 if $child;       #parent dies;
setsid();
open(STDIN, "</dev/null");
open(STDOUT, ">/dev/null");
open(STDERR, ">&STDOUT");
chdir '/';              #change working directory
umask(0);               #forget file mod creation mas


my $dbh = DBI->connect('dbi:mysql:database=DB;host=localhost:3306','root','pass',{RaiseError => 1, AutoCommit => 1});
my $sth = $dbh->prepare('delete from nodedateien');
$sth->execute();
$sth = $dbh->prepare('insert into nodedateien (HL_Name,LL_Name,Backup_Date) Values ( ?,?,? ) ');


open(IN,"/command" |");
my ($val1,$val2,$val3);
for (1 .. 12) {<IN>}
while(<IN>)
{
next if (/^\n/);
next if (/return code/);
if ($_=~/^BACKUP_DATE: (.+)/) { $val3 = $1}
if ($_=~/^ HL_NAME: (.+)/) { $val1 = $1}
if ($_=~/^ LL_NAME: (.+)/) { $val2 = $1; update($val1,$val2,$val3)}
}


sub update
{
        my ($HL,$LL,$BD) = @_;
        $sth->execute($HL,$LL,$BD);
}

.



Relevant Pages

  • Re: JETCOMP.exe utility switch to path to database to compact
    ... I ran the command line script many times. ... error message something about can't find the internal program. ... operable program or batch file. ...
    (microsoft.public.scripting.vbscript)
  • Re: [opensuse] CUPS error "client-error-not-authorized"
    ... For these type of Canon copier/printers there is ... Running this script in 10.0 and in 10.1 worked perfectly, ... is fine until the last step, then it gives the error message ... configuration command "codehost-config", ...
    (SuSE)
  • chmod error message 0481-014
    ... If I do the command as root ... but if I use my user account this message occurs. ... but the script fails because of there errors. ...
    (comp.unix.aix)
  • Re: vb script in XP
    ... But - when I again ran the vbs script I still recieve the error message ... recommending that you run the command ...
    (microsoft.public.windowsxp.general)
  • Re: JETCOMP.exe utility switch to path to database to compact
    ... I ran the command line script many times. ... When I enter that command exactly as shown, the following error message ... I assumed from this that CMD required some sort of adjustment to the ...
    (microsoft.public.scripting.vbscript)