?
Solved

Perl net ssh

Posted on 2011-10-01
9
Medium Priority
?
1,394 Views
Last Modified: 2012-05-12
Hi,

I got this error:
Use of uninitialized value in string at ./test.pl line 11.

after running below script:

#!/usr/bin/perl -w
 use Net::SSH::Perl;
    my $host="x.x.x.x";
    my $pass="pass";
    my $user="user";
    my $ssh = Net::SSH::Perl->new($host);
    $ssh->login($user, $pass);
    my($stdout, $stderr, $exit) = $ssh->cmd("sh run");
    print "$stdout";

Im quite new with perl. Any help?

Thanks.
0
Comment
Question by:chainfear
  • 5
  • 4
9 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 36898560
Very odd... since your script does not have 11 lines.  Is there anything you cut out of your script before posting it?

I haven't used Net::SSH::Perl, but looking at the manual page for it, all of your syntax looks correct.  Any other info that you can provide us experts?
0
 

Author Comment

by:chainfear
ID: 36898601
Sorry. Below is the exact script:


#!/usr/bin/perl -w
 use Net::SSH::Perl;
    my $host="x.x.x.x";
    my $pass="pass";
    my $user="user";
    my $ssh = Net::SSH::Perl->new($host);
    $ssh->login($user, $pass);
    my($stdout, $stderr, $exit) = $ssh->cmd("sh run");
    print "$stdout";

And below is the error:
Use of uninitialized value in string at ./test.pl line 9.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36899722
Well, that's just a warning line, not an error (you have '-w' added to the shabang line).  However, it appears that you're also not getting any results.  Change line 8 to:

my($stdout, $stderr, $exit) = $ssh->cmd("echo hello");

And see if you get 'hello' as output.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chainfear
ID: 36900888
Hi,

I've removed  the -w but still cannot get any output.

I've changed line 8 to be read:
 my ($stdout, $stderr, $exit) = $ssh->cmd("show version");

I am supposed to get the switch version and its running fine when I executed "show version" straight on the switch.

I missed anything?

Thank you.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36900987
Anything from the other variables?

print "OUT: $stdout\n";
print "ERR: $stderr\n";
print "Exit code: $exit\n';

Just an FYI... when I usually do something like this, I usually use "expect" instead of Perl, seems to more consistently do a better job.
0
 

Author Comment

by:chainfear
ID: 36901028
Its blank...

[root@svr perl]# ./test.pl
OUT:
ERR:
Exit code:
[root@svr perl]#

I will install Net::SSH::Expect and try. BTW, I am just curious. Why it is blanked...?

Thanks for your time & effort
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36901044
Not sure... should get *something* back (if nothing else, an exit code).  Net::SSH::Perl hasn't been updated in 3 years, but then again, I haven't update one of my CPAN modules in over 10 years.  SSH hasn't changed that much, so I would think this module would still work just fine.

Let me know how Net::SSH::Expect works for you.
0
 

Accepted Solution

by:
chainfear earned 0 total points
ID: 36902102
I found a way now. I use Net::OpenSSH.

#!/usr/bin/perl -w
use Net::OpenSSH;
my $ssh = Net::OpenSSH->new('user:pass@ip:22');
$ssh->system('sh ver') or die "Can't ssh to host" . $ssh->error;

Thanks anyway!
0
 

Author Closing Comment

by:chainfear
ID: 36935089
Use Net::OpenSSH instead of Net::SSH::Perl
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question