1

I am completely new to perl scripting and I need to write a script to login to a router and execute few commands. I have tried the below script but I am getting an error as "Can't locate Net/SSH" Could anybody please tell how to install the module or is there any better script...?

#!/usr/bin/perl -w
use strict;
use lib qw("/path/to/module/");

use Net::SSH::Perl;

my $hostname = "hostname";
my $username = "username";
my $password = "password";

my $cmd = shift;

my $ssh = Net::SSH::Perl->new("$hostname", debug=>0);
$ssh->login("$username","$password");
my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd");
print $stdout;
Learner
  • 51
  • 1
  • 9
  • 1
    possible duplicate of [What's the easiest way to install a missing Perl module?](http://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module) – AKHolland Aug 11 '14 at 13:37
  • 1
    Net::SSH and Net::SSH::Perl are two different modules. You can not load one and then use the other. – salva Aug 11 '14 at 13:54
  • I have tried installing Net::SSH::Perl but not able to, actually I am not getting a ppm promt while trying from cmd. Dont know where i am going wrong. – Learner Aug 12 '14 at 03:55

3 Answers3

1

You are loading Net::SSH at the start of your program.

use Net::SSH

But later, when you go to actually use the module, you use Net::SSH::Perl - which is a completely different module.

The error that you're getting indicates that Net::SSH isn't installed on your system. If you want to install it, the easiest way is probably to use your OS's packaging system. Assuming that you're on Linux that could be something like:

$ sudo yum install perl-Net-SSH

or

$ sudo apt-get install libnet-ssh-perl

But looking at the rest of your code, I think you're trying to use Net::SSH::Perl. In which case you probably want to just change use Net::SSH to use Net::SSH::Perl.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Sorry, That was a mistype, I am using 'Net::SSH::Perl' through out but not getting any output, and I am working on windows. – Learner Aug 12 '14 at 03:49
1

I would encourage you to use Net::OpenSSH because I have had a lot of issues in the past on installing Net::SSH::Perl. To install Net::OpenSSH, you can install it via cpan (cpan install Net::OpenSSH) and the usage is almost the same as Net::SSH::Perl. Refer to this for usage on Net::OpenSSH - http://search.cpan.org/~salva/Net-OpenSSH-0.62/lib/Net/OpenSSH.pm

A.P.
  • 26
  • 1
-1

Depending on your OS, you can probably install Net::SSH like this:

sudo perl -MCPAN -e 'install Net::SSH'

Depending what you actually want to do on the router, you may find that expect is easier... have a look here.

Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432