前些天写的文档,现在整理到这里.
首先说说环境:
Centos 5.2, XAMPP 1.7
XAMPP本身是不支持ssh2.so,需要自己编译一个.哦,先介绍一下一下主角ssh2.so,他是一个PHP的SSH2扩展,有什么用呢?当然是通过PHP来紧系ssh连接.可以用来执行远程系统的一下命令之类的,用来管理大批linux服务器十分方便.
安装过程:
1,安装libssh2
安装之前可以先确认一下openssl是否已经安装.
rpm -qa | grep openssl
如果没有安装,到http://sourceforge.net/projects/libssh2/ 找新版本安装,这里我用了个老版本
wget http://nchc.dl.sourceforge.net/sourceforge/libssh2/libssh2-1.0.tar.gz
tar -zxvf libssh2-1.0.tar.gz
cd libssh2-1.0/
./configure
make all install
2.编译 ssh2
现在最新版本是ssh2-0.11.0,最新版本可以在http://pecl.php.net/package/ssh2 找到,如果有新版本尽量用新的版本,因为0.11.0有bug,一会就知道.
cd /tmp
wget http://pecl.php.net/get/ssh2-0.11.0.tgz
tar -zxvf ssh2-0.11.0.tgz
cd ssh2-0.11.0
/opt/lampp/bin/phpize && ./configure –with-ssh2 –with-php-config=/opt/lampp/bin/php-config && make
这里不是用普通的configure make all install,第一次用phpize迷惑了好久..好在xampp自带有.
不过这里编译老是编译不错,有如以下出错信息.
gcc -I. -I/opt/lampp/ssh2-0.11.0 -DPHP_ATOM_INC -I/opt/lampp/ssh2-0.11.0/include -I/opt/lampp/ssh2-0.11.0/main -I/opt/lampp/ssh2-0.11.0 -I/opt/lampp/include/
php -I/opt/lampp/include/php/main -I/opt/lampp/include/php/TSRM -I/opt/lampp/include/php/Zend -I/opt/lampp/include/php/ext -I/opt/lampp/include/php/ext/date/lib -I/opt/lampp/include/php -DHAVE_CONFIG_H -g -O2 -c /opt/lampp/ssh2-0.11.0/ssh2_fopen_wrappers.c -fPIC -DPIC -o .libs/ssh2_fopen_wrappers.o
/opt/lampp/ssh2-0.11.0/ssh2_fopen_wrappers.c: In function `php_ssh2_channel_stream_read’:
/opt/lampp/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: `LIBSSH2_ERROR_EAGAIN’ undeclared (first use in this function)
/opt/lampp/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: (Each undeclared identifier is reported only once
/opt/lampp/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: for each function it appears in.)
make: *** [ssh2_fopen_wrappers.lo] Error 1
cp modules/ssh2.so /opt/lampp/lib/php/extensions/no-debug-non-zts-20060613/
vi /opt/lampp/etc/php.iniextension=”ssh2.so”#添加
PHP扩展SSH2的使用简述:
这里有个不错的phpclass:http://www.phpclasses.org/browse/file/10338.html
下载ssh_in_php.php下来,下面是示范函数:
1 2 3 4 5 6 7 8 9 10 11 12 | function runssh($host,$port,$user,$pass,$cmd){ require_once ('ssh_in_php.php'); $ssh = new shell2; // init class if ( $ssh->login($user,$pass,$host,$port) ) { //SSH Command $ssh->exec_cmd($cmd); echo $ssh->get_output(); } else { echo $ssh->error; } $ssh->disconnect(); } |
这样就轻松实现远程执行cmd了,是不是很爽.
当前没有评论!
第一个在本文留言。