For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > CGI execute command via ssh









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author CGI execute command via ssh
MNibble

2005-08-31, 6:55 pm

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=loca
lhost: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);
}

William Ampeh

2005-08-31, 6:55 pm





2 things could be happening:

1./ Timeout
2./ size of your descriptors.
That is: on a "?nix" platform type

limit


You see something like:

william:/foobar(1)% limit
cputime unlimited
filesize unlimited
datasize unlimited
stacksize 10240 kbytes
coredumpsize 0 kbytes
memoryuse unlimited
vmemoryuse unlimited
descriptors 1024
memorylocked 4 kbytes
maxproc 7168


__________________

William Ampeh (x3939)
Federal Reserve Board



MNibble
<ms@definitiv-ba.
de> To
beginners@perl.org
08/31/2005 10:09 cc
AM
Subject
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=loca
lhost: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);
}


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com