使用 php ssh2 模块实现远程执行命令

使用 php ssh2 模块可以在php程序中远程执行命令,只要编写一些简单的脚本工具就能实现对远程服务器的集中管理,大大简化系统运维工作的繁琐。

 

安装

1. 安装 libssh2 库

http://www.libssh2.org/ 下载源代码安装即可。

 

2. 安装 ssh2 模块

http://pecl.php.net/package/ssh2 下载源代码包,安装步骤:

/usr/local/php/bin/phpize

./configure --with-ssh2 && make

cp .libs/ssh2.so /data/php/lib/php/extensions/no-debug-non-zts-20060613/

修改 php.ini 增加 extension=ssh2.so

 

示例

<?php
$connection = ssh2_connect('192.168.0.145', 22);
ssh2_auth_password(
$connection, 'username', 'password');

$stream = ssh2_exec($connection, '/usr/local/bin/php -i');
stream_set_blocking$stream, true );
echo (stream_get_contents($stream));

$stream = ssh2_exec($connection, 'ls');
stream_set_blocking$stream, true );
echo (stream_get_contents($stream));

?>

 

php中的ssh2模块学习 中有更详细的介绍。

 

 

posted @ 2011-04-02 11:12  edwardlost  阅读(4895)  评论(0编辑  收藏  举报