首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP ssh2_tunnel:使用代理/socks抛出ssh服务器?

PHP ssh2_tunnel:使用代理/socks抛出ssh服务器?
EN

Stack Overflow用户
提问于 2014-03-31 23:39:13
回答 1查看 6.8K关注 0票数 2

我想使用代理/socks抛出ssh服务器,我在这里找到了很好的示例How do you proxy though a server using ssh (socks…) using php’s CURL?,但是示例不起作用,下面是我的代码。

代码语言:javascript
复制
<?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!";
}
?>

当我执行这个脚本时,我得到了错误响应:

代码语言:javascript
复制
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. 

我会在谷歌上搜索这个错误,并尝试修复,但无法解决问题。

有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2014-08-20 22:27:20

ssh_tunnel似乎不是这样工作的,ssh_tunnel会请求"$connection“对指定的ip /端口进行隧道操作。

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

正如您在我的示例中看到的,到Google的隧道只是一个流,您可以通过它发送流请求

代码语言:javascript
复制
$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 /端口的通道之前,您可以通过以下方式打开本地端口:

代码语言:javascript
复制
$localsock = socket_create_listen(0);
socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort);

然后你的代码有了这些改变:

代码语言:javascript
复制
$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!";
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22765956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档