简体   繁体   中英

Perl Net SFTP Foreign Proxy

I am trying to connect to a client machine via sftp and place files there via Perl Net::SFTP::Foreign . I've seen lots of examples on how to do this, but none of them involve using a socks proxy .

Background: For the standard Net::FTP I can use the IO Socket Wrapper , and everything gets proxied:

use IO::Socket::Socks::Wrapper({
    ProxyAddr  => 'my.proxy.address',
    ProxyPort  => 1080,
    SocksDebug => 1 #enable for debugging
});

However, this does not work for Net::SFTP::Foreign. So, I'm trying to come up with a work-around.

if ($sftp = Net::SFTP::Foreign->new("ftp.myserver.com",
   user      => "ftpusername",
   password  => "ftpuserpwd",
   more      => [-o => 'ProxyCommand=ssh -D 1080 my.proxy.address'],
))
{
     some code that runs when successful auth...
}

...I found the above on Perl Monks, but it doesn't seem to be helping. Here's the url to Perl Monks explaining this: http://www.perlmonks.org/?node_id=938019

I think the key is to use the correct ProxyCommand, but I'm not sure that this is correct.

Does anyone know how I can send files out via sftp and through a proxy?

You need an utility program that knows how to talk to a SOCKS proxy. For instance socat .

The first thing you need to do is to find out how to invoke that utility in order to establish the connection through the proxy to the SFTP server. It should be something like this:

 socat stdio SOCKS4:proxy_server:sftp_server:22

where proxy_server and sftp_server are the names of the proxy and the SFTP server respectively.

You know when you are doing it right because you get the version string from the SSH server back, something similar to SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2

Then, you just have to pass the command to the Net::SFTP::Foreign constructor as follows:

$sftp=Net::SFTP::Foreign->new("ftp.myserver.com",
                              user     => "ftpusername",
                              password => "ftpuserpwd",
                              more     =>
                                  [-o=>'ProxyCommand=socat stdio SOCKS4:$proxy_server:ftp.myserver.com:22']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM