mikihasa has asked for the wisdom of the Perl Monks concerning the following question:

i have a script that works great from the command line, but it fails at the call to cmd when run from the cgi...any idea why that might be?

here is the script (i know there are easier ways to get directory listings, but this is just a sublime example of what i am really trying to accomplish):

#!/usr/bin/perl use Net::SSH::Perl; use strict; print(qq(Content-Type: text/html\n\n)); print(qq(<HTML><HEAD><TITLE>Remote Test</TITLE></HEAD><BODY>)); &list_directory(); print(qq(</BODY></HTML>)); exit; sub list_directory { my(%ssh_params) = ( 'port' => '40001', ); my($hostname) = '12.345.67.890'; my($ssh_username) = 'root'; my($ssh_password) = 'noneya'; my($command) = qq(ls -l); print(qq(creating ssh connection<br>\n)); my($ssh) = Net::SSH::Perl->new($hostname), %ssh_params); print(qq(logging in<br>\n)); $ssh->login($ssh_username, $ssh_password); print(qq(running command: $command<br>\n)); my($stdout, $stderr, $exitval) = $ssh->cmd($command); print(qq(directory listing: $stdout<br>\n)); } 1;
when run from the shell (as the web server user), it works great (a.k.a. i get a directory listing) but when run via the web browser it says it is going to run the command but i get no output

i thought it might be something with the default port (22) not being accessible from the web, which is why i started sshd on another port (40001) and used the port option to access it that way, but still no luck

my guess is that it is a permissions issue (isn't it always?) but i am not sure how to ferret it out beyond what i have already done

Replies are listed 'Best First'.
Re: Net::SSH::Perl shell vs web
by jcpunk (Friar) on Jan 17, 2004 at 22:41 UTC
    a few quick questions.

    is the 'remote host' a seperate computer?
    do you have a firewall doing any sort of access control?
    just incase have you tried redirecting STDERR to STDOUT to see if anything odd comes out?
    ummmm set debuging to max and check the logs on both the remote and local host....

    sadly that is about all i can think of for now. due to the network at school being all screwed up i cant get any inbound bandwidth (time to download Net::SSH::Perl 7 days till completed) means i cant actually try it out but if an act of God occurs I will try it out and see what I can make of it.


    jcpunk
    all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
      wow, thanks for all of the great ideas

      yes, i am ssh'ing to a remote computer

      both the local (where this script is running on the webserver) and the remote computers are dedicated linux servers (with rackshack) and so i would have total control over any sort of firewall...i don't believe anything is in place -- at least i didn't put anything up

      i should try redirecting stderr to stdout again...i had done that originally and found the first problem i had was that the web user didn't have permission to write to the .ssh directory, but i fixed it

      i'll check the logs, too

      thanks again...i hope i can use this module -- the alternative is telnet but that won't work for all of the remote servers i have to access since it is disabled on some of them (which is the reason i started with this ssh module)

        the first problem i had was that the web user didn't have permission to write to the .ssh directory, but i fixed it
        I hope that you are talking about the webuser's .ssh directory and not, say, your .ssh directory. That directory is created with user-only write permissions as a matter of security, not to inconvenience users. Furthermore, I seem to recall ssh checking the permissions of a couple of things (the .ssh directory being one of them) and failing if they are not set to a sufficiently restrictive access level.

        thor