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

Hi Monks - I am trying to connect and fire a bunch of commands using ssh. I am in the prod environment and dont have approval to install modules like Net::SSH /OpenSSH. Is there any way i can fire commands over SSH to remote server, without using the above/any modules? Can this be achieved using : system("ssh host"); So far, the system(); menthod has been giving me troubles. Does anybody know the trick ? Thanks very much in advance.

  • Comment on Remote SSH to linux Server without using Module

Replies are listed 'Best First'.
Re: Remote SSH to linux Server without using Module
by Corion (Patriarch) on Aug 20, 2015 at 17:23 UTC

    See open or IPC::Open3. With either, you can work interactively with the ssh process. But I recommend either launching Perl on the other side or launching a single process on the other side which produces all its output, so you should just do:

    my $output = `ssh $host '$command'`;
Re: Remote SSH to linux Server without using Module
by sierpinski (Chaplain) on Aug 20, 2015 at 17:52 UTC

    Having worked (and still work) within the constraints of a "prod environment", I can appreciate the issue, but I must ask for some details:

    1. Is the constraint just for prod, ie can you test this in dev/test and then promote the code into production?
      • Reason: if this is the case, then go the correct route and install the Net::SSH module and follow the proper promotion procedure.
    2. Is it just a limitation of technology (ie proxy), permissions (no root) or process (not allowed) that you can't install modules?
      • Reason: If it's a permissions issue, you can install the modules in your home directory for example, and "use lib" those modules, or you can download them directly from CPAN and install them manually if a proxy is the issue.
    Honestly if the correct solution is to use a module like Net:SSH (which sounds like it's the case) there should be a way. I'm no stranger to corporate BS, but hopefully there is light at the end of the tunnel.
Re: Remote SSH to linux Server without using Module
by kroach (Pilgrim) on Aug 20, 2015 at 18:41 UTC

    You can open an ssh connection with a pipe:

    my ($user, $host) = ('me', 'host.domain'); open my $SSH, '|-', 'ssh', join '@', $user, $host;

    Then you can send commands to ssh with print:

    print $SSH "mv *.back backups/"; print $SSH "echo 'Moved backups' >> log";
Re: Remote SSH to linux Server without using Module
by hippo (Archbishop) on Aug 21, 2015 at 09:00 UTC
    So far, the system(); menthod has been giving me troubles. Does anybody know the trick ?

    It's hard to imagine what troubles you are having. system works perfectly well for this and there isn't any trick involved. If you go into a bit more detail than just "giving me troubles" then I'm sure any of us could point you in the right direction.

    Sorry to hear about the prohibition of modules.

Re: Remote SSH to linux Server without using Module
by adefaria (Beadle) on Aug 25, 2015 at 16:48 UTC
    I wrote my own a while back. Does ssh but also will fall back to rsh then telnet. Implemented as an object it keeps the connection open for you to send command after command. Also handles different shell styles on the other end (bash, tcsh, etc.). Note this is not a CPAN module so there's no real install, just use the module. See Rexec.pm
A reply falls below the community's threshold of quality. You may see it by logging in.