PHP ssh2_隧道:使用代理/ssh服务器?

PHP ssh2_隧道:使用代理/ssh服务器?,php,curl,ssh,proxy,Php,Curl,Ssh,Proxy,我想使用proxy/ssh服务器,我在这里找到了很好的示例,但示例不起作用,这是我的代码 <?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://c

我想使用proxy/ssh服务器,我在这里找到了很好的示例,但示例不起作用,这是我的代码

<?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!";
}
?>
我会搜索这个错误并尝试修复,但无法解决问题


有人能帮我吗?

看来ssh\u隧道不是那样工作的,ssh\u隧道将请求“$connection”执行到指定ip/端口的隧道

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

正如您在我的示例中所看到的,通往谷歌的隧道只是一条流,您可以通过它发送流请求

$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/端口请求隧道之前打开本地端口:

$localsock = socket_create_listen(0);
socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort);
然后使用以下更改创建代码:

$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!";
}

我尝试了您的代码,但当我添加这一行“socket_getsockname($localsock,$YourInternetIPaccessible,$openedPort);”时,服务器没有响应。我怎样才能在ubuntu终端上获得$YourInternetIPaccessible?谢谢。试试这个。你测试过你的代码了吗?我尝试了,但在这一行“socket_getsockname($localsock,$YourInternetIPaccessible,$openedPort);”上出现了错误。错误是什么,它无法创建侦听套接字?
$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!";
}