使用 php ssh2 模块可以在php程序中远程执行命令,只要编写一些简单的脚本工具就能实现对远程服务器的集中管理,大大简化系统运维工作的繁琐。
安装
1. 安装 libssh2 库
2. 安装 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
示例
$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));
?>