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

Hi,

I know I'm just overlooking something simple. I'm using Net::SSH::Perl to connect to a remote server and execute an arbitrary command. So far so good.

It appears that the actual connection is made on the first (SSH2 or every connection in SSH1) cmd() call. So, when the connection fails, I might get a "permission denied on line X" where X is the line of the first cmd().

Thinking that I could just capture stderr with the Net::SSH::Perl register_handler() handlers (either SSH1 or SSH2), I set up the handlers:

$ssh->register_handler("stderr", sub { my ($channel, $buffer) = @_; print " Unable to connect to $host\n" if ($buffer =~ /permission/i); });

Between the new constructor and the login(). I still get the "Permission denied on line X" message. grrr

clues?

Jason L. Froebe

No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Replies are listed 'Best First'.
Re: how to handle login failures with Net:::SSH::Perl?
by elwarren (Priest) on Sep 20, 2004 at 22:00 UTC
    Can't you just grab the returned exit value?
    my($stdout, $stderr, $exit) = $ssh->cmd($cmd);

      Unfortunately, no. Ends up being we need to place the $ssh->cmd() within an eval block. Net::SSH::Perl was designed to die() if the connection attempt fails.

      I was hoping to avoid an eval block as I really don't like them because they require a recompilation each time we encounter an eval block. As the camel book says, it is a performance hit.

      eval { ($stdout, $stderr, $exit) = $ssh->cmd("df -k $dump_dir"); }; if ($@) { print "SSH Connection attempt failed\n" if ($@ =~ m/Permission denied/i); }

      The login() method doesn't do anything but set the settings for the connection.. It doesn't actually connect to anything.

      Jason L. Froebe

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

        ...they require a recompilation each time we encounter an eval block...it is a performance hit.
        There is no "performance hit" from the eval in your code (at least not nearly as much as you think there is). eval $string has a performance hit, as perl does have to compile the code in $string each time, but not in eval { BLOCK }, because the code in the BLOCK is already compiled. See the eval docs in perlfunc.
        I think you're going to see an even more significant performance hit from running ssh and df -k over and over before you notice a performance penalty from eval :-)
Re: how to handle login failures with Net:::SSH::Perl?
by KeighleHawk (Scribe) on Sep 21, 2004 at 19:35 UTC
    Look at Net::SSH::Perl closely. You may need to look at the code.

    It's been awhile since I've worked with this module so I'm afraid I can't give you anything specific. But as I recall, this module has a different concept of a command failing than you might be expecting. Much of what you would consider an error is actually returned as a warning if at all. Kind of like the SQL "zero rows returned" debate of whether that is an error, success or warning sort of thing.

    Since the module can't really determine if your command executed successfully or not, there are almost no "error" conditions. You should be able to turn on verbose logging and capture that. There may be something surprising happening there...

      Hi,

      Actually, it is on the connection itself that the cmd() is die'ing on. So there won't be any output in STDERR from the remote command.

      Jason L. Froebe

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

        even so, setting verbose mode should give you more information. This was how I debugged the connection/setup problems I had when using this module in the past...

        Have you tried setting verbose mode? I notice the module has a debug option which is approximatly the same thing. Perhaps setting this and posting the results would be more enlightening...?