Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Module Net::SSH::Expect - Cannot interact

by phillipewu (Initiate)
on Apr 30, 2008 at 05:55 UTC ( [id://683629]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I am trying the module Net::SSH::Expect to login to a UNIX machine using ssh.

I know that I can set up the hosts as "trusted" hosts but the site I am working at does not allow this. Also to get to the target machine I need to go via a number of intermediate servers. I was hoping to use the module Net::SSH::Expect to log me in.

I have set up a simple trial but the "interact" part does not work - it hangs.
Can someone tell me how to fix it?
===========================================================

use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new ( host => "utility", user => 'vzblk0', raw_pty => 1 ); # Start the ssh process $ssh->run_ssh() or die "SSH process couldn't start: $!"; # you should be logged on now. Test if you received the remote prompt: $output=$ssh->read_all(2); print($output); if ($output =~ />\s*\z/) {die "where's the remote prompt?"}; # - now you know you're logged in - # # disable terminal translations and echo on the SSH server # executing on the server the stty command: $ssh->exec("stty raw -echo"); $output = $ssh->exec("who"); print ($output); $output = $ssh->exec("uname -a"); print ($output); $exp=$ssh->get_expect(); $exp->interact;

Replies are listed 'Best First'.
Re: Module Net::SSH::Expect - Cannot interact
by moklevat (Priest) on Apr 30, 2008 at 13:45 UTC
    First, you may have better luck using Net::SSH2. However, if for some reason you must use SSH1, it appears that you are using a passwordless login (implying that you are using trusted keys to login), but you said that you can not set up trusted hosts. Try using the example segment below from the Net::SSH::Expect docs for logging in with a password (includes error checking).
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; # # You can do SSH authentication with user-password or without +it. # # Making an ssh connection with user-password authentication # 1) construct the object my $ssh = Net::SSH::Expect->new ( host => "myserver.com", password=> 'pass87word', user => 'bnegrao', raw_pty => 1 ); # 2) logon to the SSH server using those credentials. # test the login output to make sure we had success my $login_output = $ssh->login(); if ($login_output !~ /Welcome/) { die "Login has failed. Login output was $login_output"; }
      Sorry there seems to be a misunderstanding.

      The login part works fine.
      However when I run $exp->interact I am expecting that I will have an interactive session but I cannot type (human) anything into that window.

      Please, any ideas how to fix this?
        This worked for me:

        #!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new( host => 'localhost', user => 'someuser', password => 'somepassword' ); warn "Starting SSH..."; $ssh->run_ssh(); warn "Testing login output..."; my $login_output = $ssh->login(); print " Done", "\n"; my $who = $ssh->exec('who'); print($who); my $exp = 'who'; $exp = $ssh->get_expect(); $exp->interact();

        I guess that my system works fine without disabling the terminal. It may or may not work for you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-28 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found