tags:

views:

852

answers:

2

I want to write a Perl script which ssh's into a remote host, prompts for password before running some commands on remote host and parse the output in the local Perl script. Any help or pointers on this? Thanks!

+5  A: 

There is Net-SSH-Perl
Some notes on usage.


If you are discouraged with the module and need an easy way out,
you could directly invoke SSH on the shell.

perl -e {`ssh user@server "command"`}

should work...


I hope you intend to let the SSH use its passwords and not implement 'security' some yourself.
Could also consider public key authentication.

nik
I tried using Net-SSH-Perl first. I kept running intohostname: Connection established.hostname: Sent key-exchange init (KEXINIT), wait response.No matching comp found: client zlib server none,[email protected] at /.../perl/5.6.1/Net/SSH/Perl/SSH2.pm line 87Before I could figure that out, your second option worked for me and yes, it does ask for the password before I enter the cmd(as I wanted). Thanks for the help! Finally, getting to know how I could work around the zlib server error would satisfy my curiosity :)
@Iceman It seems like the client wants to use compression, and the server does not support it. So, you can try compression => 0 in the constructor call. See http://search.cpan.org/dist/Net-SSH-Perl/lib/Net/SSH/Perl.pm#Net%3A%3ASSH%3A%3APerl-%253Enew(%24host%2C_%25params)
Sinan Ünür
+2  A: 

I think perhaps the tool you want is not Perl but Expect. It is designed exactly for interacting with command-line software programmatically. It is written in Tcl, which is a language somewhat simpler than Perl, but broadly in the same family and not difficult to learn.

Norman Ramsey
Thanks Norman, I already have it working in Expect.. But I have to do it in Perl for some reason..
The documentation for the Perl module Expect.pm has a funny statement that says (something like) the author wanted to make Tcl/Expect available to programmers without subjecting them to Tcl.
James Thompson
And, although Expect is written in Tcl, Expect is even simpler than Tcl if you don't need to get fancy. Perl has an Expect module too, but I'll always be fond of the original since it was so incredibly useful for poisoning wtmp for all of my professors who thought that wtmp was a useful tool for checking on who was getting work done. :)
brian d foy