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

Hey Monks,

I'm fairly new to Perl, so please excuse my stupidity. I can get the Net::SSH module working properly with the while loop, but I want to throw in a if..else statement, but I'm lost.

Also, what are the functions of the READER and WRITER commands in this script?

Thanks -Dru

#!/usr/bin/perl use Net::SSH qw(sshopen2); use strict; my $user = "jdoe"; my $host = "192.168.2.3"; my $cmd = "ps -ef | grep -v grep |grep fwd"; sshopen2("$user\@$host", *READER, *WRITER, $cmd) || die "ssh: +$!"; # Example from README # # while (<READER>) { # chomp(); # print "$_\n"; # } if ($cmd) { print "FWD is running\n"; } else { print "FWD is not running!!\n"; } close(READER); close(WRITER);

Replies are listed 'Best First'.
Re: Help with Net::SSH
by btrott (Parson) on May 31, 2001 at 21:53 UTC
    Might I suggest that you instead use Net::SSH::Perl (self-promotion, yes), which provides a Perl client interface to SSH?

    It would make this much easier:

    use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new($host); $ssh->login($user); my($out, $err, $exit) = $ssh->cmd($cmd);
    $out now holds the STDOUT of the command $cmd on the remote machine, so it looks just like it would if you had logged into the remote machine and run the command. So you can do a pattern match on it, for example, to see if 'fwd' is running.

      I originally tried using Net::SSH::Perl, but I found it difficult to install because of all the prereqs for Crypt::RSA. I'll give it another try because it sounds like it will be easier.

      Thanks,

      Dru

        Crypt::RSA is completely optional, don't worry about it if you don't have it. It's only used for RSA support in SSH-2 (SSH-1 RSA is built in to Net::SSH::Perl), and odds are you're probably not using RSA in SSH-2.

        Also, the list of prereqs depends completely on what protocol you'll be using. If you're using SSH-1 only, your list of prereqs is pretty short, and should be fairly simple to install.

        If you're using SSH-2, the list of prereqs is longer, but it's still pretty easy, at least on a *nix system. Windows is a little trickier. Which are you running on?

        If you're having trouble using Net::SSH::Perl, feel free to email me.

Re: Help with Net::SSH
by Anonymous Monk on May 31, 2001 at 21:10 UTC
    A trick with ps and grep: If you dont want the grep in your list do simply  ps -opt | grep [c]md! Enclosing the first letter of the command in brackets shows the command but not the grep.