On Sep 11, 3:47 pm, [EMAIL PROTECTED] (Chas Owens) wrote: > On 9/11/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > > How to automate ssh login with Expect.pm? > > > I found an example to automate telnet login at > >http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod#The_examples_... > > > I had done > > yum install perl-Expect > > yum install perl-Net-SSH > > > My perl script is: > > #!/usr/bin/perl > > # > > use Expect; > > use Net::SSH; > > > $remotehost = "172.20.11.242"; > > > my $ssh = new Net::SSH ($remotehost) # see Net::Ssh > > or die "Cannot ssh to $remotehost: $!\n";; > > > ... > > > $ ssh.pl > > Can't locate object method "new" via package "Net::SSH" at ./ssh.pl > > line 24. > > Net::SSH does not have an OO interface. It provides five functions. > Perhaps you are thinking of Net::SSH::Perl? > > from perldoc Net::SSH > ssh('[EMAIL PROTECTED]', $command); > > issh('[EMAIL PROTECTED]', $command); > > ssh_cmd('[EMAIL PROTECTED]', $command); > ssh_cmd( { > user => 'user', > host => 'host.name', > command => 'command', > args => [ '-arg1', '-arg2' ], > stdin_string => "string\n", > } ); > > sshopen2('[EMAIL PROTECTED]', $reader, $writer, $command); > > sshopen3('[EMAIL PROTECTED]', $writer, $reader, $error, $command);
I modified the telnet script found at http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod#The_examples_... by replacing "Telnet" with "SSH". This is a perl script to automate ssh login with Expect.pm, I try to ssh to a computer and do "ls" (a simple command now, more commands later). This script ssh to a computer successfully but then it exit without doing "ls". How to fix it? use Expect; $username = "root"; $password = "fjsdf123"; my $exp = Expect->spawn("ssh [EMAIL PROTECTED]") or die "Cannot spawn ssh: $!\n";; my $spawn_ok; $exp->expect($timeout, [ qr'login: $', sub { $spawn_ok = 1; my $fh = shift; $fh->send("$username\n"); exp_continue; } ], [ 'password: $', sub { my $fh = shift; print $fh "$password\n"; exp_continue; } ], [ eof => sub { if ($spawn_ok) { die "ERROR: premature EOF in login.\n"; } else { die "ERROR: could not spawn ssh.\n"; } } ], [ timeout => sub { die "No login.\n"; } ], '-re', qr'[#>:] $', #' wait for shell prompt, then exit expect sub { my $fh = shift; print $fh "/bin/ls\n"; } ); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/