CGI execute command via ssh
- From: ms@xxxxxxxxxxxxxxx (MNibble)
- Date: Wed, 31 Aug 2005 16:09:17 +0200
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);
}.
- Prev by Date: can any body tell me how to remove quotes from a name.
- Next by Date: DBD::Oracle installation script assumes oracle is installed local ly?
- Previous by thread: can any body tell me how to remove quotes from a name.
- Next by thread: DBD::Oracle installation script assumes oracle is installed local ly?
- Index(es):
Relevant Pages
|
|