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

How can I use Perl to establish an SSH session and then tunnel through that session? (rather than calling ssh/putty)

Replies are listed 'Best First'.
Re: HTTP requests through an SSH tunnel
by Corion (Patriarch) on Mar 24, 2009 at 09:19 UTC

    Maybe it's already enough to use the window-less version of PuTTY, plink.exe? Usually, LWP::UserAgent will pick up the proxies from the environment after a call to ->env_proxy. But it seems to me you're only doing port forwarding, so real proxies don't come into play, unless you have a HTTP proxy program running on the target machine where you ssh to.

    At least the two major ssh implementations, PuTTY and OpenSSH, have port forwarding, but simple port forwarding won't let you make outbound connections on the remote machine.

Re: HTTP requests through an SSH tunnel
by frieduck (Hermit) on Mar 24, 2009 at 13:46 UTC
Re: HTTP requests through an SSH tunnel
by JavaFan (Canon) on Mar 24, 2009 at 15:23 UTC
    What I do on my Unix box to tunnel is:
    REMOTE_SERVER="server-to-reach.some.where"; SSH_HOST="server-with-ssh-access.some.where"; LOCAL_ADDR=127.0.0.2 LOCAL_PORT=80 REMOTE_PORT=80 USER="you-over-there" sudo echo "$LOCAL_ADDR $REMOTE_SERVER" >> /etc/hosts # Only once eve +r sudo ssh -A -L $LOCAL_ADDR:$LOCAL_PORT:$REMOTE_SERVER:$REMOTE_PORT $US +ER@$SSH_HOST
    And then I can just surf to $REMOTE_SERVER using my webbrowser (or wget) without having to do anything special. Note that the setup assumes you have access to the remote server from the machine you ssh to. (This can be the same machine, but doesn't have to).

    I use several tunnels this way (using 127.0.0.2, 127.0.0.3, etc); some of them tunnelling through more than one host (using ProxyCommands in ~/.ssh/config)

    I know you can use an almost similar setup from Windows - just don't ask me about the details.

Re: HTTP requests through an SSH tunnel
by arcix (Initiate) on Mar 24, 2009 at 21:51 UTC
    Thanks for the quick replies,
    @corion, I'll look into plink, windowless putty will definitely be more convenient

    @frieduck, correct me if I'm wrong, but that code allows SSHx to go through an existing proxy, but I need exactly the opposite - to use an established SSH connection as a SOCKS (or HTTP) proxy. I've looked at LWP::Protocol::socks, but I don't really even need a SOCKS proxy, HTTP is fine. Also, the reason why I spawn Putty is because I want to use several different tunnels (SSH sessions) at the same time.

    @JavaFan, this is basically what I want, and its what I'm doing now, but with Putty rather than OpenSSH.

    Really, my question is:
    how can I emulate an ssh tunnel in Perl *WITHOUT* calling ssh or putty/plink?