Net::SSH::Expect

Net::SSH::Expect is a SSH wrapper to execute remote commands.
Download

Net::SSH::Expect Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Bruno Negrao Guimaraes Zica
  • Publisher web site:
  • http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod

Net::SSH::Expect Tags


Net::SSH::Expect Description

Net::SSH::Expect is a SSH wrapper to execute remote commands. Net::SSH::Expect is a SSH wrapper to execute remote commands.SYNOPSIS 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"; } # - now you know you're logged in - # # Starting ssh without password # 1) run the constructor my $ssh = Net::SSH::Expect->new ( host => "myserver.com", user => 'bnegrao', raw_pty => 1 ); # 2) now start the ssh process $ssh->run_ssh() or die "SSH process couldn't start: $!"; # 3) you should be logged on now. Test if you received the remote prompt: ($ssh->read_all(2) =~ />s*z/) or 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"); # runs arbitrary commands and print their outputs # (including the remote prompt comming at the end) my $ls = $ssh->exec("ls -l /"); print($ls); my $who = $ssh->exec("who"); print ($who); # When running a command that causes a huge output, # lets get the output line by line: $ssh->send("find /"); # using send() instead of exec() my $line; # returns the next line, removing it from the input stream: while ( defined ($line = $ssh->read_line()) ) { print $line . "n"; } # take a look in what is immediately available on the input stream print $ssh->peek(0); # you'll probably see the remote prompt # the last read_line() on the previous loop will not include the # remote prompt that appears at the end of the output, because the prompt # doesn't end with a 'n' character. So let's remove the remainder # prompt from the input stream: $ssh->eat($ssh->peek(0)); # removes whatever is on the input stream now # We can also iterate over the output in chunks, # printing everything that's available at each 1 second: $ssh->send ("find /home"); my $chunk; while ($chunk = $ssh->peek(1)) { # grabs chunks of output each 1 second $ssh->eat($chunk); } # Now let's run an interactive command, like passwd. # This is done combining send() and waitfor() methods together: $ssh->send("passwd"); $ssh->waitfor('password:s*z', 1) or die "prompt 'password' not found after 1 second"; $ssh->send("curren_password"); $ssh->waitfor(':s*z', 1) or die "prompt 'New password:' not found"; $ssh->send("new_password"); $ssh->waitfor(':s*z', 1) or die "prompt 'Confirm new password:' not found"; $ssh->send("new_password"); # check that we have the system prompt again. my ($before_match, $match) = $ssh->waitfor('>s*z', 1); # waitfor() in a list context die "passwd failed. passwd said '$before_match'." unless ($match); # closes the ssh connection $ssh->close();This module is a wrapper to the ssh executable that is available in your system's $PATH. Use this module to execute commands on the remote SSH server. It authenticates with the user and password you passed in the constructor's attributes user and password.Once an ssh connection was started using the connect() method it will remain open until you call the close() method. This allows you execute as many commands as you want with the exec() method using only one connection. This is a better approach over other ssh wrapper implementations, i.e: Net::SCP, Net::SSH and Net::SCP::Expect, that start a new ssh connection each time a remote command is issued or a file is transfered.It uses Expect.pm module to interact with the SSH server. A get_expect() method is provided so you can obtain the internal Expect object connected to the SSH server. Use this only if you have some special need that you can't do with the exec() method.This module was inspired by Net::SCP::Expect http://search.cpan.org/~djberg/Net-SCP-Expect-0.12/Expect.pm and by Net::Telnet and some of its methods work the same as these two modules. Requirements: · Perl


Net::SSH::Expect Related Software