Is it possible to ssh to a server in perl and execute multiple commands without doing them all at once?
I know I can potentially do something like
system( 'ssh user@server "cd directory; grep blah *; curl someurl.com"');
However, I'd like the functionality where I don't have to keep SSHing over and over to the same machine. Further, I'm not sure how to catch output from each command that's executed. If anyone could point me in the right direction or share some sample code with me, I'd appreciate it.
Thanks in advance for all responses.
You probably want to look at Net::SSH:Expect
Looks good. Thanks for the info. However, I'm not the admin on the server where I do my work is it possible store the pm file locally and perl recognize it? Sorry, but I'm pretty new to perl.
Installation of Perl modules requires lot more work than just dumping pm files into a directory.
Thankfully, complete Perl installations provide the CPAN module that takes care of:
- connecting to the CPAN module repository to fetch the latest module version
- resolving all dependencies, and installing required modules (for example, DBD::Oracle module requires DBI module; so CPAN will install DBI before DBD::Oracle)
- running tests added by the module author
- installing manual pages that come with the module
and so on...
So the first thing you may want to check is if you can use the CPAN module. The Perl script "cpan" is the command-line interface to the CPAN module; so see if these commands work on your *nix box -
cpan
cpanp
("cpanp" or CPANPLUS is a more intuitive and user-friendly alternative to CPAN.)
It is recommended to run these commands as root, failing which your SysAdmin would've (or should've) given you access.
In order to install Perl modules in your local directory, set the PERL5LIB environment variable so that it points to your directory of choice.
If you cannot use cpan, then you are on a path that even seasoned Perl programmers fear to tread.
HTH,
tyler_durden