如何使用php的CURL代理服务器使用ssh(socks ...)? [英] How do you proxy though a server using ssh (socks…) using php’s CURL?

查看:862
本文介绍了如何使用php的CURL代理服务器使用ssh(socks ...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ssh,如下所示:

  ssh -D 9999 username @ ip-address-of-ssh -server 

但在php CURL中,但我真的不知道如何做到这一点? / p>

我注意到CURLPROXY_SOCKS5是一个类型的php网站,但猜测不会工作,因为它不是真的socks,它的ssh ...



我目前正在使用此代码:

  curl_setopt($ ch,CURLOPT_PROXY, ip:port'); 

但我使用免费的代理,它是相当缓慢和不可靠,我也发送敏感信息。这是为什么我想通过我信任的保存服务器代理它,但我只有ssh设置它,它不能主持一个适当的代理。

解决方案

您可以在PHP脚本中使用libssh2和curl。




  • 首先需要从PECL网站获取ssh2库。或者, PEAR套件也支持SSH2支持。

  • 安装后,您可以阅读 ssh2文档的设置

  • 在您的脚本中,您可以设置隧道。

  • 在脚本中设置隧道后,您可以指定CURL代理。

  • 执行您的CURL操作。

  • 释放隧道资源并关闭脚本中的连接。



我不是PHP专家,但这里是一个粗略的例子:

 <?php 
$ connection = ssh2_connect(ip-address-of-ssh-server,22);
ssh2_auth_pubkey_file($ connection,'username','id_dsa.pub','id_dsa');
$ tunnel = ssh2_tunnel($ connection,'127.0.0.1',9999);
curl_setopt($ ch,CURLOPT_PROXY,'127.0.0.1:9999');
//执行curl操作

//连接和隧道将在会话和会话处死。
?>



最简单的选项



考虑是使用sftp(ftp over ssh)而不是CURL ...这可能是一个文件从一个服务器复制到另一个安全在PHP ...的方法...



更简单的例子:

 <?php 
$ connection = ssh2_connect(ip-address-of-ssh-服务器,22);
ssh2_auth_password($ connection,'username','password');
ssh2_scp_send($ connection,'/ local / filename','/ remote / filename',0644);
?>


I want to use ssh, something like this:

ssh -D 9999 username@ip-address-of-ssh-server

But within php CURL, but I don't really see how this could be done?

I noticed "CURLPROXY_SOCKS5" as a type in the php site, but guess that wouldn’t work since it isn’t really socks, it’s ssh…

I’m currently using this code:

curl_setopt($ch, CURLOPT_PROXY, ‘ip:port'); 

But I'm using a free proxy and it’s rather slow and unreliable, I'm also sending sensitive information over this proxy. This is why I want to proxy it over a save server I trust, but I only have ssh setup on it and it’s unable to host a proper proxy.

解决方案

You can use both libssh2 and curl from within a PHP script.

  • First you need to get the ssh2 library from the PECL site. Alternatively, the PEAR package has SSH2 support too.
  • After installing you can then read the ssh2 documentation on setting up a tunnel.
  • In your script you can then set up the tunnel.
  • After the tunnel is set up in the script you can specify the CURL proxy.
  • Perform your CURL operation.
  • Release the tunnel resource and close the connection in your script.

I'm not a PHP expert, but here's a rough example:

<?php
$connection = ssh2_connect(ip-address-of-ssh-server, 22);
ssh2_auth_pubkey_file($connection, 'username', 'id_dsa.pub', 'id_dsa');
$tunnel = ssh2_tunnel($connection, '127.0.0.1', 9999);
curl_setopt($ch, CURLOPT_PROXY, ‘127.0.0.1:9999'); 
// perform curl operations

// The connection and tunnel will die at the and of the session.
?>

The simplest option

Another option to consider is using sftp (ftp over ssh) instead of CURL... this is probably the recommended way to copy a file from one server to another securely in PHP...

Even simpler example:

<?php
$connection = ssh2_connect(ip-address-of-ssh-server, 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>

这篇关于如何使用php的CURL代理服务器使用ssh(socks ...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆