不过,并非所有人知道PHP可以与SSH连接的特性以及与执行远程命令的能力,不过这方面却非常有用。由于我们可以在很多不同的方面利用PHP,因此它有很多设置选项来控制其行为。一组庞大的可选参数能够保证您可以将 PHP 用于许多不同的目的,但这同时也意味着这些参数和服务端配置的组合会带来一些安全问题。笔者一直在PHP CLI应用程序中使用SSH,笔者是从cronjobs中使用它的,不过一开始并非十分简单,可以说颇费周折。关于安全使用Shell2 函数的手册也不是十分实用,笔者进行了多次试验之后才有了今天这篇小文章,愿您读了之后能为您配置PHP节省一点儿时间。
字串3
在这篇文章中,笔者需要假设:
你正在运行的操作系统是Debian / Ubuntu。如果你运行的不是Debian / Ubuntu,你可能需要用你的Linux发行版本提供的数据包管理器来替换本文对应内容。
字串3
你运行的是PHP5.如果你运行的不是PHP5,可用PHP4代替之。
你对PHP和服务器管理有基本的了解。 字串6
你已经安装了PHP。
字串6
先决条件 字串4
安装程序包
首先,让我们安装下面的程序包:
字串7
sudo aptitude update 字串3
sudo aptitude install php5-dev php5-cli php-pear buid-essential \
字串4
openssl-dev zlib1g-dev 字串2
安装完成进入下一步。 字串8
编译libssh2
在从sourceforge网站下载了Libssh2之后,我们需要编译它,不过不要担心,你只需要按照如下的方法操作: 字串6
cd /usr/src 字串6
wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz 字串4
tar -zxvf libssh2-0.14.tar.gz
字串4
cd libssh2-0.14/ 字串4
./configure
make all install 字串4
如果你想检查是否有了一个新版本,可以查看SF.NET.不过,0.14这个版本就足够了。
字串8
安装 字串4
安装ssh2.so 字串3
下一步,我们需要将libssh和 PHPr链接起来。有一个PECL模块可以完成这个功能。我们可以使用PEAR安装它。 字串4
pear install -f ssh2
字串6
-f参数确保SSH2被安装,即使并没有一个稳定的选择对象。你还可以使用如下的包名称:ssh2-beta来强行运行。
现在你需要确保我们这个新的SSH2.SO模块被PHP加载。编辑你的php.ini文件(对于CLI实用程序:/etc/php5/cli/php.ini,对于Apache实用程序:/etc/php5/apache2/php.ini)
字串2
extension=ssh2.so 字串3
这应该放在“Dynamic Extensions”的下面,大约在第515行左右。 字串8
PHP支持SSH编写代码 字串8
你刚刚在PHP中启用了SSH2。那么现在应该如何利用它呢?有两个选择。SSH支持:
1.执行方法: 字串9
这告诉你的服务器的操作系统来执行什么东西,并且通过管道传回到你的脚本。
字串4
2.外壳方法: 字串2
这种方法在操作系统中打开一个实际的外壳,这正像通过终端应用程序登录时所操作的那样。有一些路由器并没有一个完全的POSIX一致性实施过程,而是在你登录时立即运行其自身的应用程序。这时你就需要这种方法。 字串1
下面我们分别详述之: 字串3
第一种方法:执行
字串8
你最好为下面的代码创建函数或者是一个类,不过本文仅仅起到一个为您提供基本观念的作用,所以说你可以如此开始:
字串4
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist")
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("server1.example.com", 22))){
字串4
echo "fail: unable to establish connection\n";
字串4
} else {
// try to authenticate with username root, password secretpassword 字串6
if(!ssh2_auth_password($con, "root", "secretpassword")) {
echo "fail: unable to authenticate\n";
} else { 字串9
// allright, we're in!
echo "okay: logged in...\n"; 字串7
// execute a command 字串1
if(!($stream = ssh2_exec($con, "ls -al" )) ){ 字串7
echo "fail: unable to execute command\n"; 字串3
} else{
// collect returning data from command
stream_set_blocking( $stream, true );
$data = "";
while( $buf = fread($stream,4096) ){ 字串1
$data .= $buf;
} 字串8
fclose($stream); 字串7
}
}
第二种方法:外壳
字串6
同样道理,你也可以为如下的代码编写函数或者一个类。不过,本文仅仅提供基本观念:
字串6
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist")
字串2
// log in at server1.example.com on port 22 字串8
if(!($con = ssh2_connect("server1.example.com", 22))){
字串9
echo "fail: unable to establish connection\n"; 字串1
} else { 字串4
// try to authenticate with username root, password secretpassword
字串7
if(!ssh2_auth_password($con, "root", "secretpassword")) { 字串8
echo "fail: unable to authenticate\n";
字串7
} else {
// allright, we're in! 字串7
echo "okay: logged in...\n"; 字串8
// create a shell 字串2
if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){
echo "fail: unable to establish shell\n"; 字串9
} else{
stream_set_blocking( $shell, true ); 字串7
// send a command
fwrite($shell,"ls -al\n"); 字串7
sleep(1);
// & collect returning data 字串7
$data = ""; 字串7
while( $buf = fread($shell,,4096) ){ 字串4
$data .= $buf; 字串3
}
fclose($shell);
} 字串6
}
字串4
}