SSH with Perl using file handles, not Net::SSH

Posted by jorge on Stack Overflow See other posts from Stack Overflow or by jorge
Published on 2010-12-28T02:18:08Z Indexed on 2010/12/28 2:54 UTC
Read the original article Hit count: 264

Filed under:
|
|
|
|

Before I ask the question:

I can not use cpan module Net::SSH, I want to but can not, no amount of begging will change this fact

I need to be able to open an SSH connection, keep it open, and read from it's stdout and write to its stdin. My approach thus far has been to open it in a pipe, but I have not been able to advance past this, it dies straight away.

That's what I have in mind, I understand this causes a fork to occur. I've written code accordingly for this fork (or so I think).

Below is a skeleton of what I want, I just need the system to work.

    #!/usr/bin/perl


    use warnings;

    $| = 1;

    $pid = open (SSH,"| ssh user\@host");

    if(defined($pid)){
        if(!$pid){
            #child
            while(<>){
                print;
            }
        }else{
            select SSH;
            $| = 1;

            select STDIN;

            #parent
            while(<>){
                print SSH $_;
                while(<SSH>){
                    print;
                }
            }
        close(SSH);
        }
    }

I know, from what it looks like, I'm trying to recreate "system('ssh user@host')," that is not my end goal, but knowing how to do that would bring me much closer to the end goal.

Basically, I need a file handle to an open ssh connection where I can read from it the output and write to it input (not necessarily straight from my program's STDIN, anything I want, variables, yada yada)

This includes password input.

I know about key pairs, part of the end goal involves making key pairs, but the connection needs to happen regardless of their existence, and if they do not exist it's part of my plan to make them exist.

© Stack Overflow or respective owner

Related posts about perl

Related posts about ssh