Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Net::SSH::Expect problem

by finfan (Acolyte)
on Mar 20, 2013 at 19:01 UTC ( #1024594=perlquestion: print w/ replies, xml ) Need Help??
finfan has asked for the wisdom of the Perl Monks concerning the following question:

I'll start with the code.

#! /usr/bin/perl use strict; use warnings; use Net::SSH::Expect; use lib ('./subs'); use subs; my $ssh = Net::SSH::Expect->new ( host => '10.77.6.55', password => 'mypass', user => 'root', debug => 1, raw_pty => 1 ); my $login_output = $ssh->login(); if ($login_output !~ /Last login/) { die "Login Failed\n"; } $ssh->run_ssh() or die "SSH process couldn't start : $!"; $ssh -> exec("stty raw -echo"); print "Running ls -l\n"; my $ls = $ssh->exec("ls -l /"); print "ls = $ls"; $ssh->close();

I get no errors, but ls is not returning anything. Tried $ssh->exec("touch /tmp/foo"); and a file was NOT created.

Ideas?

Comment on Net::SSH::Expect problem
Download Code
Re: Net::SSH::Expect problem
by blue_cowdawg (Monsignor) on Mar 20, 2013 at 19:57 UTC

    I've always found cpan:Net::SSH::Expect to be a bit wonky, but nevertheless if you removed the line $ssh->run_ssh() or die "SSH process couldn't start : $!"; it will behave a bit differently. Also you can add the log_file parameter with a filename to new to get diagnostic information.. sorta...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Net::SSH::Expect problem
by rnewsham (Hermit) on Mar 20, 2013 at 20:11 UTC

    I have never used that module but did a quick test and got the same behaviour

    I added this line from the documentation and it did die

    ($ssh->read_all(2) =~ />\s*\z/) or die "where's the remote prompt?";

    Looking in the logs on the machine I was accessing it was a problem with the password. Hope that helps, sorry I don't have time to test further right now.

Re: Net::SSH::Expect problem
by rnewsham (Hermit) on Mar 20, 2013 at 20:56 UTC

    Having a second look I see the problem

    You don't need the call to run_ssh.

    This should work for you

    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; use lib ('./subs'); use subs; my $ssh = Net::SSH::Expect->new ( host => '10.77.6.55', password => 'mypass', user => 'root', debug => 1, raw_pty => 1 ); my $login_output = $ssh->login(); if ($login_output !~ /Last login/) { die "Login Failed\n"; } $ssh->exec("stty raw -echo"); print "Running ls -l\n"; my $ls = $ssh->exec("ls -l /"); print "ls = $ls"; $ssh->close();
Re: Net::SSH::Expect problem
by Khen1950fx (Canon) on Mar 20, 2013 at 22:47 UTC
    Here's what worked for me:
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; my $host = '127.0.0.1'; my $pass = 'password'; my $user = 'user'; my $cmd = "ls -l /"; my $ssh = Net::SSH::Expect->new( host => $host, password => $pass, user => $user, timeout => 5, raw_pty => 1, log_stdout => 1, ); $ssh->login(); $ssh->exec("$cmd ."); $ssh->close;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024594]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2014-12-18 17:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (59 votes), past polls