in reply to Problem with Net::SSH::Expect on unavailable hosts

Your code doesn't look like the code in the synopsis for the module. You're using the no password method, in which it shows that run_ssh() returns something false if it fails, but assigning that to $login_output which is the very same name of the variable in the first option of the synopsis, to log in with a username and password, where it gets the results of the login() method.

The two methods are options, not mix and match. I'd suggest reading the Net::SSH::Expect docs a little closer. Either use one method or the other, but don't try to intermingle them. That's not the way they are documented to work.

  • Comment on Re: Problem with Net::SSH::Expect on unavailable hosts

Replies are listed 'Best First'.
Re^2: Problem with Net::SSH::Expect on unavailable hosts
by jrsimmon (Hermit) on Jul 01, 2009 at 19:20 UTC
    Other than being slightly misleading, there's nothing obviously wrong with his use of $login_output. He's capturing the return value of a function call, which is generally a good practice. If he were mixing $ssh->run_ssh() and $ssh->login(), then your critique would be valid.
      My critique is perfectly valid, thanks. His return value is what he needs to check, not $@ or $!. The reason he's getting nothing useful out of them is that nothing useful is being put into them. This is apparently because of the mingling of random parts of the documentation rather than reading them for comprehension.
        I've checked the result of $login_output as well, and whether the ssh is successful or not, it always returns the same value. I have a list of 3 servers to use while I'm testing this, two work fine, and the 3rd fails, but all return values and login_output is exactly the same in every iteration.

        I have read through the documentation probably dozens of times, and have tried every variation of what I've seen there, with no success each time. I've copied the code verbatim and still have same issue. Another site I found was checking for $@, but of course that value was useless, and the return value is always 1 for any server.

        According to what I read, using eval (which I have never done in Perl before) is what populates the $@ variable, but again that apparently was not the case. I'll give an example of my list and the output of what I have below:

        Server list =
        belle (valid) everglades (valid) hugger (not valid)
        (Meaningful) Code:
        my $ssh = Net::SSH::Expect->new ( host => "$current_server", user => "$user", raw_pty => 1 ); # removed the eval $login_output = $ssh->run_ssh(); print("$current_server \$! = $!\n"); print("Login output for $current_server = $login_output\n"); ($ssh->read_all(2) =~ />\s*|$\s*\z/) or die "where's the remote prompt +?"; $ssh->exec("stty raw -echo");
        And my output:
        belle $! = Illegal seek Login output for belle = 1 belle Dump device: /dev/zvol/dsk/rpool/dump (dedicated) everglades $! = Illegal seek Login output for everglades = 1 everglades Dump device: /dev/md/dsk/d101 (swap) hugger $! = Illegal seek Login output for hugger = 1 SSHProcessError The ssh process was terminated. at ./script.pl line 39

        As you can see this is a script to monitor the dump device on Solaris boxes, and it works fine on the first two. Still working out why I'm getting the Illegal Seek messages (which is why I temporarily have the -w flag turned off -- but that's another issue).

        Thanks!
        -Tim