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

All, I'm a perl beginer. In my current project, I have to write a Perl scripts which connect to a SSH server/issue some commands/get results. Due to the environment restriction, I can only depend on the standard perl distribution. Are there any experts here can help me about that? Really thanks!
  • Comment on How can I do with only Standard Perl Distribution

Replies are listed 'Best First'.
Re: How can I do with only Standard Perl Distribution
by jbrugger (Parson) on Jan 19, 2006 at 05:39 UTC
    As noted here and here, you are able to create your own library, so you'd be able to use Net::SSH or Net::SSH::Perl or what you might need

    .
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: How can I do with only Standard Perl Distribution
by duff (Parson) on Jan 19, 2006 at 05:44 UTC

    Have a look at how Net::SSH::Perl does it and do the same thing. But since you're already modifying the system by writing a perl program, you should be able to install a module too. Or at the very least, bundle the module inside the program.

Re: How can I do with only Standard Perl Distribution
by leighsharpe (Monk) on Jan 19, 2006 at 05:45 UTC
    If your commands are not terribly interactive, you can write the commands to a file, and use system() or backticks to execute the command, piping the file to the input of the SSH client. This has a few caveats, however. You need to set up passwordless SSH first, because SSH does not like accepting passwords on STDIN. Here's an example:
    use strict; use warnings; my @output=`ssh 172.16.2.3<in.asc`; print "And here's the output:\n"; print @output;
    This assumes, however, that you have written all commands to a file called in.asc
      I should say, yes, my commands are kind of terribly interactive. I need to check the results of the one command and determine what command need to input next. (Like Expect, but I can't use Expect, since our environment doesn't have make gcc installed which required by IO::Pty).
Re: How can I do with only Standard Perl Distribution
by hawtin (Prior) on Jan 19, 2006 at 09:14 UTC

    There is another approach you can take. If you can create files in just a single directory you can still use CPAN modules. Adding this code:

    BEGIN { unshift @INC,'/home/fred/sc'; }

    To the start of your script will allow you to use any Pure Perl CPAN modules (.pm files) that have been copied into the named directory. If the CPAN modules have shared binary libraries (dll or so) then you will need to mess with either $ENV{PATH} or $ENV{LD_LIBRARY_PATH}.

    If you can only use a single script file you can still use CPAN modules by appending them to the script (obviously before any data and it is usually best to enclose each "file" in curly braces).

    The least good (but still workable) option is to read the CPAN module and "borrow" the routines into your script.

    It is almost always a better plan to use CPAN modules rather than attempt to rewrite from scratch

      use lib '/home/fred/sc';
      Not only is that much friendlier to the user, it also handles a number of possible cross-platform issues for you.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        Even better, in my opinion, for a self-contained script or application:
        use FindBin qw(); use lib "$FindBin::Bin/../lib";
Re: How can I do with only Standard Perl Distribution
by glasswalk3r (Friar) on Jan 19, 2006 at 12:16 UTC

    You can write down your programs using the modules that you need from CPAN and then using the PAR to package everything.

    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
Re: How can I do with only Standard Perl Distribution
by tweetiepooh (Hermit) on Jan 19, 2006 at 10:43 UTC
    What do you have available?

    If Net::Telnet is there can you telnet back onto you own server? That way you may be able to emulate a sort of Expect by capturing return values and sending responces. ie telnet to localhost, then SSH from that out to remote box.