Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Net::OpenSSH + CGI problem

by fwalters (Novice)
on Feb 15, 2017 at 20:35 UTC ( [id://1182095]=perlquestion: print w/replies, xml ) Need Help??

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

I have a small 'test.cgi' file where I'm just trying to ssh to a router to get some commands (the entire project is a BGP Lookingglass but this is where the problem lies).

It runs fine from command line, as the www-data user that is executing from apache (at least based on the ENV vars), but when I run it from the browser it's just a blank page.

I checked to make sure that 755 was everywhere as appropriate, for now (at least until I figure this out) www-data is able to login and like I said runs the script fine...

I checked the socket directory to make sure it was permed right as well and did some searching before posting (don't want to be shame flamed!).

Here's the code:
#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use File::Temp qw/ tempdir /; # necessary minimum for CGI print "Content-type: text/plain\n\n"; # prepare temp dir my $temp = "/home/www-data"; + my $login_user= "xxxxx"; my $login_pass = "xxxxx"; # open SSH session my %opts = ( user => $login_user, password => $login_pass, ctl_dir => $temp, strict_mode => 0 ## NOT recommended - see my comments ); my $ssh = Net::OpenSSH->new("n.n.n.n", %opts); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; # perform command and print output my @lines = $ssh->capture("sh int desc") or die "remote command failed: " . $ssh->error; print "OUTPUT<br>\n"; print @lines;
and... Here is the console output (mostly :-)
Content-type: text/plain This system is for authorized users only. Unauthorized access is prohi +bited. --snip-- Logging In<br> OUTPUT<br> Last switch-over Thu Dec 29 12:08:33 2016: 6 weeks, 6 days, 1 hour, 52 + minutes ago Wed Feb 15 14:00:58.710 cst Interface Status Protocol Description ---------------------------------------------------------------------- +---------- --snip--
and... Here is the browser output:
Logging In<br>
Also, in apache/errors.log I get:
This system is for authorized users only. Unauthorized access is prohi +bited. --snip-- Connection to n.n.n.n closed by remote host. remote command failed: 0 at /usr/lib/cgi-bin/test2.cgi line 34.
34 is the 'my @lines' line... Any thoughts?

Replies are listed 'Best First'.
Re: Net::OpenSSH + CGI problem
by salva (Canon) on Feb 16, 2017 at 07:47 UTC
    Both ssh and Net::OpenSSH are able to provide quite detailed debugging information.

    Redirect STDERR to some file or ensure it ends in your apache logs. Add $Net::OpenSSH::debug=-1 at the beginning of your script and also, call the constructor with the option master_opts=>'-vvv'.

    Then, after removing any confidential information from the output, post it here!

      Thanks!

      error.log contents after debug

        Quick followup...

        we run some Cisco ASR and IOS based routers, apparently the IOS based routers work fine, while the ASR (IOXR) based routers.

        The main difference, at least visually, is the prompt:

        For the working router the prompt I get is:

        1-CHI-RR1-Curly#

        While for the one that doesn't work I get:

        RP/0/RSP0/CPU0:1-CHI-PE14-SOB-QuiGon#

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1182095]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found