PHP使用SSH扩展

Leave a Comment2011.11.01 18:01 by admin 

PHP添加SSH扩展,就可以在WEB页面利用PHP来管理我们的Linux主机了。这样是不是对服务器管理人员来说,可以减轻不少的负担呢。

[Windows]

修改PHP.ini文件,添加extension = php_ssh2.dll;

$connection = ssh2_connect(‘*.*.*.*’, 22);
if (ssh2_auth_password($connection, ‘user’, ‘pass’)) {
echo “Authentication Successful!\n”;
} else {
die(‘Authentication Failed…’);
}
$stream = ssh2_exec($connection, ‘ls’);
stream_set_blocking($stream, true);
$data = “”;
while($buf = fread($stream, 4096)) {
$data .= $buf;
}
fclose($stream);
echo $data;

这几天在做工具,需要从一个站获取数据,然后对得到的数据再进行处理;访问网站获取网站的内容,大家可能想到的是PHP的file_get_contents()函数,因为这个函数相对简单,但我要说的是,虽然file_get_contents()函数使用起来比较简单,但在实在应用中,对于获取网络上的内容并不是很实用,原因在于:1、file_get_contents对于某些动态的URL不能很好的解析;2、对于网速慢的时候可能会获取不到值。

这里我推荐使用curl实现获取网站内容的功能,curl相对于file_get_contens也有一定的优势,1、curl可以动态设定超时的时间;2、curl本来就是用于模拟数据提交的,对于数据的组合较好,且能够很好的解析需要访问的URL;3、使用curl时,还可以在其中加入代理(http)或(socks5);4、curl可以模拟浏览器;

基于上述的优势,我也选择了curl方式,下面我就来谈谈如何使用curl来模拟浏览器及如何使用代理:

通过查看PHP手册,我们可以找到curl的相关资料,CURLOPT_USERAGENT    The contents of the “User-Agent: “ header to be used in a HTTP request, 这个就是用来模拟我们的浏览器的选项


curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; CIBA)");

CURLOPT_PROXY   The HTTP proxy to tunnel requests through.  这个就是用来设置代理的选项

上面讲述了代理设置选项,那默认情况下是http的代理,现在网络上的http代理并不是很多,当然我们需要使用socks代理的时候怎么办呢?

当然PHP curl为我们提供了使用socks代理的方法,不过curl只支持socks5的代理,socks4的代理不能使用;

CURLOPT_PROXYTYPE  设置代理类型,默认为http

我想到这里大家又会遇到一个问题,那就是现在网络上的免费代理并不是很多了,虽然每次搜索的时候都会得到一大批的代理,不过里面能用的就没几个,假如我们从ISP那里购买的代理,又该如何使用呢?我的答案是还是这样使用,只是在curl再加入用户名和密码而已。

CURLOPT_PROXYUSERPWD   设置代理服务器的用户名和密码

同样这里附上curl部分代码:

    /** Get Web source-code from Designated URL
    * @param http_url       string
    * @param proxy          array('type','host','port','username','userpass','timeout')
    * @param result         array('header','nobody','follow','f_charset','t_charset')
    * @param cookie         string
    * @return string        string
    * @exception Exception  none
    */
    function get_url_contents($http_url,$proxy=NULL,$result=false,$cookie=NULL) {
        if(empty($http_url)) {
            return '';
        }
        if(empty($proxy['timeout'])) {
            $proxy['timeout']=30;
        }
        if(!empty($result['header'])) {
            $result['header']=false;
        }
        if(!empty($result['nobody'])) {
            $result['nobody']=false;
        }
        $ch = curl_init();            // 初始化curl
        if(!empty($proxy['host']) && !empty($proxy['port'])) {
            curl_setopt($ch, CURLOPT_PROXY, $proxy['host'].':'.$proxy['port']);//  设置代理服务器IP和站端口
        }
        if(!empty($proxy['type']) && $proxy['type']=='socks5') {
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);    //   设置代理代理为socks5,默认为http,即不需要设置
        }
        if(!empty($proxy['username']) && !empty($proxy['userpass'])) {
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy['username'].':'.$proxy['userpass']);//   设置代理的用户名和密码
        }
        curl_setopt($ch, CURLOPT_URL, $http_url);//  添加需要访问的URL地址
        if(!empty($cookie)) {
            curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie );    //  设置cookie
            curl_setopt ( $ch, CURLOPT_COOKIE, $cookie );    //
  设置cookie
        }
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );   //  设置返回类型,
        if($result['follow']) {
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); // 自动获取,防止中间有301的跳转
        }
        curl_setopt ( $ch, CURLOPT_HEADER, $result['header'] );//  设置是否需要返回头部信息
        curl_setopt ( $ch, CURLOPT_NOBODY, $result['nobody'] );//  设置是否不需要内容
        $contents = curl_exec($ch);//grab URL and pass it to the browser
        if(!empty($result['f_charset']) && !empty($result['t_charset']) && $result['f_charset']!=$result['t_charset']) {
            $contents=iconv($result['f_charset'],$result['t_charset'],$contents);
        }
        //echo curl_error ($ch);
        curl_close($ch);            //   关闭curl资源
        return $contents;
    }

 

Live Chat Software