简体   繁体   中英

CGI which runs a shell script (which itself runs a shell/command on another server) does not return the output

Ok guys, I know this is pretty a rare case but I really would appreciate your wise comments on this.

I am developing a monitoring/operational web gui using CGI (Perl/SSH) to monitor more than 20 severs. Most of the work is done, I have prepared the shell scripts and we are automating most of the stuff there. Anyways, we are persuaded to provide a front-end for the monitoring and automation stuff we did.

Long story short, I am using one of the server as the main gateway to run the CGIs. What is my plan, on this server, I use Perl CGI to run shell scripts by using for example qx() function and put the output in a file. the shell script will connect to other servers and run the command. the problem is while I run the cgi or shell script in the SSH it works fine and it prints the output but when I call the cgi through web it only returns until the shell output of CGI server (the one that Im connecting to is not there while with SSH its working fine).

here is the CGI code:

#!/usr/bin/perl -w
use strict;
use CGI;
use Shell;

my $query = new CGI;
print $query->header(-charset=>'utf-8');

 my $value=qx(sh dspace91.sh > space.txt);
 open(FILE, '<space.txt') or die "Can't read file [$!]\n";

 while (<FILE>){
 $document = $document .  $_;
 }

 close FILE;
 my $gdate=qx(date);

 print  "<div id='black' style='color:white'><pre>$document</pre>Information generated on <font color='#00CC00'>$gdate</font></div>";

and here is the shell script code (dspace91.sh file)

#!/bin/bash

expect -c 'spawn ssh -q username@serverIP "df -h"; expect password ; send "password\n" ; interact; ';

exit;

What I assume is the problem is working with I/O environment variables. something to do with STDOUT. not sure though!

this is the output when I run the CGI through SSH:

spawn ssh -q username@serverIP df -h
username@serverIP's password:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G  7.1G   12G  39% /
/dev/sda7              90G   25G   61G  29% /iwa
/dev/sda6             4.9G  1.3G  3.4G  28% /oracle
/dev/sda3              10G  595M  8.9G   7% /var
/dev/sda1              99M   45M   49M  48% /boot
tmpfs                  16G     0   16G   0% /dev/shm
drst002:/logs         400G  274G  127G  69% /backup-logs
drst002:/unix         7.0T  5.2T  1.9T  74% /unix

this is the output when I run it through the web gui:

spawn ssh -q username@serverIP df -h
username@serverIP's password: 

Information generated on Tue Apr 3 02:21:47 IRDT 2012

Any ideas folks?

Some ideas:

  • Use ssh public key authentication and eliminate the need to use expect altogether.

    expect is a nasty hack that never proves reliable enough.

  • try:

    expect -c '...' 2>&1

  • also, but I don't think that's the case , CGIs usually run into trouble because they run as a different user than the one the developer is testing on the command line with. Always check if that could be affecting your setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM