PHP ssh2_tunnel:使用代理/袜子抛出ssh服务器? [英] PHP ssh2_tunnel: Using proxy/socks throw ssh server?

查看:140
本文介绍了PHP ssh2_tunnel:使用代理/袜子抛出ssh服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用代理/袜子抛出ssh服务器,我从这里找到了一个很好的例子如何通过使用php的CURL的ssh服务器(socks…)代理服务器?但示例不起作用,这是我的代码。

I want using proxy/socks throw ssh server and i found nice example from here How do you proxy though a server using ssh (socks…) using php’s CURL? but example don't work, here is my code.

<?php
$connection = ssh2_connect("64.246.126.25", 22);
if(ssh2_auth_password($connection, "ubnt", "ubnt"))
{
    if ($tunnel = ssh2_tunnel($connection, "127.0.0.1", 9999))
    {
        $url = "http://checkip.dyndns.com/";
        $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9999");
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
        $result = curl_exec($ch);   
        echo $result;

    }else{
        echo "Tunnel creation failed.\n";
    }
} 
else
{
    echo "failed!";
}
?>

我执行此脚本时出现错误响应:

When i excute this script i have error response:

Warning: ssh2_tunnel() [function.ssh2-tunnel]: Unable to request a channel from remote host in /home/domain.com/ssh2/index.php on line 7
Tunnel creation failed. 

我会用Google搜索此错误并尝试解决,但不能解决问题。

I will googled for this error and trying fix but can't solve problems.

有人可以帮助我吗?

推荐答案

似乎ssh_tunnel不能那样工作,ssh_tunnel

It seems ssh_tunnel does not work like that, ssh_tunnel will request the "$connection" to perform a tunnel to the specified ip / port.

在您的代码中,我假设您正在尝试创建一个侦听本地端口的本地端口。 cURL请求作为代理,但是基本上您是在要求ssh服务器连接到自身(127.0.0.1)。

In your code i assume you are trying to create a local port which listens for the cURL requests as proxy, but basically you are asking the ssh server to connect to itself (127.0.0.1).

如您在我的示例中所看到的,通往Google的隧道仅仅是可以发送流请求的流

As you may see in my example, the tunnel to Google is only a stream through which you may send stream requests

$connection = ssh2_connect($sshServerIP, 22);
ssh2_auth_password($connection, $sshServerUser, $sshServerPass);
$sock = ssh2_tunnel($connection, "google.com", 80);
fwrite($sock, "GET /? HTTP/1.0\r\n\r\n");
$body = "";
while (!feof($sock))
$body .= fgets($sock);
echo $body;

您可以先打开本地端口,然后再通过以下方法请求通往IP /端口的隧道:

You can open a local port before requesting the tunnel to your ip / port through:

$localsock = socket_create_listen(0);
socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort);

然后您的代码进行了以下更改:

And then your code with these changes:

$connection = ssh2_connect("64.246.126.25", 22);
if(ssh2_auth_password($connection, "ubnt", "ubnt"))
{
    if ($tunnel = ssh2_tunnel($connection, $YourInternetIPaccessible, $openedPort))
    {
        $url = "http://checkip.dyndns.com/";
        $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:$openedPort");
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
        $result = curl_exec($ch);   
        echo $result;

    }else{
        echo "Tunnel creation failed.\n";
    }
} 
else
{
    echo "failed!";
}

这篇关于PHP ssh2_tunnel:使用代理/袜子抛出ssh服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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