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

Hello Monks, I am trying to remotely execute a command in a linux box using the following piece of code:
use warnings; use strict; use Net::SSH::Perl; my $command = "ls"; my $host = "192.168.112.191"; my $user = "admin"; my $pass = "admin\@123"; my $ssh = Net::SSH::Perl->new( $host ); $ssh->login( $user, $pass ); my($stdout, $stderr, $exit) = $ssh->cmd( $command ); print $stdout;
However, when I run the script I get the following error message:

Permission denied at net_ssh_perl.pl line 12.

I have seen some posts with diverse opinion about using Net::SSH:Perl module. Posting this question expecting wisdom.

Please let me know if I should be looking for some alternatives to achieve my goal. In short, I want to run a command in a remote machine and want to know the output of the command.

Replies are listed 'Best First'.
Re: Remote Execution of a Command in Linux Box
by marto (Cardinal) on Dec 10, 2013 at 10:30 UTC

    Line 12 is the login line, have you cheched that you can connect with the same credentials from the same client machine using ssh? Ensure valid login credentials. If this works, enable debugging as described in the module documentation.

      Hi, I have checked that I can connect to the machine using ssh. I have turned on debug and got the following output.
      anupambordoloi.local: Reading configuration data /Users/anupam.bordolo +i/.ssh/config anupambordoloi.local: Reading configuration data /etc/ssh_config anupambordoloi.local: Connecting to 192.168.112.191, port 22. anupambordoloi.local: Remote version string: SSH-2.0-OpenSSH_5.3 anupambordoloi.local: Remote protocol version 2.0, remote software ver +sion OpenSSH_5.3 anupambordoloi.local: Net::SSH::Perl Version 1.36, protocol version 2. +0. .nupambordoloi.local: No compat match: OpenSSH_5.3 anupambordoloi.local: Connection established. anupambordoloi.local: Sent key-exchange init (KEXINIT), wait response. anupambordoloi.local: Algorithms, c->s: 3des-cbc hmac-sha1 none anupambordoloi.local: Algorithms, s->c: 3des-cbc hmac-sha1 none anupambordoloi.local: Entering Diffie-Hellman Group 1 key exchange. anupambordoloi.local: Sent DH public key, waiting for reply. anupambordoloi.local: Received host key, type 'ssh-dss'. anupambordoloi.local: Host '192.168.112.191' is known and matches the +host key. anupambordoloi.local: Computing shared secret key. anupambordoloi.local: Verifying server signature. anupambordoloi.local: Waiting for NEWKEYS message. anupambordoloi.local: Send NEWKEYS. anupambordoloi.local: Enabling encryption/MAC/compression. anupambordoloi.local: Sending request for user-authentication service. anupambordoloi.local: Service accepted: ssh-userauth. anupambordoloi.local: Trying empty user-authentication request. anupambordoloi.local: Authentication methods that can continue: public +key,keyboard-interactive. anupambordoloi.local: Next method to try is publickey. Permission denied at net_ssh_perl.pl line 12.

        This smells like a password problem. Please change

        my $pass = "admin\@123";

        into

        my $pass = 'admin@123';

        and try again.

        (If you put the password into single quotes then perl won't try to parse your string.)
Re: Remote Execution of a Command in Linux Box
by hdb (Monsignor) on Dec 10, 2013 at 12:49 UTC
      Thanks, hdb.
Re: Remote Execution of a Command in Linux Box
by Anonymous Monk on Dec 10, 2013 at 11:36 UTC

    FWIW, the example code worked for me sans password (I have ssh-agent set up that does not require a password multiple times).

Re: Remote Execution of a Command in Linux Box
by Preceptor (Deacon) on Dec 10, 2013 at 22:07 UTC

    I really dislike using embedded passwords in scripts. There's all sorts of reasons that I don't think it's a good idea. So I would suggest - configure SSH to authenticate via public/private keys. You need to look at the documentation for 'ssh-keygen' and 'authorized_keys'. For bonus points, you can inhibit command execution via the authorized keys file, to limit what a remote user can do.